Skip to content

Commit 6b12d5b

Browse files
Merge branch '5.2' into 5.3
* 5.2: Leverage str_contains/str_starts_with Leverage str_ends_with
2 parents a917d7c + 62ae901 commit 6b12d5b

26 files changed

+56
-56
lines changed

Alias.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function setDeprecated(/* string $package, string $version, string $messa
112112
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
113113
}
114114

115-
if (false === strpos($message, '%alias_id%')) {
115+
if (!str_contains($message, '%alias_id%')) {
116116
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
117117
}
118118
}

ChildDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function replaceArgument($index, $value)
9393
{
9494
if (\is_int($index)) {
9595
$this->arguments['index_'.$index] = $value;
96-
} elseif (0 === strpos($index, '$')) {
96+
} elseif (str_starts_with($index, '$')) {
9797
$this->arguments[$index] = $value;
9898
} else {
9999
throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');

Compiler/AutowirePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private function getAutowiredReference(TypedReference $reference): ?TypedReferen
324324

325325
if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) {
326326
foreach ($this->container->getAliases() as $id => $alias) {
327-
if ($name === (string) $alias && 0 === strpos($id, $type.' $')) {
327+
if ($name === (string) $alias && str_starts_with($id, $type.' $')) {
328328
return new TypedReference($name, $type, $reference->getInvalidBehavior());
329329
}
330330
}

Compiler/CheckDefinitionValidityPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function process(ContainerBuilder $container)
4949
throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
5050
}
5151
if (class_exists($id) || interface_exists($id, false)) {
52-
if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
52+
if (str_starts_with($id, '\\') && 1 < substr_count($id, '\\')) {
5353
throw new RuntimeException(sprintf('The definition for "%s" has no class attribute, and appears to reference a class or interface. Please specify the class attribute explicitly or remove the leading backslash by renaming the service to "%s" to get rid of this error.', $id, substr($id, 1)));
5454
}
5555

Compiler/CheckTypeDeclarationsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
231231
$value = $this->container->getParameter(substr($value, 1, -1));
232232
}
233233

234-
if ($envPlaceholderUniquePrefix && \is_string($value) && false !== strpos($value, 'env_')) {
234+
if ($envPlaceholderUniquePrefix && \is_string($value) && str_contains($value, 'env_')) {
235235
// If the value is an env placeholder that is either mixed with a string or with another env placeholder, then its resolved value will always be a string, so we don't need to resolve it.
236236
// We don't need to change the value because it is already a string.
237237
if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) {

Compiler/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function addPass(CompilerPassInterface $pass, string $type = PassConfig::
6464
*/
6565
public function log(CompilerPassInterface $pass, string $message)
6666
{
67-
if (false !== strpos($message, "\n")) {
67+
if (str_contains($message, "\n")) {
6868
$message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
6969
}
7070

Compiler/MergeExtensionConfigurationPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
209209
}
210210

211211
foreach ($bag->getEnvPlaceholders() as $env => $placeholders) {
212-
if (false === strpos($env, ':')) {
212+
if (!str_contains($env, ':')) {
213213
continue;
214214
}
215215
foreach ($placeholders as $placeholder) {

Compiler/RegisterServiceSubscribersPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected function processValue($value, bool $isRoot = false)
9898
if ($name) {
9999
if (false !== $i = strpos($name, '::get')) {
100100
$name = lcfirst(substr($name, 5 + $i));
101-
} elseif (false !== strpos($name, '::')) {
101+
} elseif (str_contains($name, '::')) {
102102
$name = null;
103103
}
104104
}

Compiler/ResolveBindingsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function process(ContainerBuilder $container)
4545
foreach ($this->unusedBindings as [$key, $serviceId, $bindingType, $file]) {
4646
$argumentType = $argumentName = $message = null;
4747

48-
if (false !== strpos($key, ' ')) {
48+
if (str_contains($key, ' ')) {
4949
[$argumentType, $argumentName] = explode(' ', $key, 2);
5050
} elseif ('$' === $key[0]) {
5151
$argumentName = $key;

Compiler/ResolveChildDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private function doResolveDefinition(ChildDefinition $definition): Definition
164164
foreach ($definition->getArguments() as $k => $v) {
165165
if (is_numeric($k)) {
166166
$def->addArgument($v);
167-
} elseif (0 === strpos($k, 'index_')) {
167+
} elseif (str_starts_with($k, 'index_')) {
168168
$def->replaceArgument((int) substr($k, \strlen('index_')), $v);
169169
} else {
170170
$def->setArgument($k, $v);

Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private function make(string $id, int $invalidBehavior)
263263
continue;
264264
}
265265
$lev = levenshtein($id, $knownId);
266-
if ($lev <= \strlen($id) / 3 || false !== strpos($knownId, $id)) {
266+
if ($lev <= \strlen($id) / 3 || str_contains($knownId, $id)) {
267267
$alternatives[] = $knownId;
268268
}
269269
}

ContainerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ private function inVendors(string $path): bool
16521652
$path = realpath($path) ?: $path;
16531653

16541654
foreach ($this->vendors as $vendor) {
1655-
if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
1655+
if (str_starts_with($path, $vendor) && false !== strpbrk(substr($path, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
16561656
$this->addResource(new FileResource($vendor.'/composer/installed.json'));
16571657

16581658
return true;

Definition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function setFactory($factory)
104104
{
105105
$this->changes['factory'] = true;
106106

107-
if (\is_string($factory) && false !== strpos($factory, '::')) {
107+
if (\is_string($factory) && str_contains($factory, '::')) {
108108
$factory = explode('::', $factory, 2);
109109
} elseif ($factory instanceof Reference) {
110110
$factory = [$factory, '__invoke'];
@@ -737,7 +737,7 @@ public function setDeprecated(/* string $package, string $version, string $messa
737737
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
738738
}
739739

740-
if (false === strpos($message, '%service_id%')) {
740+
if (!str_contains($message, '%service_id%')) {
741741
throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
742742
}
743743
}
@@ -798,7 +798,7 @@ public function setConfigurator($configurator)
798798
{
799799
$this->changes['configurator'] = true;
800800

801-
if (\is_string($configurator) && false !== strpos($configurator, '::')) {
801+
if (\is_string($configurator) && str_contains($configurator, '::')) {
802802
$configurator = explode('::', $configurator, 2);
803803
} elseif ($configurator instanceof Reference) {
804804
$configurator = [$configurator, '__invoke'];

Dumper/PhpDumper.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function dump(array $options = [])
160160
$this->inlineRequires = $options['inline_class_loader_parameter'] && ($this->container->hasParameter($options['inline_class_loader_parameter']) ? $this->container->getParameter($options['inline_class_loader_parameter']) : (\PHP_VERSION_ID < 70400 || $options['debug']));
161161
$this->serviceLocatorTag = $options['service_locator_tag'];
162162

163-
if (0 !== strpos($baseClass = $options['base_class'], '\\') && 'Container' !== $baseClass) {
163+
if (!str_starts_with($baseClass = $options['base_class'], '\\') && 'Container' !== $baseClass) {
164164
$baseClass = sprintf('%s\%s', $options['namespace'] ? '\\'.$options['namespace'] : '', $baseClass);
165165
$this->baseClass = $baseClass;
166166
} elseif ('Container' === $baseClass) {
@@ -332,7 +332,7 @@ class %s extends {$options['class']}
332332
EOF;
333333

334334
foreach ($this->preload as $class) {
335-
if (!$class || false !== strpos($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
335+
if (!$class || str_contains($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
336336
continue;
337337
}
338338
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) {
@@ -523,7 +523,7 @@ private function collectLineage(string $class, array &$lineage)
523523
return;
524524
}
525525
$file = $r->getFileName();
526-
if (') : eval()\'d code' === substr($file, -17)) {
526+
if (str_ends_with($file, ') : eval()\'d code')) {
527527
$file = substr($file, 0, strrpos($file, '(', -17));
528528
}
529529
if (!$file || $this->doExport($file) === $exportedFile = $this->export($file)) {
@@ -656,7 +656,7 @@ private function addServiceInstance(string $id, Definition $definition, bool $is
656656
{
657657
$class = $this->dumpValue($definition->getClass());
658658

659-
if (0 === strpos($class, "'") && false === strpos($class, '$') && !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
659+
if (str_starts_with($class, "'") && !str_contains($class, '$') && !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
660660
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
661661
}
662662

@@ -783,11 +783,11 @@ private function addServiceConfigurator(Definition $definition, string $variable
783783

784784
$class = $this->dumpValue($callable[0]);
785785
// If the class is a string we can optimize away
786-
if (0 === strpos($class, "'") && false === strpos($class, '$')) {
786+
if (str_starts_with($class, "'") && !str_contains($class, '$')) {
787787
return sprintf(" %s::%s(\$%s);\n", $this->dumpLiteralClass($class), $callable[1], $variableName);
788788
}
789789

790-
if (0 === strpos($class, 'new ')) {
790+
if (str_starts_with($class, 'new ')) {
791791
return sprintf(" (%s)->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
792792
}
793793

@@ -808,7 +808,7 @@ private function addService(string $id, Definition $definition): array
808808

809809
if ($class = $definition->getClass()) {
810810
$class = $class instanceof Parameter ? '%'.$class.'%' : $this->container->resolveEnvPlaceholders($class);
811-
$return[] = sprintf(0 === strpos($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
811+
$return[] = sprintf(str_starts_with($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
812812
} elseif ($definition->getFactory()) {
813813
$factory = $definition->getFactory();
814814
if (\is_string($factory)) {
@@ -821,7 +821,7 @@ private function addService(string $id, Definition $definition): array
821821
}
822822

823823
if ($definition->isDeprecated()) {
824-
if ($return && 0 === strpos($return[\count($return) - 1], '@return')) {
824+
if ($return && str_starts_with($return[\count($return) - 1], '@return')) {
825825
$return[] = '';
826826
}
827827

@@ -1151,15 +1151,15 @@ private function addNewInstance(Definition $definition, string $return = '', str
11511151

11521152
$class = $this->dumpValue($callable[0]);
11531153
// If the class is a string we can optimize away
1154-
if (0 === strpos($class, "'") && false === strpos($class, '$')) {
1154+
if (str_starts_with($class, "'") && !str_contains($class, '$')) {
11551155
if ("''" === $class) {
11561156
throw new RuntimeException(sprintf('Cannot dump definition: "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id ? 'The "'.$id.'"' : 'inline'));
11571157
}
11581158

11591159
return $return.sprintf('%s::%s(%s)', $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '').$tail;
11601160
}
11611161

1162-
if (0 === strpos($class, 'new ')) {
1162+
if (str_starts_with($class, 'new ')) {
11631163
return $return.sprintf('(%s)->%s(%s)', $class, $callable[1], $arguments ? implode(', ', $arguments) : '').$tail;
11641164
}
11651165

@@ -1889,16 +1889,16 @@ private function dumpValue($value, bool $interpolate = true): string
18891889
*/
18901890
private function dumpLiteralClass(string $class): string
18911891
{
1892-
if (false !== strpos($class, '$')) {
1892+
if (str_contains($class, '$')) {
18931893
return sprintf('${($_ = %s) && false ?: "_"}', $class);
18941894
}
1895-
if (0 !== strpos($class, "'") || !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
1895+
if (!str_starts_with($class, "'") || !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
18961896
throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s).', $class ?: 'n/a'));
18971897
}
18981898

18991899
$class = substr(str_replace('\\\\', '\\', $class), 1, -1);
19001900

1901-
return 0 === strpos($class, '\\') ? $class : '\\'.$class;
1901+
return str_starts_with($class, '\\') ? $class : '\\'.$class;
19021902
}
19031903

19041904
private function dumpParameter(string $name): string
@@ -2146,7 +2146,7 @@ private function doExport($value, bool $resolveEnv = false)
21462146
if ($shouldCacheValue && isset($this->exportedVariables[$value])) {
21472147
return $this->exportedVariables[$value];
21482148
}
2149-
if (\is_string($value) && false !== strpos($value, "\n")) {
2149+
if (\is_string($value) && str_contains($value, "\n")) {
21502150
$cleanParts = explode("\n", $value);
21512151
$cleanParts = array_map(function ($part) { return var_export($part, true); }, $cleanParts);
21522152
$export = implode('."\n".', $cleanParts);
@@ -2164,7 +2164,7 @@ private function doExport($value, bool $resolveEnv = false)
21642164

21652165
if ($resolveEnv && "'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('string:%s').'")) {
21662166
$export = $resolvedExport;
2167-
if (".''" === substr($export, -3)) {
2167+
if (str_ends_with($export, ".''")) {
21682168
$export = substr($export, 0, -3);
21692169
if ("'" === $export[1]) {
21702170
$export = substr_replace($export, '', 18, 7);
@@ -2200,7 +2200,7 @@ private function getAutoloadFile(): ?string
22002200
}
22012201

22022202
foreach (get_declared_classes() as $class) {
2203-
if (0 === strpos($class, 'ComposerAutoloaderInit') && $class::getLoader() === $autoloader[0]) {
2203+
if (str_starts_with($class, 'ComposerAutoloaderInit') && $class::getLoader() === $autoloader[0]) {
22042204
$file = \dirname((new \ReflectionClass($class))->getFileName(), 2).'/autoload.php';
22052205

22062206
if (null !== $this->targetDirRegex && preg_match($this->targetDirRegex.'A', $file)) {

Dumper/YamlDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private function prepareParameters(array $parameters, bool $escape = true): arra
347347
foreach ($parameters as $key => $value) {
348348
if (\is_array($value)) {
349349
$value = $this->prepareParameters($value, $escape);
350-
} elseif ($value instanceof Reference || \is_string($value) && 0 === strpos($value, '@')) {
350+
} elseif ($value instanceof Reference || \is_string($value) && str_starts_with($value, '@')) {
351351
$value = '@'.$value;
352352
}
353353

EnvVarProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
130130
$env = $getEnv($name);
131131
} elseif (isset($_ENV[$name])) {
132132
$env = $_ENV[$name];
133-
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
133+
} elseif (isset($_SERVER[$name]) && !str_starts_with($name, 'HTTP_')) {
134134
$env = $_SERVER[$name];
135135
} elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
136136
foreach ($this->loadedVars as $vars) {

Extension/Extension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getNamespace()
6767
public function getAlias()
6868
{
6969
$className = static::class;
70-
if ('Extension' != substr($className, -9)) {
70+
if (!str_ends_with($className, 'Extension')) {
7171
throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
7272
}
7373
$classBaseName = substr(strrchr($className, '\\'), 1, -9);
@@ -82,7 +82,7 @@ public function getConfiguration(array $config, ContainerBuilder $container)
8282
{
8383
$class = static::class;
8484

85-
if (false !== strpos($class, "\0")) {
85+
if (str_contains($class, "\0")) {
8686
return null; // ignore anonymous classes
8787
}
8888

Loader/DirectoryLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ public function supports($resource, string $type = null)
4949
return true;
5050
}
5151

52-
return null === $type && \is_string($resource) && '/' === substr($resource, -1);
52+
return null === $type && \is_string($resource) && str_ends_with($resource, '/');
5353
}
5454
}

Loader/FileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function import($resource, string $type = null, $ignoreErrors = false, st
9191
*/
9292
public function registerClasses(Definition $prototype, string $namespace, string $resource, $exclude = null)
9393
{
94-
if ('\\' !== substr($namespace, -1)) {
94+
if (!str_ends_with($namespace, '\\')) {
9595
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
9696
}
9797
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
@@ -193,7 +193,7 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
193193
if (null === $prefixLen) {
194194
$prefixLen = \strlen($resource->getPrefix());
195195

196-
if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
196+
if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) {
197197
throw new InvalidArgumentException(sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s).', $namespace, $excludePattern, $pattern));
198198
}
199199
}

0 commit comments

Comments
 (0)