Skip to content

Commit 1c2f18d

Browse files
committed
Updated Rector to commit d4ff3fcf3b9583cda388dd49e5587a2916ac323b
rectorphp/rector-src@d4ff3fc [Php85] Skip by-ref arg and by-ref foreach on ArrayFirstLastRector (#9080)
1 parent b5a6db1 commit 1c2f18d

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ private function shouldSkip(ArrayDimFetch $arrayDimFetch, Node $scopeNode): bool
164164
if ($arrayDimFetch->getAttribute(AttributeKey::IS_INCREMENT_OR_DECREMENT) === \true) {
165165
return \true;
166166
}
167+
if ($arrayDimFetch->getAttribute(AttributeKey::IS_BYREF_VAR) === \true) {
168+
return \true;
169+
}
167170
return (bool) $arrayDimFetch->getAttribute(AttributeKey::IS_UNSET_VAR);
168171
}
169172
}

‎src/Application/VersionResolver.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '2dad902c5232e23cadfb517d652190ba0ebf3c33';
22+
public const PACKAGE_VERSION = 'd4ff3fcf3b9583cda388dd49e5587a2916ac323b';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-11-27 16:14:47';
27+
public const RELEASE_DATE = '2026-11-27 20:34:48';
2828
/**
2929
* @var int
3030
*/

‎src/PhpParser/NodeVisitor/ByRefNodeVisitor.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
use PhpParser\Node;
77
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\ArrayDimFetch;
89
use PhpParser\Node\Expr\AssignRef;
910
use PhpParser\Node\Expr\Closure;
1011
use PhpParser\Node\Expr\Variable;
1112
use PhpParser\Node\FunctionLike;
13+
use PhpParser\Node\Stmt\Foreach_;
1214
use PhpParser\NodeVisitorAbstract;
1315
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
1416
use Rector\NodeTypeResolver\Node\AttributeKey;
@@ -41,6 +43,11 @@ public function enterNode(Node $node): ?Node
4143
$node->expr->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
4244
return null;
4345
}
46+
// only array dim fetch, a by-ref variable is tracked per function-like below
47+
if ($node instanceof Foreach_ && $node->byRef && $node->expr instanceof ArrayDimFetch) {
48+
$node->expr->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
49+
return null;
50+
}
4451
if ($node instanceof Variable) {
4552
$this->decorateByRefVariable($node);
4653
return null;

‎src/PhpParser/NodeVisitor/CallLikeReflectionNodeVisitor.php‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
use PhpParser\Node\Arg;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\Array_;
10+
use PhpParser\Node\Expr\ArrayDimFetch;
1011
use PhpParser\Node\Expr\ArrowFunction;
1112
use PhpParser\Node\Expr\CallLike;
1213
use PhpParser\Node\Expr\Closure;
14+
use PhpParser\Node\Expr\FuncCall;
1315
use PhpParser\Node\Identifier;
1416
use PhpParser\NodeVisitorAbstract;
1517
use PHPStan\Reflection\FunctionReflection;
@@ -29,6 +31,7 @@
2931
* - Closure/arrow-fn args passed to a variadic callable parameter (HAS_CLOSURE_WITH_VARIADIC_ARGS)
3032
* - 2-item array args passed to a parameter that cannot hold a Closure, so an array callable is kept as is
3133
* (IS_ARG_NOT_ACCEPTING_CLOSURE), see https://github.com/rectorphp/rector/issues/9563
34+
* - array dim fetch on a func call (e.g. $items[array_key_last($items)]) passed to a by-ref parameter (IS_BYREF_VAR)
3235
*/
3336
final class CallLikeReflectionNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
3437
{
@@ -52,13 +55,14 @@ public function enterNode(Node $node): ?Node
5255
if ($args === []) {
5356
return null;
5457
}
55-
// only closure/arrow-fn or array callable shaped args are decorated below; skip the reflection otherwise
56-
if (!$this->hasClosureArrowOrArrayCallableArg($args)) {
58+
// only closure/arrow-fn, array callable or func call dim fetch shaped args are decorated below; skip the reflection otherwise
59+
if (!$this->hasClosureArrowOrArrayCallableArg($args) && !$this->hasFuncCallArrayDimFetchArg($args)) {
5760
return null;
5861
}
5962
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
6063
$this->decorateClosureWithVariadicArgs($args, $functionLikeReflection);
6164
$this->decorateArgNotAcceptingClosure($node, $args, $functionLikeReflection);
65+
$this->decorateByRefArrayDimFetchArg($node, $args, $functionLikeReflection);
6266
return null;
6367
}
6468
/**
@@ -75,6 +79,24 @@ private function hasClosureArrowOrArrayCallableArg(array $args): bool
7579
}
7680
return $found;
7781
}
82+
/**
83+
* @param Arg[] $args
84+
*/
85+
private function hasFuncCallArrayDimFetchArg(array $args): bool
86+
{
87+
$found = \false;
88+
foreach ($args as $arg) {
89+
if ($this->isFuncCallArrayDimFetchShape($arg->value)) {
90+
$found = \true;
91+
break;
92+
}
93+
}
94+
return $found;
95+
}
96+
private function isFuncCallArrayDimFetchShape(Expr $expr): bool
97+
{
98+
return $expr instanceof ArrayDimFetch && ($expr->dim instanceof FuncCall || $expr->var instanceof FuncCall);
99+
}
78100
/**
79101
* Only [$caller, 'method'] can be an array callable
80102
*/
@@ -132,6 +154,33 @@ private function decorateArgNotAcceptingClosure(CallLike $callLike, array $args,
132154
$arg->value->setAttribute(AttributeKey::IS_ARG_NOT_ACCEPTING_CLOSURE, \true);
133155
}
134156
}
157+
/**
158+
* @param Arg[] $args
159+
* @param \PHPStan\Reflection\FunctionReflection|\PHPStan\Reflection\MethodReflection|null $functionLikeReflection
160+
*/
161+
private function decorateByRefArrayDimFetchArg(CallLike $callLike, array $args, $functionLikeReflection): void
162+
{
163+
if (!$functionLikeReflection instanceof FunctionReflection && !$functionLikeReflection instanceof MethodReflection) {
164+
return;
165+
}
166+
if (!$this->hasFuncCallArrayDimFetchArg($args)) {
167+
return;
168+
}
169+
$parameterReflections = ParametersAcceptorSelectorVariantsWrapper::select($functionLikeReflection, $callLike, ScopeFetcher::fetch($callLike))->getParameters();
170+
foreach ($args as $position => $arg) {
171+
if (!$this->isFuncCallArrayDimFetchShape($arg->value)) {
172+
continue;
173+
}
174+
$parameterReflection = $this->matchParameterReflection($arg, $position, $parameterReflections);
175+
if (!$parameterReflection instanceof ParameterReflection) {
176+
continue;
177+
}
178+
if (!$parameterReflection->passedByReference()->yes()) {
179+
continue;
180+
}
181+
$arg->value->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
182+
}
183+
}
135184
/**
136185
* @param ParameterReflection[] $parameterReflections
137186
*/

0 commit comments

Comments
 (0)