Skip to content

Commit

Permalink
Merge right scope of coalesce assign operator with scope before the e…
Browse files Browse the repository at this point in the history
…xpression
  • Loading branch information
ondrejmirtes committed Nov 18, 2023
1 parent 918c47d commit 846f44e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1959,10 +1959,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep());
if ($expr instanceof Expr\AssignOp\Coalesce) {
return new ExpressionResult(
$result->getScope()->mergeWith(
$this->processExprNode($expr->expr, $originalScope, static function (): void {
}, $context->enterDeep())->getScope(),
),
$result->getScope()->mergeWith($originalScope),
$result->hasYield(),
$result->getThrowPoints(),
);
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,12 @@ public function testObjectShapes(): void
$this->analyse([__DIR__ . '/data/isset-object-shapes.php'], []);
}

public function testBug10151(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = true;

$this->analyse([__DIR__ . '/data/bug-10151.php'], []);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-10151.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Bug10151;

class Test
{
/**
* @var array<int>
*/
protected array $cache = [];

public function getCachedItemId (string $keyName): void
{
$result = $this->cache[$keyName] ??= ($newIndex = count($this->cache) + 1);

// WRONG ERROR: Variable $newIndex in isset() always exists and is not nullable.
if (isset($newIndex)) {
$this->recordNewCacheItem($keyName);
}
}

protected function recordNewCacheItem (string $keyName): void {
// ...
}
}

0 comments on commit 846f44e

Please sign in to comment.