Skip to content

Commit

Permalink
minor #41973 Leverage str_ends_with (Tobion)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

Leverage str_ends_with

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        |

added the php80 polyfill to requirements when necessary. some components already had the requirement anyway.
Related to #41576

Commits
-------

9d80729 Leverage str_ends_with
  • Loading branch information
nicolas-grekas committed Jul 21, 2021
2 parents 29277c9 + 9d80729 commit a5675d3
Show file tree
Hide file tree
Showing 66 changed files with 80 additions and 62 deletions.
Expand Up @@ -96,7 +96,7 @@ protected function findExtension($name)
}
}

if ('Bundle' !== substr($name, -6)) {
if (!str_ends_with($name, 'Bundle')) {
$message = sprintf('No extensions with configuration available for "%s".', $name);
} else {
$message = sprintf('No extension with alias "%s" is enabled.', $name);
Expand Down
Expand Up @@ -81,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
// the old cache dir name must not be longer than the real one to avoid exceeding
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
$oldCacheDir = substr($realCacheDir, 0, -1).(str_ends_with($realCacheDir, '~') ? '+' : '~');
$fs->remove($oldCacheDir);

if (!is_writable($realCacheDir)) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Expand Up @@ -25,6 +25,7 @@
"symfony/http-foundation": "^4.4|^5.0",
"symfony/http-kernel": "^4.4",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.16",
"symfony/filesystem": "^3.4|^4.0|^5.0",
"symfony/finder": "^3.4|^4.0|^5.0",
"symfony/routing": "^4.4.12|^5.1.4"
Expand Down
Expand Up @@ -201,7 +201,7 @@ private function getBundleTemplatePaths(ContainerBuilder $container, array $conf

private function normalizeBundleName(string $name): string
{
if ('Bundle' === substr($name, -6)) {
if (str_ends_with($name, 'Bundle')) {
$name = substr($name, 0, -6);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/TemplateIterator.php
Expand Up @@ -61,7 +61,7 @@ public function getIterator()
}
foreach ($this->kernel->getBundles() as $bundle) {
$name = $bundle->getName();
if ('Bundle' === substr($name, -6)) {
if (str_ends_with($name, 'Bundle')) {
$name = substr($name, 0, -6);
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/composer.json
Expand Up @@ -21,6 +21,7 @@
"symfony/http-foundation": "^4.3|^5.0",
"symfony/http-kernel": "^4.4",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-php80": "^1.16",
"twig/twig": "^1.43|^2.13|^3.0.4"
},
"require-dev": {
Expand Down
Expand Up @@ -221,7 +221,7 @@ private function authorizesInline(array $directivesSet, string $type): bool
private function hasHashOrNonce(array $directives): bool
{
foreach ($directives as $directive) {
if ('\'' !== substr($directive, -1)) {
if (!str_ends_with($directive, '\'')) {
continue;
}
if ('\'nonce-' === substr($directive, 0, 7)) {
Expand Down
Expand Up @@ -82,7 +82,7 @@ public function getNames(Profile $profile)
continue;
}

if ('.html.twig' === substr($template, -10)) {
if (str_ends_with($template, '.html.twig')) {
$template = substr($template, 0, -10);
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/WebProfilerBundle/composer.json
Expand Up @@ -20,6 +20,7 @@
"symfony/config": "^4.2|^5.0",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/http-kernel": "^4.4",
"symfony/polyfill-php80": "^1.16",
"symfony/routing": "^4.3|^5.0",
"symfony/twig-bundle": "^4.2|^5.0",
"twig/twig": "^1.43|^2.13|^3.0.4"
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -688,7 +688,7 @@ protected function getAbsoluteUri($uri)
if ('/' !== $uri[0]) {
$path = parse_url($currentUri, \PHP_URL_PATH);

if ('/' !== substr($path, -1)) {
if (!str_ends_with($path, '/')) {
$path = substr($path, 0, strrpos($path, '/') + 1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/BrowserKit/CookieJar.php
Expand Up @@ -46,7 +46,7 @@ public function get($name, $path = '/', $domain = null)
foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
if ($cookieDomain && $domain) {
$cookieDomain = '.'.ltrim($cookieDomain, '.');
if ($cookieDomain !== substr('.'.$domain, -\strlen($cookieDomain))) {
if (!str_ends_with('.'.$domain, $cookieDomain)) {
continue;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/BrowserKit/composer.json
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": ">=7.1.3",
"symfony/dom-crawler": "^3.4|^4.0|^5.0"
"symfony/dom-crawler": "^3.4|^4.0|^5.0",
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"symfony/css-selector": "^3.4|^4.0|^5.0",
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function __construct(string $resource, string $sourceResource = null, ?in
// Include the previous exception, to help the user see what might be the underlying cause

// Trim the trailing period of the previous message. We only want 1 period remove so no rtrim...
if ('.' === substr($previous->getMessage(), -1)) {
if (str_ends_with($previous->getMessage(), '.')) {
$trimmedMessage = substr($previous->getMessage(), 0, -1);
$message .= sprintf('%s', $trimmedMessage).' in ';
} else {
Expand Down
Expand Up @@ -83,7 +83,7 @@ public function isFresh($timestamp)

// always monitor directories for changes, except the .. entries
// (otherwise deleted files wouldn't get detected)
if ($file->isDir() && '/..' === substr($file, -3)) {
if ($file->isDir() && str_ends_with($file, '/..')) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Config/composer.json
Expand Up @@ -19,6 +19,7 @@
"php": ">=7.1.3",
"symfony/filesystem": "^3.4|^4.0|^5.0",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-php80": "^1.16",
"symfony/polyfill-php81": "^1.22"
},
"require-dev": {
Expand Down
Expand Up @@ -54,7 +54,7 @@ public static function escape($text)
*/
public static function escapeTrailingBackslash(string $text): string
{
if ('\\' === substr($text, -1)) {
if (str_ends_with($text, '\\')) {
$len = \strlen($text);
$text = rtrim($text, '\\');
$text = str_replace("\0", '', $text);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Style/SymfonyStyle.php
Expand Up @@ -442,7 +442,7 @@ private function autoPrependText(): void
{
$fetched = $this->bufferedOutput->fetch();
//Prepend new line if last char isn't EOL:
if ("\n" !== substr($fetched, -1)) {
if (!str_ends_with($fetched, "\n")) {
$this->newLine();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/composer.json
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.1.3",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php73": "^1.8",
"symfony/polyfill-php80": "^1.15",
"symfony/polyfill-php80": "^1.16",
"symfony/service-contracts": "^1.1|^2"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/DebugClassLoader.php
Expand Up @@ -61,7 +61,7 @@ public function __construct(callable $classLoader)
if (false === $test || false === $i) {
// filesystem is case sensitive
self::$caseCheck = 0;
} elseif (substr($test, -\strlen($file)) === $file) {
} elseif (str_ends_with($test, $file)) {
// filesystem is case insensitive and realpath() normalizes the case of characters
self::$caseCheck = 1;
} elseif (false !== stripos(\PHP_OS, 'darwin')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.1.3",
"psr/log": "^1|^2|^3",
"symfony/polyfill-php80": "^1.15"
"symfony/polyfill-php80": "^1.16"
},
"conflict": {
"symfony/http-kernel": "<3.4"
Expand Down
Expand Up @@ -504,7 +504,7 @@ private function collectLineage(string $class, array &$lineage)
return;
}
$file = $r->getFileName();
if (') : eval()\'d code' === substr($file, -17)) {
if (str_ends_with($file, ') : eval()\'d code')) {
$file = substr($file, 0, strrpos($file, '(', -17));
}
if (!$file || $this->doExport($file) === $exportedFile = $this->export($file)) {
Expand Down Expand Up @@ -2095,7 +2095,7 @@ private function doExport($value, bool $resolveEnv = false)

if ($resolveEnv && "'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('string:%s').'")) {
$export = $resolvedExport;
if (".''" === substr($export, -3)) {
if (str_ends_with($export, ".''")) {
$export = substr($export, 0, -3);
if ("'" === $export[1]) {
$export = substr_replace($export, '', 18, 7);
Expand Down
Expand Up @@ -66,7 +66,7 @@ public function getNamespace()
public function getAlias()
{
$className = static::class;
if ('Extension' != substr($className, -9)) {
if (!str_ends_with($className, 'Extension')) {
throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
}
$classBaseName = substr(strrchr($className, '\\'), 1, -9);
Expand Down
Expand Up @@ -49,6 +49,6 @@ public function supports($resource, $type = null)
return true;
}

return null === $type && \is_string($resource) && '/' === substr($resource, -1);
return null === $type && \is_string($resource) && str_ends_with($resource, '/');
}
}
Expand Up @@ -91,7 +91,7 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
*/
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
{
if ('\\' !== substr($namespace, -1)) {
if (!str_ends_with($namespace, '\\')) {
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
}
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
Expand Down
Expand Up @@ -31,7 +31,7 @@ class EnvPlaceholderParameterBag extends ParameterBag
*/
public function get($name)
{
if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
if (0 === strpos($name, 'env(') && str_ends_with($name, ')') && 'env()' !== $name) {
$env = substr($name, 4, -1);

if (isset($this->envPlaceholders[$env])) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/composer.json
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": ">=7.1.3",
"psr/container": "^1.0",
"symfony/polyfill-php80": "^1.16",
"symfony/service-contracts": "^1.1.6|^2"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DomCrawler/AbstractUriElement.php
Expand Up @@ -142,7 +142,7 @@ protected function canonicalizePath($path)
return $path;
}

if ('.' === substr($path, -1)) {
if (str_ends_with($path, '.')) {
$path .= '/';
}

Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/DomCrawler/composer.json
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.1.3",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0"
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"symfony/css-selector": "^3.4|^4.0|^5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Expand Up @@ -205,7 +205,7 @@ public function __construct(callable $classLoader)
if (false === $test || false === $i) {
// filesystem is case sensitive
self::$caseCheck = 0;
} elseif (substr($test, -\strlen($file)) === $file) {
} elseif (str_ends_with($test, $file)) {
// filesystem is case insensitive and realpath() normalizes the case of characters
self::$caseCheck = 1;
} elseif (false !== stripos(\PHP_OS, 'darwin')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ErrorHandler/composer.json
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.1.3",
"psr/log": "^1|^2|^3",
"symfony/debug": "^4.4.5",
"symfony/polyfill-php80": "^1.15",
"symfony/polyfill-php80": "^1.16",
"symfony/var-dumper": "^4.4|^5.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Gitignore.php
Expand Up @@ -78,6 +78,6 @@ private static function lineToRegex(string $gitignoreLine): string

return ($isAbsolute ? '' : '(?:[^/]+/)*')
.$regex
.('/' !== substr($gitignoreLine, -1) ? '(?:$|/)' : '');
.(!str_ends_with($gitignoreLine, '/') ? '(?:$|/)' : '');
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Finder/composer.json
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=7.1.3"
"php": ">=7.1.3",
"symfony/polyfill-php80": "^1.16"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Finder\\": "" },
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/Command/DebugCommand.php
Expand Up @@ -167,7 +167,7 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, strin
$classes[] = $fqcn;
} elseif (class_exists($fqcn = $namespace.'\\'.ucfirst($shortClassName).'Type')) {
$classes[] = $fqcn;
} elseif ('type' === substr($shortClassName, -4) && class_exists($fqcn = $namespace.'\\'.ucfirst(substr($shortClassName, 0, -4).'Type'))) {
} elseif (str_ends_with($shortClassName, 'type') && class_exists($fqcn = $namespace.'\\'.ucfirst(substr($shortClassName, 0, -4).'Type'))) {
$classes[] = $fqcn;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/composer.json
Expand Up @@ -22,6 +22,7 @@
"symfony/options-resolver": "~4.3|^5.0",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.16",
"symfony/property-access": "^3.4.40|^4.4.8|^5.0.8",
"symfony/service-contracts": "^1.1|^2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/NativeHttpClient.php
Expand Up @@ -460,7 +460,7 @@ private static function configureHeadersAndProxy($context, string $host, array $
foreach ($noProxy as $rule) {
$dotRule = '.'.ltrim($rule, '.');

if ('*' === $rule || $host === $rule || substr($host, -\strlen($dotRule)) === $dotRule) {
if ('*' === $rule || $host === $rule || str_ends_with($host, $dotRule)) {
stream_context_set_option($context, 'http', 'proxy', null);
stream_context_set_option($context, 'http', 'request_fulluri', false);
stream_context_set_option($context, 'http', 'header', $requestHeaders);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/composer.json
Expand Up @@ -25,6 +25,7 @@
"psr/log": "^1|^2|^3",
"symfony/http-client-contracts": "^1.1.10|^2",
"symfony/polyfill-php73": "^1.11",
"symfony/polyfill-php80": "^1.16",
"symfony/service-contracts": "^1.0|^2"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -2011,7 +2011,7 @@ private function getTrustedValues(int $type, string $ip = null): array
continue;
}
if (self::HEADER_X_FORWARDED_PORT === $type) {
if (']' === substr($v, -1) || false === $v = strrchr($v, ':')) {
if (str_ends_with($v, ']') || false === $v = strrchr($v, ':')) {
$v = $this->isSecure() ? ':443' : ':80';
}
$v = '0.0.0.0'.$v;
Expand Down
Expand Up @@ -88,7 +88,7 @@ public function testGuessWithNonReadablePath()
touch($path);
@chmod($path, 0333);

if ('0333' == substr(sprintf('%o', fileperms($path)), -4)) {
if (str_ends_with(sprintf('%o', fileperms($path)), '0333')) {
$this->expectException(AccessDeniedException::class);
MimeTypeGuesser::getInstance()->guess($path);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/composer.json
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.1.3",
"symfony/mime": "^4.3|^5.0",
"symfony/polyfill-mbstring": "~1.1",
"symfony/polyfill-php80": "^1.15"
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"predis/predis": "~1.0",
Expand Down
Expand Up @@ -62,7 +62,7 @@ private function expandClasses(array $patterns, array $classes): array

// Explicit classes declared in the patterns are returned directly
foreach ($patterns as $key => $pattern) {
if ('\\' !== substr($pattern, -1) && false === strpos($pattern, '*')) {
if (!str_ends_with($pattern, '\\') && false === strpos($pattern, '*')) {
unset($patterns[$key]);
$expanded[] = ltrim($pattern, '\\');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Expand Up @@ -23,7 +23,7 @@
"symfony/http-foundation": "^4.4|^5.0",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-php73": "^1.9",
"symfony/polyfill-php80": "^1.15",
"symfony/polyfill-php80": "^1.16",
"psr/log": "^1|^2"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Messenger/composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.1.3",
"psr/log": "^1|^2|^3",
"symfony/polyfill-php80": "^1.15"
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"doctrine/dbal": "^2.6|^3.0",
Expand Down
Expand Up @@ -116,7 +116,7 @@ public function testGuessWithNonReadablePath()
touch($path);
@chmod($path, 0333);

if ('0333' == substr(sprintf('%o', fileperms($path)), -4)) {
if (str_ends_with(sprintf('%o', fileperms($path)), '0333')) {
$this->expectException(\InvalidArgumentException::class);
$this->getGuesser()->guessMimeType($path);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Mime/composer.json
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.1.3",
"symfony/polyfill-intl-idn": "^1.10",
"symfony/polyfill-mbstring": "^1.0"
"symfony/polyfill-mbstring": "^1.0",
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"egulias/email-validator": "^2.1.10|^3.1",
Expand Down

0 comments on commit a5675d3

Please sign in to comment.