Skip to content

Commit cf593f8

Browse files
Improve
1 parent 23b06c9 commit cf593f8

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/Type/Php/IsAFunctionTypeSpecifyingHelper.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ static function (Type $type, callable $traverse) use ($objectOrClassType, $objec
4747
}
4848
if ($type instanceof ConstantStringType) {
4949
if (!$allowSameClass) {
50-
// For objectType we cannot be sure since 'Foo' is used for both
51-
// - the Foo class
52-
// - a child of foo class
53-
if (
54-
$objectOrClassTypeClassNames === [$type->getValue()]
55-
&& $objectOrClassType->isString()->yes()
56-
) {
57-
return new NeverType();
50+
if ($objectOrClassTypeClassNames === [$type->getValue()]) {
51+
$isSameClass = true;
52+
foreach ($objectOrClassType->getObjectClassReflections() as $classReflection) {
53+
if (!$classReflection->isFinal()) {
54+
$isSameClass = false;
55+
break;
56+
}
57+
}
58+
59+
if ($isSameClass) {
60+
return new NeverType();
61+
}
5862
}
5963

6064
if (

tests/PHPStan/Analyser/nsrt/bug-6305.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ class B extends A {}
1515
}
1616

1717
if (is_subclass_of($b, B::class)) {
18-
assertType('Bug6305Types\B', $b); // Could be NEVER
18+
assertType('*NEVER*', $b);
1919
}

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ public function testBug6305(): void
393393
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\A\' will always evaluate to true.',
394394
11,
395395
],
396-
// [
397-
// 'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\B\' will always evaluate to false.',
398-
// 14,
399-
// ],
396+
[
397+
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\B\' will always evaluate to false.',
398+
14,
399+
],
400400
]);
401401
}
402402

0 commit comments

Comments
 (0)