Skip to content

Commit 3d34d5f

Browse files
staabmphpstan-bot
authored andcommitted
Fix static method call on non-generic class-string returning ErrorType
- Static method calls on non-generic class-string (e.g. $class::foo() where $class is class-string) now return mixed instead of *ERROR* - The root cause was that ClassStringType::getObjectTypeOrClassStringObjectType() returns ObjectWithoutClassType, which has hasMethod() returning maybe, causing filterTypeWithMethod() to return null - Added check in StaticCallHandler: when the resolved type is a classless object type (from class-string), return MixedType instead of ErrorType - New regression test in tests/PHPStan/Analyser/nsrt/bug-9844.php
1 parent 47f36d8 commit 3d34d5f

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/Analyser/ExprHandler/StaticCallHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,17 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type
335335
$staticMethodCalledOnType = TypeCombinator::removeNull($scope->getType($expr->class))->getObjectTypeOrClassStringObjectType();
336336
}
337337

338+
$methodName = $expr->name->toString();
338339
$callType = $this->methodCallReturnTypeHelper->methodCallReturnType(
339340
$scope,
340341
$staticMethodCalledOnType,
341-
$expr->name->toString(),
342+
$methodName,
342343
$expr,
343344
);
344345
if ($callType === null) {
345-
$callType = new ErrorType();
346+
$callType = $staticMethodCalledOnType->isObject()->yes() && $staticMethodCalledOnType->getObjectClassNames() === []
347+
? new MixedType()
348+
: new ErrorType();
346349
}
347350

348351
if ($expr->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)