Skip to content

Commit

Permalink
Fix null coalesce nullsafe in AccessPropertiesRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 12, 2020
1 parent 2444b4b commit 023cc70
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ static function () use ($expr, $rightResult): MutatingScope {
} elseif ($expr instanceof Coalesce) {
$nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left, false);

if ($expr->left instanceof PropertyFetch || $expr->left instanceof StaticPropertyFetch) {
if ($expr->left instanceof PropertyFetch || $expr->left instanceof StaticPropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch) {
$scope = $nonNullabilityResult->getScope();
} else {
$scope = $this->lookForEnterVariableAssign($nonNullabilityResult->getScope(), $expr->left);
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,22 @@ public function testNullSafe(): void
'Access to an undefined property NullsafePropertyFetch\Foo::$baz.',
13,
],
[
'Cannot access property $bar on string.',
18,
],
[
'Cannot access property $bar on string.',
19,
],
[
'Cannot access property $bar on string.',
21,
],
[
'Cannot access property $bar on string.',
22,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ public function doFoo(?self $selfOrNull): void
$selfOrNull?->baz;
}

public function doBar(string $string, ?string $nullableString): void
{
echo $string->bar ?? 4;
echo $nullableString->bar ?? 4;

echo $string?->bar ?? 4;
echo $nullableString?->bar ?? 4;
}

}

0 comments on commit 023cc70

Please sign in to comment.