Skip to content

Commit

Permalink
Merge pull request #39216 from shdehnavi/replace_substr_calls_in_lib_…
Browse files Browse the repository at this point in the history
…private

Refactor "substr" calls in lib/private to improve code readability
  • Loading branch information
icewind1991 authored Sep 21, 2023
2 parents 667e9f7 + 0808e19 commit ef87ff1
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/private/DB/OracleMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $conn
* @return string
*/
protected function convertStatementToScript($statement) {
if (substr($statement, -1) === ';') {
if (str_ends_with($statement, ';')) {
return $statement . PHP_EOL . '/' . PHP_EOL;
}
$script = $statement . ';';
Expand Down
4 changes: 2 additions & 2 deletions lib/private/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function formatDate($timestamp, $format = 'long', \DateTimeZone $timeZone
* @return string Formatted relative date string
*/
public function formatDateRelativeDay($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
if (substr($format, -1) !== '*' && substr($format, -1) !== '*') {
if (!str_ends_with($format, '^') && !str_ends_with($format, '*')) {
$format .= '^';
}

Expand Down Expand Up @@ -289,7 +289,7 @@ public function formatDateTime($timestamp, $formatDate = 'long', $formatTime = '
* @return string Formatted relative date and time string
*/
public function formatDateTimeRelativeDay($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
if (substr($formatDate, -1) !== '^' && substr($formatDate, -1) !== '*') {
if (!str_ends_with($formatDate, '^') && !str_ends_with($formatDate, '*')) {
$formatDate .= '^';
}

Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/Storage/DAV.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public function __construct($params) {
if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
$host = $params['host'];
//remove leading http[s], will be generated in createBaseUri()
if (substr($host, 0, 8) == "https://") {
if (str_starts_with($host, "https://")) {
$host = substr($host, 8);
} elseif (substr($host, 0, 7) == "http://") {
} elseif (str_starts_with($host, "http://")) {
$host = substr($host, 7);
}
$this->host = $host;
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __construct($arguments) {
$realPath = realpath($this->datadir) ?: $this->datadir;
$this->realDataDir = rtrim($realPath, '/') . '/';
}
if (substr($this->datadir, -1) !== '/') {
if (!str_ends_with($this->datadir, '/')) {
$this->datadir .= '/';
}
$this->dataDirLength = strlen($this->realDataDir);
Expand Down Expand Up @@ -162,7 +162,7 @@ public function opendir($path) {
}

public function is_dir($path) {
if (substr($path, -1) == '/') {
if (str_ends_with($path, '/')) {
$path = substr($path, 0, -1);
}
return is_dir($this->getSourcePath($path));
Expand Down
4 changes: 2 additions & 2 deletions lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function findAvailableLanguages($app = null): array {
$files = scandir($dir);
if ($files !== false) {
foreach ($files as $file) {
if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
if (str_ends_with($file, '.json') && !str_starts_with($file, 'l10n')) {
$available[] = substr($file, 0, -5);
}
}
Expand All @@ -374,7 +374,7 @@ public function findAvailableLanguages($app = null): array {
$files = scandir($themeDir);
if ($files !== false) {
foreach ($files as $file) {
if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
if (str_ends_with($file, '.json') && !str_starts_with($file, 'l10n')) {
$available[] = substr($file, 0, -5);
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/private/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,23 @@ public function create($name,
*/
public function findMatchingRoute(string $url): array {
$this->eventLogger->start('route:match', 'Match route');
if (substr($url, 0, 6) === '/apps/') {
if (str_starts_with($url, '/apps/')) {
// empty string / 'apps' / $app / rest of the route
[, , $app,] = explode('/', $url, 4);

$app = \OC_App::cleanAppId($app);
\OC::$REQUESTEDAPP = $app;
$this->loadRoutes($app);
} elseif (substr($url, 0, 13) === '/ocsapp/apps/') {
} elseif (str_starts_with($url, '/ocsapp/apps/')) {
// empty string / 'ocsapp' / 'apps' / $app / rest of the route
[, , , $app,] = explode('/', $url, 5);

$app = \OC_App::cleanAppId($app);
\OC::$REQUESTEDAPP = $app;
$this->loadRoutes($app);
} elseif (substr($url, 0, 10) === '/settings/') {
} elseif (str_starts_with($url, '/settings/')) {
$this->loadRoutes('settings');
} elseif (substr($url, 0, 6) === '/core/') {
} elseif (str_starts_with($url, '/core/')) {
\OC::$REQUESTEDAPP = $url;
if (!$this->config->getSystemValueBool('maintenance') && !Util::needUpgrade()) {
\OC_App::loadApps();
Expand All @@ -277,7 +277,7 @@ public function findMatchingRoute(string $url): array {
try {
$parameters = $matcher->match($url);
} catch (ResourceNotFoundException $e) {
if (substr($url, -1) !== '/') {
if (!str_ends_with($url, '/')) {
// We allow links to apps/files? for backwards compatibility reasons
// However, since Symfony does not allow empty route names, the route
// we need to match is '/', so we need to append the '/' here.
Expand Down
2 changes: 1 addition & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function __construct($renderAs, $appId = '') {
$web = $info[1];
$file = $info[2];

if (substr($file, -strlen('print.css')) === 'print.css') {
if (str_ends_with($file, 'print.css')) {
$this->append('printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix());
} else {
$suffix = $this->getVersionHashSuffix($web, $file);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function linkTo(string $appName, string $file, array $args = []): string
$app_path = $this->getAppManager()->getAppPath($appName);
// Check if the app is in the app folder
if (file_exists($app_path . '/' . $file)) {
if (substr($file, -3) === 'php') {
if (str_ends_with($file, 'php')) {
$urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
if ($frontControllerActive) {
$urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public function getAvatarImage($size) {
public function getCloudId() {
$uid = $this->getUID();
$server = rtrim($this->urlGenerator->getAbsoluteURL('/'), '/');
if (substr($server, -10) === '/index.php') {
if (str_ends_with($server, '/index.php')) {
$server = substr($server, 0, -10);
}
$server = $this->removeProtocolFromUrl($server);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/legacy/OC_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function setContentType($format = null) {
protected static function isV2(\OCP\IRequest $request) {
$script = $request->getScriptName();

return substr($script, -11) === '/ocs/v2.php';
return str_ends_with($script, '/ocs/v2.php');
}

/**
Expand Down

0 comments on commit ef87ff1

Please sign in to comment.