|
6 | 6 | use PhpParser\Node\Expr\MethodCall; |
7 | 7 | use PHPStan\Analyser\Scope; |
8 | 8 | use PHPStan\Rules\Rule; |
9 | | -use PHPStan\Type\Constant\ConstantStringType; |
10 | 9 | use PHPStan\Type\Generic\GenericObjectType; |
11 | 10 | use PHPStan\Type\IntersectionType; |
12 | 11 | use PHPStan\Type\ObjectType; |
@@ -44,53 +43,56 @@ public function processNode(Node $node, Scope $scope): array |
44 | 43 | } |
45 | 44 |
|
46 | 45 | $argType = $scope->getType($node->getArgs()[0]->value); |
47 | | - if (!($argType instanceof ConstantStringType)) { |
| 46 | + if (count($argType->getConstantStrings()) === 0) { |
48 | 47 | return []; |
49 | 48 | } |
50 | 49 |
|
51 | | - $method = $argType->getValue(); |
52 | | - $type = $scope->getType($node->var); |
53 | | - |
54 | | - if ( |
55 | | - $type instanceof IntersectionType |
56 | | - && ( |
57 | | - in_array(MockObject::class, $type->getReferencedClasses(), true) |
58 | | - || in_array(Stub::class, $type->getReferencedClasses(), true) |
59 | | - ) |
60 | | - && !$type->hasMethod($method)->yes() |
61 | | - ) { |
62 | | - $mockClass = array_filter($type->getReferencedClasses(), static function (string $class): bool { |
63 | | - return $class !== MockObject::class && $class !== Stub::class; |
64 | | - }); |
65 | | - |
66 | | - return [ |
67 | | - sprintf( |
| 50 | + $errors = []; |
| 51 | + foreach ($argType->getConstantStrings() as $constantString) { |
| 52 | + $method = $constantString->getValue(); |
| 53 | + $type = $scope->getType($node->var); |
| 54 | + |
| 55 | + if ( |
| 56 | + $type instanceof IntersectionType |
| 57 | + && ( |
| 58 | + in_array(MockObject::class, $type->getObjectClassNames(), true) |
| 59 | + || in_array(Stub::class, $type->getObjectClassNames(), true) |
| 60 | + ) |
| 61 | + && !$type->hasMethod($method)->yes() |
| 62 | + ) { |
| 63 | + $mockClass = array_filter($type->getObjectClassNames(), static function (string $class): bool { |
| 64 | + return $class !== MockObject::class && $class !== Stub::class; |
| 65 | + }); |
| 66 | + |
| 67 | + $errors[] = sprintf( |
68 | 68 | 'Trying to mock an undefined method %s() on class %s.', |
69 | 69 | $method, |
70 | 70 | implode('&', $mockClass) |
71 | | - ), |
72 | | - ]; |
73 | | - } |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + if ( |
| 75 | + !($type instanceof GenericObjectType) |
| 76 | + || $type->getClassName() !== InvocationMocker::class |
| 77 | + || count($type->getTypes()) <= 0 |
| 78 | + ) { |
| 79 | + continue; |
| 80 | + } |
74 | 81 |
|
75 | | - if ( |
76 | | - $type instanceof GenericObjectType |
77 | | - && $type->getClassName() === InvocationMocker::class |
78 | | - && count($type->getTypes()) > 0 |
79 | | - ) { |
80 | 82 | $mockClass = $type->getTypes()[0]; |
81 | 83 |
|
82 | | - if ($mockClass instanceof ObjectType && !$mockClass->hasMethod($method)->yes()) { |
83 | | - return [ |
84 | | - sprintf( |
85 | | - 'Trying to mock an undefined method %s() on class %s.', |
86 | | - $method, |
87 | | - $mockClass->getClassName() |
88 | | - ), |
89 | | - ]; |
| 84 | + if (!($mockClass instanceof ObjectType) || $mockClass->hasMethod($method)->yes()) { |
| 85 | + continue; |
90 | 86 | } |
| 87 | + |
| 88 | + $errors[] = sprintf( |
| 89 | + 'Trying to mock an undefined method %s() on class %s.', |
| 90 | + $method, |
| 91 | + $mockClass->getClassName() |
| 92 | + ); |
91 | 93 | } |
92 | 94 |
|
93 | | - return []; |
| 95 | + return $errors; |
94 | 96 | } |
95 | 97 |
|
96 | 98 | } |
0 commit comments