Skip to content

Commit 846f44e

Browse files
committed
Merge right scope of coalesce assign operator with scope before the expression
1 parent 918c47d commit 846f44e

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,10 +1959,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
19591959
$result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep());
19601960
if ($expr instanceof Expr\AssignOp\Coalesce) {
19611961
return new ExpressionResult(
1962-
$result->getScope()->mergeWith(
1963-
$this->processExprNode($expr->expr, $originalScope, static function (): void {
1964-
}, $context->enterDeep())->getScope(),
1965-
),
1962+
$result->getScope()->mergeWith($originalScope),
19661963
$result->hasYield(),
19671964
$result->getThrowPoints(),
19681965
);

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,12 @@ public function testObjectShapes(): void
450450
$this->analyse([__DIR__ . '/data/isset-object-shapes.php'], []);
451451
}
452452

453+
public function testBug10151(): void
454+
{
455+
$this->treatPhpDocTypesAsCertain = true;
456+
$this->strictUnnecessaryNullsafePropertyFetch = true;
457+
458+
$this->analyse([__DIR__ . '/data/bug-10151.php'], []);
459+
}
460+
453461
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Bug10151;
4+
5+
class Test
6+
{
7+
/**
8+
* @var array<int>
9+
*/
10+
protected array $cache = [];
11+
12+
public function getCachedItemId (string $keyName): void
13+
{
14+
$result = $this->cache[$keyName] ??= ($newIndex = count($this->cache) + 1);
15+
16+
// WRONG ERROR: Variable $newIndex in isset() always exists and is not nullable.
17+
if (isset($newIndex)) {
18+
$this->recordNewCacheItem($keyName);
19+
}
20+
}
21+
22+
protected function recordNewCacheItem (string $keyName): void {
23+
// ...
24+
}
25+
}

0 commit comments

Comments
 (0)