Skip to content

Commit db436df

Browse files
committed
Do not use instanceof *Type
1 parent c5ace3a commit db436df

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

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

+3-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use PhpParser\Node\Expr\MethodCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9-
use PHPStan\Type\Generic\GenericObjectType;
109
use PHPStan\Type\IntersectionType;
11-
use PHPStan\Type\ObjectType;
1210
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
1311
use PHPUnit\Framework\MockObject\MockObject;
1412
use PHPUnit\Framework\MockObject\Stub;
@@ -71,24 +69,15 @@ public function processNode(Node $node, Scope $scope): array
7169
);
7270
}
7371

74-
if (
75-
!($type instanceof GenericObjectType)
76-
|| $type->getClassName() !== InvocationMocker::class
77-
|| count($type->getTypes()) <= 0
78-
) {
79-
continue;
80-
}
81-
82-
$mockClass = $type->getTypes()[0];
83-
84-
if (!($mockClass instanceof ObjectType) || $mockClass->hasMethod($method)->yes()) {
72+
$mockedClassObject = $type->getTemplateType(InvocationMocker::class, 'TMockedClass');
73+
if ($mockedClassObject->hasMethod($method)->yes()) {
8574
continue;
8675
}
8776

8877
$errors[] = sprintf(
8978
'Trying to mock an undefined method %s() on class %s.',
9079
$method,
91-
$mockClass->getClassName()
80+
implode('|', $mockedClassObject->getObjectClassNames())
9281
);
9382
}
9483

Diff for: src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use PHPStan\Analyser\SpecifiedTypes;
1919
use PHPStan\Analyser\TypeSpecifier;
2020
use PHPStan\Analyser\TypeSpecifierContext;
21-
use PHPStan\Type\Constant\ConstantStringType;
2221
use ReflectionObject;
2322
use function array_key_exists;
2423
use function count;
@@ -125,14 +124,15 @@ private static function getExpressionResolvers(): array
125124
if (self::$resolvers === null) {
126125
self::$resolvers = [
127126
'InstanceOf' => static function (Scope $scope, Arg $class, Arg $object): ?Instanceof_ {
128-
$classType = $scope->getType($class->value);
129-
if (!$classType instanceof ConstantStringType) {
127+
$classType = $scope->getType($class->value)->getClassStringObjectType();
128+
$classNames = $classType->getObjectClassNames();
129+
if (count($classNames) !== 1) {
130130
return null;
131131
}
132132

133133
return new Instanceof_(
134134
$object->value,
135-
new Name($classType->getValue())
135+
new Name($classNames[0])
136136
);
137137
},
138138
'Same' => static function (Scope $scope, Arg $expected, Arg $actual): Identical {
@@ -205,12 +205,12 @@ private static function getExpressionResolvers(): array
205205
return new FuncCall(new Name('is_scalar'), [$actual]);
206206
},
207207
'InternalType' => static function (Scope $scope, Arg $type, Arg $value): ?FuncCall {
208-
$typeType = $scope->getType($type->value);
209-
if (!$typeType instanceof ConstantStringType) {
208+
$typeNames = $scope->getType($type->value)->getConstantStrings();
209+
if (count($typeNames) !== 1) {
210210
return null;
211211
}
212212

213-
switch ($typeType->getValue()) {
213+
switch ($typeNames[0]->getValue()) {
214214
case 'numeric':
215215
$functionName = 'is_numeric';
216216
break;

0 commit comments

Comments
 (0)