Skip to content

Commit 7ddfa17

Browse files
committed
Enter assignment of property fetch's var when in null coalesce operator
1 parent 165504c commit 7ddfa17

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,8 +2326,14 @@ static function () use ($expr, $rightResult): MutatingScope {
23262326
} elseif ($expr instanceof Coalesce) {
23272327
$nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left, false);
23282328

2329-
if ($expr->left instanceof PropertyFetch || $expr->left instanceof StaticPropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch) {
2330-
$scope = $nonNullabilityResult->getScope();
2329+
if ($expr->left instanceof PropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch) {
2330+
$scope = $this->lookForEnterVariableAssign($nonNullabilityResult->getScope(), $expr->left->var);
2331+
} elseif ($expr->left instanceof StaticPropertyFetch) {
2332+
if ($expr->left->class instanceof Expr) {
2333+
$scope = $this->lookForEnterVariableAssign($nonNullabilityResult->getScope(), $expr->left->class);
2334+
} else {
2335+
$scope = $nonNullabilityResult->getScope();
2336+
}
23312337
} else {
23322338
$scope = $this->lookForEnterVariableAssign($nonNullabilityResult->getScope(), $expr->left);
23332339
}

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,4 +840,14 @@ public function testBug4412(): void
840840
]);
841841
}
842842

843+
public function testBug3283(): void
844+
{
845+
$this->cliArgumentsVariablesRegistered = true;
846+
$this->polluteScopeWithLoopInitialAssignments = false;
847+
$this->polluteCatchScopeWithTryAssignments = false;
848+
$this->checkMaybeUndefinedVariables = true;
849+
$this->polluteScopeWithAlwaysIterableForeach = true;
850+
$this->analyse([__DIR__ . '/data/bug-3283.php'], []);
851+
}
852+
843853
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug3283;
4+
5+
function (): void
6+
{
7+
$test = random_int(0, 10);
8+
9+
if ($test > 5) {
10+
$user = new \stdClass;
11+
$user->name = 'Thibaud';
12+
}
13+
14+
echo $user->name ?? 'Default';
15+
};

0 commit comments

Comments
 (0)