Skip to content

Commit

Permalink
Fix conditional types regression for `polluteScopeWithAlwaysIterableF…
Browse files Browse the repository at this point in the history
…oreach: false`
  • Loading branch information
rajyan committed Dec 8, 2022
1 parent bff85e2 commit e4e5aa9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -818,11 +818,11 @@ private function processStmtNode(
$inForeachScope = $this->processVarAnnotation($scope, [$stmt->expr->name], $stmt);
}
$nodeCallback(new InForeachNode($stmt), $inForeachScope);
$bodyScope = $this->enterForeach($scope->filterByTruthyValue($arrayComparisonExpr), $stmt);
$bodyScope = $this->polluteScopeWithAlwaysIterableForeach ? $this->enterForeach($scope->filterByTruthyValue($arrayComparisonExpr), $stmt) : $this->enterForeach($scope, $stmt);
$count = 0;
do {
$prevScope = $bodyScope;
$bodyScope = $bodyScope->mergeWith($scope->filterByTruthyValue($arrayComparisonExpr));
$bodyScope = $bodyScope->mergeWith($this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope);
$bodyScope = $this->enterForeach($bodyScope, $stmt);
$bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void {
})->filterOutLoopExitPoints();
Expand All @@ -841,7 +841,7 @@ private function processStmtNode(
$count++;
} while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS);

$bodyScope = $bodyScope->mergeWith($scope->filterByTruthyValue($arrayComparisonExpr));
$bodyScope = $bodyScope->mergeWith($this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope);
$bodyScope = $this->enterForeach($bodyScope, $stmt);
$finalScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, $nodeCallback)->filterOutLoopExitPoints();
$finalScope = $finalScopeResult->getScope();
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Expand Up @@ -927,4 +927,22 @@ public function testBug5805(): void
$this->analyse([__DIR__ . '/data/bug-5805.php'], []);
}

public function testBug8467c(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = false;
$this->analyse([__DIR__ . '/data/bug-8467c.php'], [
[
'Variable $v might not be defined.',
16,
],
[
'Variable $v might not be defined.',
18,
],
]);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-8467c.php
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Bug8467c;

/**
* @param int[] $a
* @return void
*/
function foo(array $a, int $key): void
{
$k = [];
foreach ($a as $v) {
$k[$key][] = $v;
}

echo $v;
foreach ($k as $values) {
echo $v;
foreach ($values as $v) {
echo $v;
}
}
}

0 comments on commit e4e5aa9

Please sign in to comment.