Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a06d509

Browse files
schlndhondrejmirtes
authored andcommitted
fix type of $a?->b::c() and $a?->b::$c
1 parent ca55346 commit a06d509

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

‎src/Analyser/MutatingScope.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ private function resolveType(string $exprString, Expr $node): Type
18021802
if ($node->class instanceof Name) {
18031803
$staticMethodCalledOnType = $this->resolveTypeByName($node->class);
18041804
} else {
1805-
$staticMethodCalledOnType = $this->getType($node->class)->getObjectTypeOrClassStringObjectType();
1805+
$staticMethodCalledOnType = TypeCombinator::removeNull($this->getType($node->class))->getObjectTypeOrClassStringObjectType();
18061806
}
18071807

18081808
$returnType = $this->methodCallReturnType(
@@ -1894,7 +1894,7 @@ private function resolveType(string $exprString, Expr $node): Type
18941894
if ($node->class instanceof Name) {
18951895
$staticPropertyFetchedOnType = $this->resolveTypeByName($node->class);
18961896
} else {
1897-
$staticPropertyFetchedOnType = $this->getType($node->class)->getObjectTypeOrClassStringObjectType();
1897+
$staticPropertyFetchedOnType = TypeCombinator::removeNull($this->getType($node->class))->getObjectTypeOrClassStringObjectType();
18981898
}
18991899

19001900
$returnType = $this->propertyFetchType(

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ public function dataFileAsserts(): iterable
12321232
if (PHP_VERSION_ID >= 80000) {
12331233
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10071.php');
12341234
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9394.php');
1235+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-nullsafe-prop-static-access.php');
12351236
}
12361237

12371238
yield from $this->gatherAssertTypes(__DIR__ . '/data/allowed-subtypes-datetime.php');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1); // lint >= 8.0
2+
3+
namespace BugNullsafePropStaticAccess;
4+
5+
class A
6+
{
7+
public function __construct(public readonly B $b)
8+
{}
9+
}
10+
11+
class B
12+
{
13+
public static int $value = 0;
14+
15+
public static function get(): string
16+
{
17+
return 'B';
18+
}
19+
}
20+
21+
function foo(?A $a): void
22+
{
23+
\PHPStan\Testing\assertType('string|null', $a?->b::get());
24+
\PHPStan\Testing\assertType('string|null', $a?->b->get());
25+
26+
\PHPStan\Testing\assertType('int|null', $a?->b::$value);
27+
\PHPStan\Testing\assertType('int|null', $a?->b->value);
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /