diff --git a/rules-tests/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector/Fixture/different_named_arg.php.inc b/rules-tests/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector/Fixture/different_named_arg.php.inc new file mode 100644 index 00000000000..2b1fb39b51e --- /dev/null +++ b/rules-tests/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector/Fixture/different_named_arg.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php index 35422b5565b..81380fdbef7 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php @@ -5,7 +5,6 @@ namespace Rector\DeadCode\Rector\ClassMethod; use PhpParser\Node; -use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; diff --git a/rules/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector.php b/rules/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector.php index db3b77d0e91..766bbf06fbf 100644 --- a/rules/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector.php +++ b/rules/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector.php @@ -5,9 +5,7 @@ namespace Rector\Php81\Rector\FuncCall; use PhpParser\Node; -use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionReflection; @@ -88,13 +86,7 @@ public function refactor(Node $node): ?Node } $args = $node->getArgs(); - $position = $this->argsAnalyzer->hasNamedArg($args) - ? $this->resolveNamedPosition($args) - : 2; - - if ($position === null) { - return null; - } + $position = $this->argsAnalyzer->resolveArgPosition($args, 'limit', 2); if (! isset($args[$position])) { return null; @@ -131,26 +123,6 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_STRING_FUNCTION; } - /** - * @param Arg[] $args - */ - private function resolveNamedPosition(array $args): ?int - { - foreach ($args as $position => $arg) { - if (! $arg->name instanceof Identifier) { - continue; - } - - if (! $this->isName($arg->name, 'limit')) { - continue; - } - - return $position; - } - - return null; - } - private function shouldSkip(FuncCall $funcCall): bool { if (! $this->isName($funcCall, 'preg_split')) { diff --git a/rules/Php85/Rector/Class_/SleepToSerializeRector.php b/rules/Php85/Rector/Class_/SleepToSerializeRector.php index a389d570d96..c70198b7f1d 100644 --- a/rules/Php85/Rector/Class_/SleepToSerializeRector.php +++ b/rules/Php85/Rector/Class_/SleepToSerializeRector.php @@ -4,6 +4,8 @@ namespace Rector\Php85\Rector\Class_; +use PhpParser\Node\Scalar\String_; +use PhpParser\Node\Expr\Variable; use PhpParser\Node; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayItem; @@ -28,8 +30,8 @@ final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( - private BetterNodeFinder $betterNodeFinder, - private ReturnAnalyzer $returnAnalyzer + private readonly BetterNodeFinder $betterNodeFinder, + private readonly ReturnAnalyzer $returnAnalyzer ) { } @@ -110,18 +112,19 @@ public function refactor(Node $node): ?Node return null; } - if (count($return->expr->items) > 0) { + if ($return->expr->items !== []) { $newItems = []; foreach ($return->expr->items as $item) { - if ($item !== null && $item->value instanceof Node\Scalar\String_) { + if ($item !== null && $item->value instanceof String_) { $propName = $item->value->value; $newItems[] = new ArrayItem( - new PropertyFetch(new Node\Expr\Variable('this'), $propName), + new PropertyFetch(new Variable('this'), $propName), $item->value ); } } - if (count($newItems) > 0) { + + if ($newItems !== []) { $hasChanged = true; $return->expr->items = $newItems; } diff --git a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php index 9f2f649ead6..9618aa6afed 100644 --- a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php +++ b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php @@ -5,12 +5,11 @@ namespace Rector\Php85\Rector\FuncCall; use PhpParser\Node; -use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionReflection; +use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; use Rector\Php81\NodeManipulator\NullToStrictStringIntConverter; @@ -29,7 +28,8 @@ final class ArrayKeyExistsNullToEmptyStringRector extends AbstractRector impleme { public function __construct( private readonly ReflectionResolver $reflectionResolver, - private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter + private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter, + private readonly ArgsAnalyzer $argsAnalyzer ) { } @@ -93,7 +93,7 @@ public function refactor(Node $node): ?Node $result = $this->nullToStrictStringIntConverter->convertIfNull( $node, $args, - $this->resolvePosition($args), + $this->argsAnalyzer->resolveArgPosition($args, 'key', 0), $isTrait, $scope, $parametersAcceptor @@ -109,18 +109,4 @@ public function provideMinPhpVersion(): int { return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION; } - - /** - * @param Arg[] $args - */ - private function resolvePosition(array $args): int - { - foreach ($args as $position => $arg) { - if ($arg->name instanceof Identifier && $arg->name->toString() === 'key') { - return $position; - } - } - - return 0; - } } diff --git a/src/NodeAnalyzer/ArgsAnalyzer.php b/src/NodeAnalyzer/ArgsAnalyzer.php index e961df92f9d..504900b95ba 100644 --- a/src/NodeAnalyzer/ArgsAnalyzer.php +++ b/src/NodeAnalyzer/ArgsAnalyzer.php @@ -6,9 +6,15 @@ use PhpParser\Node\Arg; use PhpParser\Node\Identifier; +use Rector\NodeNameResolver\NodeNameResolver; -final class ArgsAnalyzer +final readonly class ArgsAnalyzer { + public function __construct( + private NodeNameResolver $nodeNameResolver + ) { + } + /** * @param Arg[] $args */ @@ -22,4 +28,24 @@ public function hasNamedArg(array $args): bool return false; } + + /** + * @param Arg[] $args + */ + public function resolveArgPosition(array $args, string $name, int $defaultPosition): int + { + foreach ($args as $position => $arg) { + if (! $arg->name instanceof Identifier) { + continue; + } + + if (! $this->nodeNameResolver->isName($arg->name, $name)) { + continue; + } + + return $position; + } + + return $defaultPosition; + } }