Skip to content

Commit d544782

Browse files
committed
Fix ReflectionClassIsSubclassOfTypeSpecifyingExtension
1 parent 4f2af3b commit d544782

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

src/Type/Php/ReflectionClassIsSubclassOfTypeSpecifyingExtension.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Type\Generic\GenericObjectType;
1313
use PHPStan\Type\MethodTypeSpecifyingExtension;
1414
use PHPStan\Type\ObjectWithoutClassType;
15+
use PHPStan\Type\TypeCombinator;
1516
use ReflectionClass;
1617

1718
final class ReflectionClassIsSubclassOfTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
@@ -46,20 +47,11 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod
4647

4748
$valueType = $scope->getType($node->getArgs()[0]->value);
4849
$objectType = $valueType->getClassStringObjectType();
49-
$narrowingType = new GenericObjectType(ReflectionClass::class, [$objectType]);
5050

51-
if (!$reflectionType->isSuperTypeOf($objectType)->yes()) {
52-
// cause "always false" error
53-
return $this->typeSpecifier->create(
54-
$node->var,
55-
$narrowingType,
56-
$context,
57-
$scope,
58-
);
59-
}
51+
$intersected = TypeCombinator::intersect($reflectionType, $objectType);
52+
$narrowingType = new GenericObjectType(ReflectionClass::class, [$intersected]);
6053

61-
if ($objectType->isSuperTypeOf($reflectionType)->yes()) {
62-
// cause "always true" error
54+
if ($reflectionType->isSuperTypeOf($objectType)->no()) {
6355
return $this->typeSpecifier->create(
6456
$node->var,
6557
$narrowingType,

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,21 @@ public function testBug12473(): void
259259
$this->treatPhpDocTypesAsCertain = true;
260260
$tip = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
261261
$this->analyse([__DIR__ . '/data/bug-12473.php'], [
262-
[
262+
/*[
263263
'Call to method ReflectionClass<Bug12473\\Picture>::isSubclassOf() with \'Bug12473\\\\Picture\' will always evaluate to true.',
264264
39,
265265
$tip,
266-
],
266+
],*/
267267
[
268268
'Call to method ReflectionClass<Bug12473\\PictureUser>::isSubclassOf() with \'Bug12473\\\\PictureProduct\' will always evaluate to false.',
269269
49,
270270
$tip,
271271
],
272-
[
272+
/*[
273273
'Call to method ReflectionClass<Bug12473\\PictureUser>::isSubclassOf() with \'Bug12473\\\\PictureUser\' will always evaluate to true.',
274274
59,
275275
$tip,
276-
],
276+
],*/
277277
]);
278278
}
279279

tests/PHPStan/Rules/Comparison/data/bug-12473.php

+11
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,14 @@ function doFoo3(string $a): void {
6060

6161
}
6262
}
63+
64+
/**
65+
* @param ReflectionClass<object> $a
66+
* @param class-string<object> $b
67+
* @return void
68+
*/
69+
function doFoo4(ReflectionClass $a, string $b): void {
70+
if ($a->isSubclassOf($b)) {
71+
72+
}
73+
};

0 commit comments

Comments
 (0)