Skip to content

Commit 75f87d4

Browse files
committed
Fix build
1 parent 87516ff commit 75f87d4

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

Diff for: src/PhpDoc/PHPUnit/MockObjectTypeNodeResolverExtension.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use PHPStan\Type\NeverType;
1212
use PHPStan\Type\Type;
1313
use PHPStan\Type\TypeCombinator;
14-
use PHPStan\Type\TypeWithClassName;
1514
use function array_key_exists;
15+
use function count;
1616

1717
class MockObjectTypeNodeResolverExtension implements TypeNodeResolverExtension, TypeNodeResolverAwareExtension
1818
{
@@ -44,11 +44,12 @@ public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type
4444

4545
$types = $this->typeNodeResolver->resolveMultiple($typeNode->types, $nameScope);
4646
foreach ($types as $type) {
47-
if (!$type instanceof TypeWithClassName) {
47+
$classNames = $type->getObjectClassNames();
48+
if (count($classNames) !== 1) {
4849
continue;
4950
}
5051

51-
if (array_key_exists($type->getClassName(), $mockClassNames)) {
52+
if (array_key_exists($classNames[0], $mockClassNames)) {
5253
$resultType = TypeCombinator::intersect(...$types);
5354
if ($resultType instanceof NeverType) {
5455
continue;

Diff for: src/Rules/PHPUnit/MockMethodCallRule.php

+38-36
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node\Expr\MethodCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9-
use PHPStan\Type\Constant\ConstantStringType;
109
use PHPStan\Type\Generic\GenericObjectType;
1110
use PHPStan\Type\IntersectionType;
1211
use PHPStan\Type\ObjectType;
@@ -44,53 +43,56 @@ public function processNode(Node $node, Scope $scope): array
4443
}
4544

4645
$argType = $scope->getType($node->getArgs()[0]->value);
47-
if (!($argType instanceof ConstantStringType)) {
46+
if (count($argType->getConstantStrings()) === 0) {
4847
return [];
4948
}
5049

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(
6868
'Trying to mock an undefined method %s() on class %s.',
6969
$method,
7070
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+
}
7481

75-
if (
76-
$type instanceof GenericObjectType
77-
&& $type->getClassName() === InvocationMocker::class
78-
&& count($type->getTypes()) > 0
79-
) {
8082
$mockClass = $type->getTypes()[0];
8183

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;
9086
}
87+
88+
$errors[] = sprintf(
89+
'Trying to mock an undefined method %s() on class %s.',
90+
$method,
91+
$mockClass->getClassName()
92+
);
9193
}
9294

93-
return [];
95+
return $errors;
9496
}
9597

9698
}

Diff for: src/Type/PHPUnit/MockObjectDynamicReturnTypeExtension.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Type\IntersectionType;
1111
use PHPStan\Type\ObjectType;
1212
use PHPStan\Type\Type;
13-
use PHPStan\Type\TypeWithClassName;
1413
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
1514
use PHPUnit\Framework\MockObject\MockObject;
1615
use function array_filter;
@@ -38,7 +37,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3837
}
3938

4039
$mockClasses = array_values(array_filter($type->getTypes(), static function (Type $type): bool {
41-
return !$type instanceof TypeWithClassName || $type->getClassName() !== MockObject::class;
40+
$classNames = $type->getObjectClassNames();
41+
if (count($classNames) !== 1) {
42+
return true;
43+
}
44+
45+
return $classNames[0] !== MockObject::class;
4246
}));
4347

4448
if (count($mockClasses) !== 1) {

0 commit comments

Comments
 (0)