1919use PhpParser \Node \Expr \Instanceof_ ;
2020use PhpParser \Node \Expr \MethodCall ;
2121use PhpParser \Node \Expr \PropertyFetch ;
22+ use PhpParser \Node \Expr \StaticCall ;
2223use PhpParser \Node \Expr \StaticPropertyFetch ;
2324use PhpParser \Node \Expr \Variable ;
2425use PhpParser \Node \FunctionLike ;
3536use PHPStan \Type \UnionType ;
3637use Rector \NodeTypeResolver \PHPStan \Type \TypeFactory ;
3738use Rector \NodeTypeResolver \TypeComparator \TypeComparator ;
39+ use Rector \PhpParser \Node \BetterNodeFinder ;
3840use Rector \Rector \AbstractRector ;
3941use Rector \RuleDoc \CodeSample \CodeSample ;
4042use Rector \RuleDoc \RuleDefinition ;
@@ -48,7 +50,8 @@ final class StrictArrayParamDimFetchRector extends AbstractRector
4850 public function __construct (
4951 private readonly ParentClassMethodTypeOverrideGuard $ parentClassMethodTypeOverrideGuard ,
5052 private readonly TypeComparator $ typeComparator ,
51- private readonly TypeFactory $ typeFactory
53+ private readonly TypeFactory $ typeFactory ,
54+ private readonly BetterNodeFinder $ betterNodeFinder
5255 ) {
5356 }
5457
@@ -111,7 +114,7 @@ public function refactor(Node $node): ?Node
111114 return null ;
112115 }
113116
114- foreach ($ node ->getParams () as $ param ) {
117+ foreach ($ node ->getParams () as $ position => $ param ) {
115118 if ($ param ->type instanceof Node) {
116119 continue ;
117120 }
@@ -128,6 +131,15 @@ public function refactor(Node $node): ?Node
128131 continue ;
129132 }
130133
134+ // a private method is only reachable from inside the class, so all its callers are visible here;
135+ // skip if any of them passes an ArrayAccess object, which also supports dim fetch
136+ if ($ node instanceof ClassMethod && $ node ->isPrivate () && $ this ->isCalledWithArrayAccessArg (
137+ $ node ,
138+ $ position
139+ )) {
140+ continue ;
141+ }
142+
131143 $ param ->type = new Identifier ('array ' );
132144 $ hasChanged = true ;
133145 }
@@ -339,6 +351,51 @@ private function isMethodCall(string $paramName, ?Node $node): bool
339351 return false ;
340352 }
341353
354+ private function isCalledWithArrayAccessArg (ClassMethod $ classMethod , int $ position ): bool
355+ {
356+ $ class = $ this ->findClassOfMethod ($ classMethod );
357+ if (! $ class instanceof Class_) {
358+ return false ;
359+ }
360+
361+ $ methodName = $ this ->getName ($ classMethod );
362+
363+ $ calls = $ this ->betterNodeFinder ->findInstancesOf ($ class ->getMethods (), [MethodCall::class, StaticCall::class]);
364+
365+ return array_any (
366+ $ calls ,
367+ function (MethodCall |StaticCall $ call ) use ($ methodName , $ position ): bool {
368+ if ($ call ->isFirstClassCallable ()) {
369+ return false ;
370+ }
371+
372+ if (! $ this ->isName ($ call ->name , $ methodName )) {
373+ return false ;
374+ }
375+
376+ $ args = $ call ->getArgs ();
377+ if (! isset ($ args [$ position ])) {
378+ return false ;
379+ }
380+
381+ return $ this ->isArrayAccess ($ this ->getType ($ args [$ position ]->value ));
382+ }
383+ );
384+ }
385+
386+ private function findClassOfMethod (ClassMethod $ classMethod ): ?Class_
387+ {
388+ $ classes = $ this ->betterNodeFinder ->findInstanceOf ($ this ->getFile ()->getNewStmts (), Class_::class);
389+
390+ foreach ($ classes as $ class ) {
391+ if (in_array ($ classMethod , $ class ->getMethods (), true )) {
392+ return $ class ;
393+ }
394+ }
395+
396+ return null ;
397+ }
398+
342399 private function isArrayAccess (Type $ type ): bool
343400 {
344401 if (! $ type instanceof ObjectType) {
0 commit comments