Skip to content

Commit 5c40c85

Browse files
committed
Nullsafe operator on null results in null
1 parent 4d77e98 commit 5c40c85

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,9 @@ private function resolveType(string $exprString, Expr $node): Type
17021702

17031703
if ($node instanceof Expr\NullsafeMethodCall) {
17041704
$varType = $this->getType($node->var);
1705+
if ($varType->isNull()->yes()) {
1706+
return new NullType();
1707+
}
17051708
if (!TypeCombinator::containsNull($varType)) {
17061709
return $this->getType(new MethodCall($node->var, $node->name, $node->args));
17071710
}
@@ -1797,6 +1800,9 @@ private function resolveType(string $exprString, Expr $node): Type
17971800

17981801
if ($node instanceof Expr\NullsafePropertyFetch) {
17991802
$varType = $this->getType($node->var);
1803+
if ($varType->isNull()->yes()) {
1804+
return new NullType();
1805+
}
18001806
if (!TypeCombinator::containsNull($varType)) {
18011807
return $this->getType(new PropertyFetch($node->var, $node->name));
18021808
}

tests/PHPStan/Analyser/data/nullsafe.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,11 @@ public function doDolor(?self $self)
9999
assertType('Nullsafe\Foo|null', $self?->nullableSelf);
100100
}
101101

102+
public function doNull(): void
103+
{
104+
$null = null;
105+
assertType('null', $null?->foo);
106+
assertType('null', $null?->doFoo());
107+
}
108+
102109
}

0 commit comments

Comments
 (0)