Skip to content

Commit

Permalink
Merge branch '5.2' into 5.3
Browse files Browse the repository at this point in the history
* 5.2:
  Leverage str_contains/str_starts_with
  Leverage str_ends_with
  • Loading branch information
nicolas-grekas committed Jul 21, 2021
2 parents a917d7c + 62ae901 commit 6b12d5b
Show file tree
Hide file tree
Showing 26 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function setDeprecated(/* string $package, string $version, string $messa
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
}

if (false === strpos($message, '%alias_id%')) {
if (!str_contains($message, '%alias_id%')) {
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion ChildDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function replaceArgument($index, $value)
{
if (\is_int($index)) {
$this->arguments['index_'.$index] = $value;
} elseif (0 === strpos($index, '$')) {
} elseif (str_starts_with($index, '$')) {
$this->arguments[$index] = $value;
} else {
throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');
Expand Down
2 changes: 1 addition & 1 deletion Compiler/AutowirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private function getAutowiredReference(TypedReference $reference): ?TypedReferen

if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) {
foreach ($this->container->getAliases() as $id => $alias) {
if ($name === (string) $alias && 0 === strpos($id, $type.' $')) {
if ($name === (string) $alias && str_starts_with($id, $type.' $')) {
return new TypedReference($name, $type, $reference->getInvalidBehavior());
}
}
Expand Down
2 changes: 1 addition & 1 deletion Compiler/CheckDefinitionValidityPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function process(ContainerBuilder $container)
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));
}
if (class_exists($id) || interface_exists($id, false)) {
if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
if (str_starts_with($id, '\\') && 1 < substr_count($id, '\\')) {
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)));
}

Expand Down
2 changes: 1 addition & 1 deletion Compiler/CheckTypeDeclarationsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
$value = $this->container->getParameter(substr($value, 1, -1));
}

if ($envPlaceholderUniquePrefix && \is_string($value) && false !== strpos($value, 'env_')) {
if ($envPlaceholderUniquePrefix && \is_string($value) && str_contains($value, 'env_')) {
// 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.
// We don't need to change the value because it is already a string.
if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) {
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function addPass(CompilerPassInterface $pass, string $type = PassConfig::
*/
public function log(CompilerPassInterface $pass, string $message)
{
if (false !== strpos($message, "\n")) {
if (str_contains($message, "\n")) {
$message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
}

Expand Down
2 changes: 1 addition & 1 deletion Compiler/MergeExtensionConfigurationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
}

foreach ($bag->getEnvPlaceholders() as $env => $placeholders) {
if (false === strpos($env, ':')) {
if (!str_contains($env, ':')) {
continue;
}
foreach ($placeholders as $placeholder) {
Expand Down
2 changes: 1 addition & 1 deletion Compiler/RegisterServiceSubscribersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function processValue($value, bool $isRoot = false)
if ($name) {
if (false !== $i = strpos($name, '::get')) {
$name = lcfirst(substr($name, 5 + $i));
} elseif (false !== strpos($name, '::')) {
} elseif (str_contains($name, '::')) {
$name = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Compiler/ResolveBindingsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function process(ContainerBuilder $container)
foreach ($this->unusedBindings as [$key, $serviceId, $bindingType, $file]) {
$argumentType = $argumentName = $message = null;

if (false !== strpos($key, ' ')) {
if (str_contains($key, ' ')) {
[$argumentType, $argumentName] = explode(' ', $key, 2);
} elseif ('$' === $key[0]) {
$argumentName = $key;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/ResolveChildDefinitionsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private function doResolveDefinition(ChildDefinition $definition): Definition
foreach ($definition->getArguments() as $k => $v) {
if (is_numeric($k)) {
$def->addArgument($v);
} elseif (0 === strpos($k, 'index_')) {
} elseif (str_starts_with($k, 'index_')) {
$def->replaceArgument((int) substr($k, \strlen('index_')), $v);
} else {
$def->setArgument($k, $v);
Expand Down
2 changes: 1 addition & 1 deletion Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private function make(string $id, int $invalidBehavior)
continue;
}
$lev = levenshtein($id, $knownId);
if ($lev <= \strlen($id) / 3 || false !== strpos($knownId, $id)) {
if ($lev <= \strlen($id) / 3 || str_contains($knownId, $id)) {
$alternatives[] = $knownId;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ private function inVendors(string $path): bool
$path = realpath($path) ?: $path;

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

return true;
Expand Down
6 changes: 3 additions & 3 deletions Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function setFactory($factory)
{
$this->changes['factory'] = true;

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

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

if (\is_string($configurator) && false !== strpos($configurator, '::')) {
if (\is_string($configurator) && str_contains($configurator, '::')) {
$configurator = explode('::', $configurator, 2);
} elseif ($configurator instanceof Reference) {
$configurator = [$configurator, '__invoke'];
Expand Down
32 changes: 16 additions & 16 deletions Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function dump(array $options = [])
$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']));
$this->serviceLocatorTag = $options['service_locator_tag'];

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

foreach ($this->preload as $class) {
if (!$class || false !== strpos($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
if (!$class || str_contains($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
continue;
}
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) {
Expand Down Expand Up @@ -523,7 +523,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 @@ -656,7 +656,7 @@ private function addServiceInstance(string $id, Definition $definition, bool $is
{
$class = $this->dumpValue($definition->getClass());

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

Expand Down Expand Up @@ -783,11 +783,11 @@ private function addServiceConfigurator(Definition $definition, string $variable

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -1889,16 +1889,16 @@ private function dumpValue($value, bool $interpolate = true): string
*/
private function dumpLiteralClass(string $class): string
{
if (false !== strpos($class, '$')) {
if (str_contains($class, '$')) {
return sprintf('${($_ = %s) && false ?: "_"}', $class);
}
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)) {
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)) {
throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s).', $class ?: 'n/a'));
}

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

return 0 === strpos($class, '\\') ? $class : '\\'.$class;
return str_starts_with($class, '\\') ? $class : '\\'.$class;
}

private function dumpParameter(string $name): string
Expand Down Expand Up @@ -2146,7 +2146,7 @@ private function doExport($value, bool $resolveEnv = false)
if ($shouldCacheValue && isset($this->exportedVariables[$value])) {
return $this->exportedVariables[$value];
}
if (\is_string($value) && false !== strpos($value, "\n")) {
if (\is_string($value) && str_contains($value, "\n")) {
$cleanParts = explode("\n", $value);
$cleanParts = array_map(function ($part) { return var_export($part, true); }, $cleanParts);
$export = implode('."\n".', $cleanParts);
Expand All @@ -2164,7 +2164,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 @@ -2200,7 +2200,7 @@ private function getAutoloadFile(): ?string
}

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

if (null !== $this->targetDirRegex && preg_match($this->targetDirRegex.'A', $file)) {
Expand Down
2 changes: 1 addition & 1 deletion Dumper/YamlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private function prepareParameters(array $parameters, bool $escape = true): arra
foreach ($parameters as $key => $value) {
if (\is_array($value)) {
$value = $this->prepareParameters($value, $escape);
} elseif ($value instanceof Reference || \is_string($value) && 0 === strpos($value, '@')) {
} elseif ($value instanceof Reference || \is_string($value) && str_starts_with($value, '@')) {
$value = '@'.$value;
}

Expand Down
2 changes: 1 addition & 1 deletion EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
$env = $getEnv($name);
} elseif (isset($_ENV[$name])) {
$env = $_ENV[$name];
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
} elseif (isset($_SERVER[$name]) && !str_starts_with($name, 'HTTP_')) {
$env = $_SERVER[$name];
} elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
foreach ($this->loadedVars as $vars) {
Expand Down
4 changes: 2 additions & 2 deletions Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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 All @@ -82,7 +82,7 @@ public function getConfiguration(array $config, ContainerBuilder $container)
{
$class = static::class;

if (false !== strpos($class, "\0")) {
if (str_contains($class, "\0")) {
return null; // ignore anonymous classes
}

Expand Down
2 changes: 1 addition & 1 deletion Loader/DirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public function supports($resource, string $type = null)
return true;
}

return null === $type && \is_string($resource) && '/' === substr($resource, -1);
return null === $type && \is_string($resource) && str_ends_with($resource, '/');
}
}
4 changes: 2 additions & 2 deletions Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function import($resource, string $type = null, $ignoreErrors = false, st
*/
public function registerClasses(Definition $prototype, string $namespace, string $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 @@ -193,7 +193,7 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
if (null === $prefixLen) {
$prefixLen = \strlen($resource->getPrefix());

if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) {
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));
}
}
Expand Down
Loading

0 comments on commit 6b12d5b

Please sign in to comment.