Skip to content

Commit

Permalink
Fix processing switch with complex case conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 30, 2023
1 parent faba805 commit f6b34f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ private function processStmtNode(
$scopeForBranches = $caseResult->getScope();
$hasYield = $hasYield || $caseResult->hasYield();
$throwPoints = array_merge($throwPoints, $caseResult->getThrowPoints());
$branchScope = $scopeForBranches->filterByTruthyValue($condExpr);
$branchScope = $caseResult->getTruthyScope()->filterByTruthyValue($condExpr);
} else {
$hasDefaultCase = true;
$branchScope = $scopeForBranches;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3983,7 +3983,7 @@ public function testNotSwitchInstanceof(): void
{
$this->assertTypes(
__DIR__ . '/data/switch-instanceof-not.php',
'*ERROR*',
'*NEVER*',
'$foo',
);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -967,4 +967,13 @@ public function testBug9474(): void
$this->analyse([__DIR__ . '/data/bug-9474.php'], []);
}

public function testBug5326(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-5326.php'], []);
}

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

namespace Bug5326;

function normalize(string $value): string
{
switch (true) {
case strpos($value, 'get_') === 0 && preg_match('/get_(\S+)/m', $value, $match) === 1:
return $match[1];
default:
return $value;
}
}

0 comments on commit f6b34f2

Please sign in to comment.