Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

final class ByValueArg
{
public function run(array $items)
{
echo count($items[array_key_first($items)]);

foreach ($items[array_key_last($items)] as $item) {
}
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

final class ByValueArg
{
public function run(array $items)
{
echo count(array_first($items));

foreach (array_last($items) as $item) {
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

final class SkipByRef
{
public function run(array $items)
{
sort($items[array_key_first($items)]);
array_push($items[array_key_last($items)], 1);
$this->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
{
}
}
4 changes: 4 additions & 0 deletions rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions src/PhpParser/NodeVisitor/ByRefNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
61 changes: 59 additions & 2 deletions src/PhpParser/NodeVisitor/CallLikeReflectionNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -54,15 +57,16 @@ 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;
}

$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);

$this->decorateClosureWithVariadicArgs($args, $functionLikeReflection);
$this->decorateArgNotAcceptingClosure($node, $args, $functionLikeReflection);
$this->decorateByRefArrayDimFetchArg($node, $args, $functionLikeReflection);

return null;
}
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
Loading