Skip to content

Commit 4c21f24

Browse files
derrabusnicolas-grekas
authored andcommitted
Leverage str_contains/str_starts_with
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent e0c1083 commit 4c21f24

24 files changed

+51
-51
lines changed

Alias.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function setDeprecated($status = true, $template = null)
103103
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
104104
}
105105

106-
if (false === strpos($template, '%alias_id%')) {
106+
if (!str_contains($template, '%alias_id%')) {
107107
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
108108
}
109109

ChildDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function replaceArgument($index, $value)
9797
{
9898
if (\is_int($index)) {
9999
$this->arguments['index_'.$index] = $value;
100-
} elseif (0 === strpos($index, '$')) {
100+
} elseif (str_starts_with($index, '$')) {
101101
$this->arguments[$index] = $value;
102102
} else {
103103
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
@@ -297,7 +297,7 @@ private function getAutowiredReference(TypedReference $reference): ?TypedReferen
297297

298298
if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) {
299299
foreach ($this->container->getAliases() as $id => $alias) {
300-
if ($name === (string) $alias && 0 === strpos($id, $type.' $')) {
300+
if ($name === (string) $alias && str_starts_with($id, $type.' $')) {
301301
return new TypedReference($name, $type, $reference->getInvalidBehavior());
302302
}
303303
}

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
@@ -67,7 +67,7 @@ public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BE
6767
*/
6868
public function log(CompilerPassInterface $pass, string $message)
6969
{
70-
if (false !== strpos($message, "\n")) {
70+
if (str_contains($message, "\n")) {
7171
$message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
7272
}
7373

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
@@ -91,7 +91,7 @@ protected function processValue($value, $isRoot = false)
9191
if ($name) {
9292
if (false !== $i = strpos($name, '::get')) {
9393
$name = lcfirst(substr($name, 5 + $i));
94-
} elseif (false !== strpos($name, '::')) {
94+
} elseif (str_contains($name, '::')) {
9595
$name = null;
9696
}
9797
}

Compiler/ResolveBindingsPass.php

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

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

Compiler/ResolveChildDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private function doResolveDefinition(ChildDefinition $definition): Definition
158158
foreach ($definition->getArguments() as $k => $v) {
159159
if (is_numeric($k)) {
160160
$def->addArgument($v);
161-
} elseif (0 === strpos($k, 'index_')) {
161+
} elseif (str_starts_with($k, 'index_')) {
162162
$def->replaceArgument((int) substr($k, \strlen('index_')), $v);
163163
} else {
164164
$def->setArgument($k, $v);

Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private function make(string $id, int $invalidBehavior)
281281
continue;
282282
}
283283
$lev = levenshtein($id, $knownId);
284-
if ($lev <= \strlen($id) / 3 || false !== strpos($knownId, $id)) {
284+
if ($lev <= \strlen($id) / 3 || str_contains($knownId, $id)) {
285285
$alternatives[] = $knownId;
286286
}
287287
}

ContainerBuilder.php

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

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

16641664
return true;

Definition.php

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

113-
if (\is_string($factory) && false !== strpos($factory, '::')) {
113+
if (\is_string($factory) && str_contains($factory, '::')) {
114114
$factory = explode('::', $factory, 2);
115115
} elseif ($factory instanceof Reference) {
116116
$factory = [$factory, '__invoke'];
@@ -765,7 +765,7 @@ public function setDeprecated($status = true, $template = null)
765765
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
766766
}
767767

768-
if (false === strpos($template, '%service_id%')) {
768+
if (!str_contains($template, '%service_id%')) {
769769
throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
770770
}
771771

@@ -813,7 +813,7 @@ public function setConfigurator($configurator)
813813
{
814814
$this->changes['configurator'] = true;
815815

816-
if (\is_string($configurator) && false !== strpos($configurator, '::')) {
816+
if (\is_string($configurator) && str_contains($configurator, '::')) {
817817
$configurator = explode('::', $configurator, 2);
818818
} elseif ($configurator instanceof Reference) {
819819
$configurator = [$configurator, '__invoke'];

Dumper/PhpDumper.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function dump(array $options = [])
156156
$this->inlineRequires = $options['inline_class_loader_parameter'] && $this->container->hasParameter($options['inline_class_loader_parameter']) && $this->container->getParameter($options['inline_class_loader_parameter']);
157157
$this->serviceLocatorTag = $options['service_locator_tag'];
158158

159-
if (0 !== strpos($baseClass = $options['base_class'], '\\') && 'Container' !== $baseClass) {
159+
if (!str_starts_with($baseClass = $options['base_class'], '\\') && 'Container' !== $baseClass) {
160160
$baseClass = sprintf('%s\%s', $options['namespace'] ? '\\'.$options['namespace'] : '', $baseClass);
161161
$this->baseClass = $baseClass;
162162
} elseif ('Container' === $baseClass) {
@@ -313,7 +313,7 @@ public function dump(array $options = [])
313313
EOF;
314314

315315
foreach ($this->preload as $class) {
316-
if (!$class || false !== strpos($class, '$')) {
316+
if (!$class || str_contains($class, '$')) {
317317
continue;
318318
}
319319
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) {
@@ -631,7 +631,7 @@ private function addServiceInstance(string $id, Definition $definition, bool $is
631631
{
632632
$class = $this->dumpValue($definition->getClass());
633633

634-
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)) {
634+
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)) {
635635
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
636636
}
637637

@@ -758,11 +758,11 @@ private function addServiceConfigurator(Definition $definition, string $variable
758758

759759
$class = $this->dumpValue($callable[0]);
760760
// If the class is a string we can optimize away
761-
if (0 === strpos($class, "'") && false === strpos($class, '$')) {
761+
if (str_starts_with($class, "'") && !str_contains($class, '$')) {
762762
return sprintf(" %s::%s(\$%s);\n", $this->dumpLiteralClass($class), $callable[1], $variableName);
763763
}
764764

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

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

784784
if ($class = $definition->getClass()) {
785785
$class = $class instanceof Parameter ? '%'.$class.'%' : $this->container->resolveEnvPlaceholders($class);
786-
$return[] = sprintf(0 === strpos($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
786+
$return[] = sprintf(str_starts_with($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
787787
} elseif ($definition->getFactory()) {
788788
$factory = $definition->getFactory();
789789
if (\is_string($factory)) {
@@ -796,7 +796,7 @@ private function addService(string $id, Definition $definition): array
796796
}
797797

798798
if ($definition->isDeprecated()) {
799-
if ($return && 0 === strpos($return[\count($return) - 1], '@return')) {
799+
if ($return && str_starts_with($return[\count($return) - 1], '@return')) {
800800
$return[] = '';
801801
}
802802

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

11001100
$class = $this->dumpValue($callable[0]);
11011101
// If the class is a string we can optimize away
1102-
if (0 === strpos($class, "'") && false === strpos($class, '$')) {
1102+
if (str_starts_with($class, "'") && !str_contains($class, '$')) {
11031103
if ("''" === $class) {
11041104
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'));
11051105
}
11061106

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

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

@@ -1828,16 +1828,16 @@ private function dumpValue($value, bool $interpolate = true): string
18281828
*/
18291829
private function dumpLiteralClass(string $class): string
18301830
{
1831-
if (false !== strpos($class, '$')) {
1831+
if (str_contains($class, '$')) {
18321832
return sprintf('${($_ = %s) && false ?: "_"}', $class);
18331833
}
1834-
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)) {
1834+
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)) {
18351835
throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s).', $class ?: 'n/a'));
18361836
}
18371837

18381838
$class = substr(str_replace('\\\\', '\\', $class), 1, -1);
18391839

1840-
return 0 === strpos($class, '\\') ? $class : '\\'.$class;
1840+
return str_starts_with($class, '\\') ? $class : '\\'.$class;
18411841
}
18421842

18431843
private function dumpParameter(string $name): string
@@ -2085,7 +2085,7 @@ private function doExport($value, bool $resolveEnv = false)
20852085
if ($shouldCacheValue && isset($this->exportedVariables[$value])) {
20862086
return $this->exportedVariables[$value];
20872087
}
2088-
if (\is_string($value) && false !== strpos($value, "\n")) {
2088+
if (\is_string($value) && str_contains($value, "\n")) {
20892089
$cleanParts = explode("\n", $value);
20902090
$cleanParts = array_map(function ($part) { return var_export($part, true); }, $cleanParts);
20912091
$export = implode('."\n".', $cleanParts);
@@ -2131,7 +2131,7 @@ private function getAutoloadFile(): ?string
21312131
}
21322132

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

21372137
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
@@ -325,7 +325,7 @@ private function prepareParameters(array $parameters, bool $escape = true): arra
325325
foreach ($parameters as $key => $value) {
326326
if (\is_array($value)) {
327327
$value = $this->prepareParameters($value, $escape);
328-
} elseif ($value instanceof Reference || \is_string($value) && 0 === strpos($value, '@')) {
328+
} elseif ($value instanceof Reference || \is_string($value) && str_starts_with($value, '@')) {
329329
$value = '@'.$value;
330330
}
331331

EnvVarProcessor.php

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

Extension/Extension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getConfiguration(array $config, ContainerBuilder $container)
8181
{
8282
$class = static::class;
8383

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

Loader/FileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
179179
if (null === $prefixLen) {
180180
$prefixLen = \strlen($resource->getPrefix());
181181

182-
if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
182+
if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) {
183183
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));
184184
}
185185
}

Loader/XmlFileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private function parseDefinition(\DOMElement $service, string $file, array $defa
337337
continue;
338338
}
339339

340-
if (false !== strpos($name, '-') && false === strpos($name, '_') && !\array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
340+
if (str_contains($name, '-') && !str_contains($name, '_') && !\array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
341341
$parameters[$normalizedName] = XmlUtils::phpize($node->nodeValue);
342342
}
343343
// keep not normalized key
@@ -622,7 +622,7 @@ public function validateSchema(\DOMDocument $dom)
622622
array_shift($parts);
623623
$locationstart = 'phar:///';
624624
}
625-
} elseif ('\\' === \DIRECTORY_SEPARATOR && 0 === strpos($location, '\\\\')) {
625+
} elseif ('\\' === \DIRECTORY_SEPARATOR && str_starts_with($location, '\\\\')) {
626626
$locationstart = '';
627627
}
628628
$drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';

0 commit comments

Comments
 (0)