Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor "substr" calls in lib/private to improve code readability #39216

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
* @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($formatDate, '^') && !str_ends_with($formatDate, '*')) {

Check failure on line 128 in lib/private/DateTimeFormatter.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedVariable

lib/private/DateTimeFormatter.php:128:22: UndefinedVariable: Cannot find referenced variable $formatDate (see https://psalm.dev/024)

Check failure on line 128 in lib/private/DateTimeFormatter.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedVariable

lib/private/DateTimeFormatter.php:128:58: UndefinedVariable: Cannot find referenced variable $formatDate (see https://psalm.dev/024)
shdehnavi marked this conversation as resolved.
Show resolved Hide resolved
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
$format .= '^';
}

Expand Down Expand Up @@ -289,7 +289,7 @@
* @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 @@ -85,7 +85,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 @@ -155,7 +155,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 @@ -610,7 +610,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
Loading