diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 8f3a85e1c6..7aa5eb338e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1702,6 +1702,9 @@ private function resolveType(string $exprString, Expr $node): Type if ($node instanceof Expr\NullsafeMethodCall) { $varType = $this->getType($node->var); + if ($varType->isNull()->yes()) { + return new NullType(); + } if (!TypeCombinator::containsNull($varType)) { return $this->getType(new MethodCall($node->var, $node->name, $node->args)); } @@ -1797,6 +1800,9 @@ private function resolveType(string $exprString, Expr $node): Type if ($node instanceof Expr\NullsafePropertyFetch) { $varType = $this->getType($node->var); + if ($varType->isNull()->yes()) { + return new NullType(); + } if (!TypeCombinator::containsNull($varType)) { return $this->getType(new PropertyFetch($node->var, $node->name)); } diff --git a/tests/PHPStan/Analyser/data/nullsafe.php b/tests/PHPStan/Analyser/data/nullsafe.php index fcb27c2ebd..ed4b00481a 100644 --- a/tests/PHPStan/Analyser/data/nullsafe.php +++ b/tests/PHPStan/Analyser/data/nullsafe.php @@ -99,4 +99,11 @@ public function doDolor(?self $self) assertType('Nullsafe\Foo|null', $self?->nullableSelf); } + public function doNull(): void + { + $null = null; + assertType('null', $null?->foo); + assertType('null', $null?->doFoo()); + } + }