Skip to content

Commit 1805bf2

Browse files
committed
fix
1 parent e11f9cf commit 1805bf2

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\PropertyFetch;
1111
use PhpParser\Node\Expr\Variable;
12+
use PhpParser\Node\Identifier;
1213
use PhpParser\Node\Stmt\Class_;
1314
use PhpParser\Node\Stmt\ClassMethod;
1415
use 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

Comments
 (0)