diff --git a/src/DI/Autowiring.php b/src/DI/Autowiring.php index a351e724c..3fa4a25da 100644 --- a/src/DI/Autowiring.php +++ b/src/DI/Autowiring.php @@ -61,7 +61,7 @@ public function getByType(string $type, bool $throw = false): ?string } else { $list = $types[$type]; natsort($list); - $hint = count($list) === 2 && ($tmp = strpos($list[0], '.') xor strpos($list[1], '.')) + $hint = count($list) === 2 && ($tmp = str_contains($list[0], '.') xor str_contains($list[1], '.')) ? '. If you want to overwrite service ' . $list[$tmp ? 0 : 1] . ', give it proper name.' : ''; throw new ServiceCreationException(sprintf( diff --git a/src/DI/CompilerExtension.php b/src/DI/CompilerExtension.php index ab8db4e04..3a258989f 100644 --- a/src/DI/CompilerExtension.php +++ b/src/DI/CompilerExtension.php @@ -137,7 +137,7 @@ public function getInitialization(): Nette\PhpGenerator\Closure */ public function prefix(string $id): string { - return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0); + return substr_replace($id, $this->name . '.', str_starts_with($id, '@') ? 1 : 0, 0); } diff --git a/src/DI/Config/Adapters/NeonAdapter.php b/src/DI/Config/Adapters/NeonAdapter.php index a6474f7a9..aed858203 100644 --- a/src/DI/Config/Adapters/NeonAdapter.php +++ b/src/DI/Config/Adapters/NeonAdapter.php @@ -40,7 +40,7 @@ public function process(array $arr): array { $res = []; foreach ($arr as $key => $val) { - if (is_string($key) && substr($key, -1) === self::PREVENT_MERGING_SUFFIX) { + if (is_string($key) && str_ends_with($key, self::PREVENT_MERGING_SUFFIX)) { if (!is_array($val) && $val !== null) { throw new Nette\DI\InvalidConfigurationException(sprintf( "Replacing operator is available only for arrays, item '%s' is not array.", @@ -66,7 +66,7 @@ public function process(array $arr): array $val = $tmp; } else { $tmp = $this->process([$val->value]); - if (is_string($tmp[0]) && strpos($tmp[0], '?') !== false) { + if (is_string($tmp[0]) && str_contains($tmp[0], '?')) { trigger_error('Operator ? is deprecated in config files.', E_USER_DEPRECATED); } $val = new Statement($tmp[0], $this->process($val->attributes)); diff --git a/src/DI/Definitions/AccessorDefinition.php b/src/DI/Definitions/AccessorDefinition.php index e147ea4ab..b126d1ba2 100644 --- a/src/DI/Definitions/AccessorDefinition.php +++ b/src/DI/Definitions/AccessorDefinition.php @@ -69,7 +69,7 @@ public function setReference(string|Reference $reference): static if ($reference instanceof Reference) { $this->reference = $reference; } else { - $this->reference = substr($reference, 0, 1) === '@' + $this->reference = str_starts_with($reference, '@') ? new Reference(substr($reference, 1)) : Reference::fromType($reference); } diff --git a/src/DI/Definitions/LocatorDefinition.php b/src/DI/Definitions/LocatorDefinition.php index 2450631c9..5f0928ebe 100644 --- a/src/DI/Definitions/LocatorDefinition.php +++ b/src/DI/Definitions/LocatorDefinition.php @@ -61,7 +61,7 @@ public function setReferences(array $references): static { $this->references = []; foreach ($references as $name => $ref) { - $this->references[$name] = substr($ref, 0, 1) === '@' + $this->references[$name] = str_starts_with($ref, '@') ? new Reference(substr($ref, 1)) : Reference::fromType($ref); } diff --git a/src/DI/Definitions/Reference.php b/src/DI/Definitions/Reference.php index d0b502ea1..c303b01ea 100644 --- a/src/DI/Definitions/Reference.php +++ b/src/DI/Definitions/Reference.php @@ -26,7 +26,7 @@ final class Reference public static function fromType(string $value): static { - if (strpos($value, '\\') === false) { + if (!str_contains($value, '\\')) { $value = '\\' . $value; } return new static($value); @@ -47,13 +47,13 @@ public function getValue(): string public function isName(): bool { - return strpos($this->value, '\\') === false && $this->value !== self::SELF; + return !str_contains($this->value, '\\') && $this->value !== self::SELF; } public function isType(): bool { - return strpos($this->value, '\\') !== false; + return str_contains($this->value, '\\'); } diff --git a/src/DI/Definitions/Statement.php b/src/DI/Definitions/Statement.php index 616b88360..9c6377386 100644 --- a/src/DI/Definitions/Statement.php +++ b/src/DI/Definitions/Statement.php @@ -48,9 +48,9 @@ public function __construct(string|array|Definition|Reference|null $entity, arra if (is_string($entity) && Strings::contains($entity, '::') && !Strings::contains($entity, '?')) { $entity = explode('::', $entity, 2); } - if (is_string($entity) && substr($entity, 0, 1) === '@') { // normalize @service to Reference + if (is_string($entity) && str_starts_with($entity, '@')) { // normalize @service to Reference $entity = new Reference(substr($entity, 1)); - } elseif (is_array($entity) && is_string($entity[0]) && substr($entity[0], 0, 1) === '@') { + } elseif (is_array($entity) && is_string($entity[0]) && str_starts_with($entity[0], '@')) { $entity[0] = new Reference(substr($entity[0], 1)); } diff --git a/src/DI/DependencyChecker.php b/src/DI/DependencyChecker.php index 79b5a467e..05b5e009f 100644 --- a/src/DI/DependencyChecker.php +++ b/src/DI/DependencyChecker.php @@ -140,7 +140,7 @@ class_uses($name), $flip = array_flip($classes); foreach ($functions as $name) { - if (strpos($name, '::')) { + if (str_contains($name, '::')) { $method = new ReflectionMethod($name); $class = $method->getDeclaringClass(); if (isset($flip[$class->name])) { diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index df2445199..d7df36ad3 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -89,7 +89,7 @@ public static function getInjectMethods(string $class): array { $classes = []; foreach (get_class_methods($class) as $name) { - if (substr($name, 0, 6) === 'inject') { + if (str_starts_with($name, 'inject')) { $classes[$name] = (new \ReflectionMethod($class, $name))->getDeclaringClass()->name; } } @@ -114,7 +114,7 @@ public static function getInjectProperties(string $class): array if ($hasAttr || DI\Helpers::parseAnnotation($rp, 'inject') !== null) { if ($type = Reflection::getPropertyType($rp)) { } elseif (!$hasAttr && ($type = DI\Helpers::parseAnnotation($rp, 'var'))) { - if (strpos($type, '|') !== false) { + if (str_contains($type, '|')) { throw new Nette\InvalidStateException(sprintf( 'The %s is not expected to have a union type.', Reflection::toString($rp), diff --git a/src/DI/Extensions/SearchExtension.php b/src/DI/Extensions/SearchExtension.php index c05460a2e..c6640091b 100644 --- a/src/DI/Extensions/SearchExtension.php +++ b/src/DI/Extensions/SearchExtension.php @@ -139,7 +139,7 @@ private static function buildNameRegexp(array $masks): ?string { $res = []; foreach ((array) $masks as $mask) { - $mask = (strpos($mask, '\\') === false ? '**\\' : '') . $mask; + $mask = (str_contains($mask, '\\') ? '' : '**\\') . $mask; $mask = preg_quote($mask, '#'); $mask = str_replace('\*\*\\\\', '(.*\\\\)?', $mask); $mask = str_replace('\\\\\*\*', '(\\\\.*)?', $mask); diff --git a/src/DI/Extensions/ServicesExtension.php b/src/DI/Extensions/ServicesExtension.php index a00d4b241..27e4a2a2a 100644 --- a/src/DI/Extensions/ServicesExtension.php +++ b/src/DI/Extensions/ServicesExtension.php @@ -72,7 +72,7 @@ private function loadDefinition(?string $name, \stdClass $config): void } catch (\Exception $e) { $message = $e->getMessage(); - if ($name && !Nette\Utils\Strings::startsWith($message, '[Service ')) { + if ($name && !str_starts_with($message, '[Service ')) { $message = "[Service '$name']\n$message"; } throw new Nette\DI\InvalidConfigurationException($message, 0, $e); diff --git a/src/DI/PhpGenerator.php b/src/DI/PhpGenerator.php index 44a3d439a..44f732948 100644 --- a/src/DI/PhpGenerator.php +++ b/src/DI/PhpGenerator.php @@ -126,7 +126,7 @@ public function formatStatement(Statement $statement): string switch (true) { case $entity[1][0] === '$': // property getter, setter or appender $name = substr($entity[1], 1); - if ($append = (substr($name, -2) === '[]')) { + if ($append = (str_ends_with($name, '[]'))) { $name = substr($name, 0, -2); } $prop = $entity[0] instanceof Reference @@ -138,7 +138,7 @@ public function formatStatement(Statement $statement): string case $entity[0] instanceof Statement: $inner = $this->formatPhp('?', [$entity[0]]); - if (substr($inner, 0, 4) === 'new ') { + if (str_starts_with($inner, 'new ')) { $inner = "($inner)"; } return $this->formatPhp("$inner->?(...?)", [$entity[1], $arguments]); diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index 8b49d3dd6..b7c6b8559 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -259,7 +259,7 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo case $entity[0] instanceof Reference: if ($entity[1][0] === '$') { // property getter, setter or appender Validators::assert($arguments, 'list:0..1', "setup arguments for '" . Nette\Utils\Callback::toString($entity) . "'"); - if (!$arguments && substr($entity[1], -2) === '[]') { + if (!$arguments && str_ends_with($entity[1], '[]')) { throw new ServiceCreationException(sprintf('Missing argument for %s.', $entity[1])); } } elseif ( @@ -287,7 +287,7 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo $arguments = $this->completeArguments($arguments); } catch (ServiceCreationException $e) { - if (!strpos($e->getMessage(), "\nRelated to")) { + if (!str_contains($e->getMessage(), "\nRelated to")) { if (is_string($entity)) { $desc = $entity . '::__construct()'; } else { @@ -432,7 +432,7 @@ public function addDependency(\ReflectionClass|\ReflectionFunctionAbstract|strin private function completeException(\Exception $e, Definition $def): ServiceCreationException { $message = $e->getMessage(); - if ($e instanceof ServiceCreationException && Strings::startsWith($message, '[Service ')) { + if ($e instanceof ServiceCreationException && str_starts_with($message, '[Service ')) { return $e; } @@ -460,7 +460,7 @@ private function convertReferences(array $arguments): array $val = new Statement([new Reference($pair[0]), '$' . $pair[1]]); } - } elseif (is_string($val) && substr($val, 0, 2) === '@@') { // escaped text @@ + } elseif (is_string($val) && str_starts_with($val, '@@')) { // escaped text @@ $val = substr($val, 1); } });