99use PhpParser \Node \Expr \MethodCall ;
1010use PhpParser \Node \Expr \PropertyFetch ;
1111use PhpParser \Node \Expr \Variable ;
12+ use PhpParser \Node \Identifier ;
1213use PhpParser \Node \Stmt \Class_ ;
1314use PhpParser \Node \Stmt \ClassMethod ;
1415use PHPStan \Reflection \MethodReflection ;
@@ -90,7 +91,7 @@ public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool
9091 }
9192
9293 // check if variable is passed as arg to a method that declares MockObject type parameter
93- foreach ($ methodCall ->getArgs () as $ position => $ arg ) {
94+ foreach ($ methodCall ->getArgs () as $ arg ) {
9495 if (! $ arg instanceof Arg) {
9596 continue ;
9697 }
@@ -108,15 +109,30 @@ public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool
108109 continue ;
109110 }
110111
111- $ parameters = $ methodReflection ->getVariants ()[0 ]
112- ->getParameters ();
113- if (! isset ($ parameters [$ position ])) {
112+ $ variants = $ methodReflection ->getVariants ();
113+ if (! isset ($ variants [0 ])) {
114114 continue ;
115115 }
116116
117- $ paramType = $ parameters [$ position ]->getType ();
118- if ($ mockObjectType ->isSuperTypeOf ($ paramType )->yes ()) {
119- return true ;
117+ $ parameters = $ variants [0 ]->getParameters ();
118+
119+ foreach ($ parameters as $ index => $ parameterReflection ) {
120+ $ paramType = $ parameters [$ index ]->getType ();
121+ if ($ arg ->name instanceof Identifier
122+ && $ this ->nodeNameResolver ->isName ($ arg ->name , $ parameterReflection ->getName ())
123+ && $ mockObjectType ->isSuperTypeOf ($ paramType )
124+ ->yes ()) {
125+ return true ;
126+ }
127+
128+ if (! isset ($ parameters [$ index ])) {
129+ continue ;
130+ }
131+
132+ if ($ mockObjectType ->isSuperTypeOf ($ paramType )->yes ()) {
133+ return true ;
134+ }
135+
120136 }
121137 }
122138 }
0 commit comments