diff --git a/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/by_value_arg.php.inc b/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/by_value_arg.php.inc new file mode 100644 index 00000000000..093f182bc8a --- /dev/null +++ b/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/by_value_arg.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/skip_by_ref.php.inc b/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/skip_by_ref.php.inc new file mode 100644 index 00000000000..118caabbbc7 --- /dev/null +++ b/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/skip_by_ref.php.inc @@ -0,0 +1,29 @@ +modify($items[array_key_last($items)]); + self::modifyStatic($items[array_key_first($items)]); + self::modifyStatic(value: $items[array_key_last($items)]); + array_push($items[array_key_first($items)], ...[1, 2]); + + foreach ($items[array_key_last($items)] as &$item) { + } + } + + private function modify(array &$value): void + { + } + + private static function modifyStatic(array &$value): void + { + } +} diff --git a/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php b/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php index 2ab3c35866b..3c97bdba6ad 100644 --- a/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php +++ b/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php @@ -207,6 +207,10 @@ private function shouldSkip(ArrayDimFetch $arrayDimFetch, Node $scopeNode): bool return true; } + if ($arrayDimFetch->getAttribute(AttributeKey::IS_BYREF_VAR) === true) { + return true; + } + return (bool) $arrayDimFetch->getAttribute(AttributeKey::IS_UNSET_VAR); } } diff --git a/src/PhpParser/NodeVisitor/ByRefNodeVisitor.php b/src/PhpParser/NodeVisitor/ByRefNodeVisitor.php index 03cca6f2328..5312bbc63dd 100644 --- a/src/PhpParser/NodeVisitor/ByRefNodeVisitor.php +++ b/src/PhpParser/NodeVisitor/ByRefNodeVisitor.php @@ -6,10 +6,12 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\AssignRef; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; +use PhpParser\Node\Stmt\Foreach_; use PhpParser\NodeVisitorAbstract; use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -46,6 +48,12 @@ public function enterNode(Node $node): ?Node return null; } + // only array dim fetch, a by-ref variable is tracked per function-like below + if ($node instanceof Foreach_ && $node->byRef && $node->expr instanceof ArrayDimFetch) { + $node->expr->setAttribute(AttributeKey::IS_BYREF_VAR, true); + return null; + } + if ($node instanceof Variable) { $this->decorateByRefVariable($node); return null; diff --git a/src/PhpParser/NodeVisitor/CallLikeReflectionNodeVisitor.php b/src/PhpParser/NodeVisitor/CallLikeReflectionNodeVisitor.php index a332086e418..b2ffe7200b9 100644 --- a/src/PhpParser/NodeVisitor/CallLikeReflectionNodeVisitor.php +++ b/src/PhpParser/NodeVisitor/CallLikeReflectionNodeVisitor.php @@ -8,9 +8,11 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; +use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Closure; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Identifier; use PhpParser\NodeVisitorAbstract; use PHPStan\Reflection\FunctionReflection; @@ -31,6 +33,7 @@ * - Closure/arrow-fn args passed to a variadic callable parameter (HAS_CLOSURE_WITH_VARIADIC_ARGS) * - 2-item array args passed to a parameter that cannot hold a Closure, so an array callable is kept as is * (IS_ARG_NOT_ACCEPTING_CLOSURE), see https://github.com/rectorphp/rector/issues/9563 + * - array dim fetch on a func call (e.g. $items[array_key_last($items)]) passed to a by-ref parameter (IS_BYREF_VAR) */ final class CallLikeReflectionNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface { @@ -54,8 +57,8 @@ public function enterNode(Node $node): ?Node return null; } - // only closure/arrow-fn or array callable shaped args are decorated below; skip the reflection otherwise - if (! $this->hasClosureArrowOrArrayCallableArg($args)) { + // only closure/arrow-fn, array callable or func call dim fetch shaped args are decorated below; skip the reflection otherwise + if (! $this->hasClosureArrowOrArrayCallableArg($args) && ! $this->hasFuncCallArrayDimFetchArg($args)) { return null; } @@ -63,6 +66,7 @@ public function enterNode(Node $node): ?Node $this->decorateClosureWithVariadicArgs($args, $functionLikeReflection); $this->decorateArgNotAcceptingClosure($node, $args, $functionLikeReflection); + $this->decorateByRefArrayDimFetchArg($node, $args, $functionLikeReflection); return null; } @@ -80,6 +84,19 @@ private function hasClosureArrowOrArrayCallableArg(array $args): bool ); } + /** + * @param Arg[] $args + */ + private function hasFuncCallArrayDimFetchArg(array $args): bool + { + return array_any($args, fn (Arg $arg): bool => $this->isFuncCallArrayDimFetchShape($arg->value)); + } + + private function isFuncCallArrayDimFetchShape(Expr $expr): bool + { + return $expr instanceof ArrayDimFetch && ($expr->dim instanceof FuncCall || $expr->var instanceof FuncCall); + } + /** * Only [$caller, 'method'] can be an array callable */ @@ -160,6 +177,46 @@ private function decorateArgNotAcceptingClosure( } } + /** + * @param Arg[] $args + */ + private function decorateByRefArrayDimFetchArg( + CallLike $callLike, + array $args, + MethodReflection|FunctionReflection|null $functionLikeReflection + ): void { + if (! $functionLikeReflection instanceof FunctionReflection && ! $functionLikeReflection instanceof MethodReflection) { + return; + } + + if (! $this->hasFuncCallArrayDimFetchArg($args)) { + return; + } + + $parameterReflections = ParametersAcceptorSelectorVariantsWrapper::select( + $functionLikeReflection, + $callLike, + ScopeFetcher::fetch($callLike) + )->getParameters(); + + foreach ($args as $position => $arg) { + if (! $this->isFuncCallArrayDimFetchShape($arg->value)) { + continue; + } + + $parameterReflection = $this->matchParameterReflection($arg, $position, $parameterReflections); + if (! $parameterReflection instanceof ParameterReflection) { + continue; + } + + if (! $parameterReflection->passedByReference()->yes()) { + continue; + } + + $arg->value->setAttribute(AttributeKey::IS_BYREF_VAR, true); + } + } + /** * @param ParameterReflection[] $parameterReflections */