diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 456c8d6874..404957fe57 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1802,7 +1802,7 @@ private function resolveType(string $exprString, Expr $node): Type if ($node->class instanceof Name) { $staticMethodCalledOnType = $this->resolveTypeByName($node->class); } else { - $staticMethodCalledOnType = $this->getType($node->class)->getObjectTypeOrClassStringObjectType(); + $staticMethodCalledOnType = TypeCombinator::removeNull($this->getType($node->class))->getObjectTypeOrClassStringObjectType(); } $returnType = $this->methodCallReturnType( @@ -1894,7 +1894,7 @@ private function resolveType(string $exprString, Expr $node): Type if ($node->class instanceof Name) { $staticPropertyFetchedOnType = $this->resolveTypeByName($node->class); } else { - $staticPropertyFetchedOnType = $this->getType($node->class)->getObjectTypeOrClassStringObjectType(); + $staticPropertyFetchedOnType = TypeCombinator::removeNull($this->getType($node->class))->getObjectTypeOrClassStringObjectType(); } $returnType = $this->propertyFetchType( diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 73d9e7f373..9c8a48acb0 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -1232,6 +1232,7 @@ public function dataFileAsserts(): iterable if (PHP_VERSION_ID >= 80000) { yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10071.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9394.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-nullsafe-prop-static-access.php'); } yield from $this->gatherAssertTypes(__DIR__ . '/data/allowed-subtypes-datetime.php'); diff --git a/tests/PHPStan/Analyser/data/bug-nullsafe-prop-static-access.php b/tests/PHPStan/Analyser/data/bug-nullsafe-prop-static-access.php new file mode 100644 index 0000000000..65ba442a78 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-nullsafe-prop-static-access.php @@ -0,0 +1,28 @@ += 8.0 + +namespace BugNullsafePropStaticAccess; + +class A +{ + public function __construct(public readonly B $b) + {} +} + +class B +{ + public static int $value = 0; + + public static function get(): string + { + return 'B'; + } +} + +function foo(?A $a): void +{ + \PHPStan\Testing\assertType('string|null', $a?->b::get()); + \PHPStan\Testing\assertType('string|null', $a?->b->get()); + + \PHPStan\Testing\assertType('int|null', $a?->b::$value); + \PHPStan\Testing\assertType('int|null', $a?->b->value); +}