Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -200,11 +202,14 @@ public function check(
if (!$classType->isObject()->yes()) {
return [[], null];
}
} elseif ($classType instanceof ClassStringType) { // @phpstan-ignore phpstanApi.instanceofType
$typeForDescribe = $classType;
$classType = new ObjectWithoutClassType();
} elseif ($classType->isString()->yes()) {
return [[], null];
}

$typeForDescribe = $classType;
$typeForDescribe ??= $classType;
if ($classType instanceof StaticType) {
$typeForDescribe = $classType->getStaticObjectType();
}
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9844.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace Bug9844;

use function PHPStan\Testing\assertType;

class HelloWorld
{

/**
* @param class-string $class
*/
public function sayHello(string $class): void
{
assertType('*ERROR*', $class::foo());
}
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ public function testCallStaticMethods(): void
'Static call to instance method CallStaticMethods\InterfaceWithStaticMethod::doInstanceFoo().',
213,
],
[
'Call to an undefined static method class-string::nonexistentMethod().',
295,
],
[
'Static method CallStaticMethods\Foo::test() invoked with 3 parameters, 0 required.',
298,
Expand Down Expand Up @@ -1009,4 +1013,21 @@ public function testPipeOperator(): void
]);
}

public function testBug9844(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = false;
$this->checkImplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-9844.php'], [
[
'Call to an undefined static method class-string::foo().',
13,
],
[
'Call to an undefined static method object::foo().',
21,
],
]);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9844.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Bug9844;

class HelloWorld
{

/**
* @param class-string $class
*/
public function sayHello(string $class): void
{
$class::foo();
}

/**
* @param object $class
*/
public function sayHello2(object $class): void
{
$class::foo();
}
}
Loading