Skip to content

Commit e8a33a8

Browse files
committed
Fix static method call on non-generic class-string returning ErrorType
- When calling a static method on a non-generic class-string (e.g. $class::foo() where $class is class-string), the result was ErrorType instead of MixedType - Root cause: class-string converts to ObjectWithoutClassType via getObjectTypeOrClassStringObjectType(), which returns maybe for hasMethod(), causing filterTypeWithMethod() to reject it and methodCallReturnType() to return null, falling back to ErrorType - Fix checks if the static call is on an expression (not a class name), the resolved type has no known class names, and hasMethod is not definitively no, returning MixedType in that case - New regression test in tests/PHPStan/Analyser/nsrt/bug-9844.php Closes phpstan/phpstan#9844
1 parent 106fc93 commit e8a33a8

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/Analyser/MutatingScope.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6390,7 +6390,15 @@ private function getStaticCallType(Expr\StaticCall $node): ?Type
63906390
$node,
63916391
);
63926392
if ($callType === null) {
6393-
$callType = new ErrorType();
6393+
if (
6394+
!$node->class instanceof Name
6395+
&& count($staticMethodCalledOnType->getObjectClassNames()) === 0
6396+
&& !$staticMethodCalledOnType->hasMethod($node->name->toString())->no()
6397+
) {
6398+
$callType = new MixedType();
6399+
} else {
6400+
$callType = new ErrorType();
6401+
}
63946402
}
63956403

63966404
if ($node->class instanceof Expr) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug9844;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
10+
/**
11+
* @param class-string $class
12+
*/
13+
public function sayHello(string $class): void
14+
{
15+
assertType('mixed', $class::foo());
16+
}
17+
}

0 commit comments

Comments
 (0)