77use PhpParser \Node \Arg ;
88use PhpParser \Node \Expr ;
99use PhpParser \Node \Expr \Array_ ;
10+ use PhpParser \Node \Expr \ArrayDimFetch ;
1011use PhpParser \Node \Expr \ArrowFunction ;
1112use PhpParser \Node \Expr \CallLike ;
1213use PhpParser \Node \Expr \Closure ;
14+ use PhpParser \Node \Expr \FuncCall ;
1315use PhpParser \Node \Identifier ;
1416use PhpParser \NodeVisitorAbstract ;
1517use PHPStan \Reflection \FunctionReflection ;
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 */
3336final 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