diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 0f90b3d6be..4a4a89bb5f 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3929,17 +3929,7 @@ private function createConditionalExpressions( continue; } - $exprIsGuardExcluded = array_key_exists($exprString, $guardsToExclude); foreach ($variableTypeGuards as $guardExprString => $guardHolder) { - // A subtype-absorbed target (kept only for re-narrowing) paired with a - // constant-array guard never helps: such a guard represents a unique - // literal value that is not re-asserted as a condition later, yet the - // downstream isSuperTypeOf() guard machinery pays to compare these - // (potentially huge) constant arrays on every branch merge. Skip them. - if ($exprIsGuardExcluded && $guardHolder->getType()->isConstantArray()->yes()) { - continue; - } - if ( array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() diff --git a/tests/PHPStan/Analyser/nsrt/bug-14926.php b/tests/PHPStan/Analyser/nsrt/bug-14926.php new file mode 100644 index 0000000000..aee82d3215 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-14926.php @@ -0,0 +1,35 @@ += 8.0 + +declare(strict_types = 1); + +namespace Bug14926; + +use function PHPStan\Testing\assertType; + +function test_if(?string $x): void +{ + if ($x === 'aa') { + $class = ['some_class']; + } elseif ($x === 'bb' || $x === 'cc') { + $class = ['another_class']; + } else { + $class = null; + } + if ($class === null) { + return; + } + assertType("'aa'|'bb'|'cc'", $x); +} + +function test_match(?string $x): void +{ + $class = match ($x) { + 'aa' => ['some_class'], + 'bb', 'cc' => ['another_class'], + default => null, + }; + if ($class === null) { + return; + } + assertType("'aa'|'bb'|'cc'", $x); +}