Skip to content

Commit

Permalink
used native PHP 8 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 31, 2021
1 parent 7e4dfd3 commit 8fe5b6b
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/DI/Autowiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/DI/CompilerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
4 changes: 2 additions & 2 deletions src/DI/Config/Adapters/NeonAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Definitions/AccessorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Definitions/LocatorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/DI/Definitions/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, '\\');
}


Expand Down
4 changes: 2 additions & 2 deletions src/DI/Definitions/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/DI/DependencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
4 changes: 2 additions & 2 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Extensions/SearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Extensions/ServicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/DI/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
Expand Down
8 changes: 4 additions & 4 deletions src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
});
Expand Down

0 comments on commit 8fe5b6b

Please sign in to comment.