Skip to content

Commit f6b34f2

Browse files
committed
Fix processing switch with complex case conditions
Closes phpstan/phpstan#5326
1 parent faba805 commit f6b34f2

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/Analyser/NodeScopeResolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ private function processStmtNode(
11881188
$scopeForBranches = $caseResult->getScope();
11891189
$hasYield = $hasYield || $caseResult->hasYield();
11901190
$throwPoints = array_merge($throwPoints, $caseResult->getThrowPoints());
1191-
$branchScope = $scopeForBranches->filterByTruthyValue($condExpr);
1191+
$branchScope = $caseResult->getTruthyScope()->filterByTruthyValue($condExpr);
11921192
} else {
11931193
$hasDefaultCase = true;
11941194
$branchScope = $scopeForBranches;

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3983,7 +3983,7 @@ public function testNotSwitchInstanceof(): void
39833983
{
39843984
$this->assertTypes(
39853985
__DIR__ . '/data/switch-instanceof-not.php',
3986-
'*ERROR*',
3986+
'*NEVER*',
39873987
'$foo',
39883988
);
39893989
}

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -967,4 +967,13 @@ public function testBug9474(): void
967967
$this->analyse([__DIR__ . '/data/bug-9474.php'], []);
968968
}
969969

970+
public function testBug5326(): void
971+
{
972+
$this->cliArgumentsVariablesRegistered = true;
973+
$this->polluteScopeWithLoopInitialAssignments = true;
974+
$this->checkMaybeUndefinedVariables = true;
975+
$this->polluteScopeWithAlwaysIterableForeach = true;
976+
$this->analyse([__DIR__ . '/data/bug-5326.php'], []);
977+
}
978+
970979
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Bug5326;
4+
5+
function normalize(string $value): string
6+
{
7+
switch (true) {
8+
case strpos($value, 'get_') === 0 && preg_match('/get_(\S+)/m', $value, $match) === 1:
9+
return $match[1];
10+
default:
11+
return $value;
12+
}
13+
}

0 commit comments

Comments
 (0)