Skip to content

Commit

Permalink
Nullsafe operator on null results in null
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 7, 2023
1 parent 4d77e98 commit 5c40c85
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/nullsafe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit 5c40c85

Please sign in to comment.