diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 94510cd3a6..ddd6bd0211 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3238,6 +3238,9 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self // Pass 2: Supertype match. Only runs when Pass 1 found no exact match for this expression. foreach ($conditionalExpressions as $conditionalExpression) { + if ($conditionalExpression->getTypeHolder()->getCertainty()->no()) { + continue; + } foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) { if ( !array_key_exists($holderExprString, $specifiedExpressions) diff --git a/tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php b/tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php index d6a06d8581..88af910f2b 100644 --- a/tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php +++ b/tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php @@ -387,6 +387,11 @@ public function testBug4846(): void ]); } + public function testBug14458(): void + { + $this->analyse([__DIR__ . '/data/bug-14458.php'], []); + } + public function testBug14393(): void { $this->analyse([__DIR__ . '/data/bug-14393.php'], [ diff --git a/tests/PHPStan/Rules/Variables/data/bug-14458.php b/tests/PHPStan/Rules/Variables/data/bug-14458.php new file mode 100644 index 0000000000..552f883e86 --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-14458.php @@ -0,0 +1,50 @@ + $payload + * @return array + */ + public function doFoo(array $payload): array + { + if ($payload['a'] !== 'b') { + throw new \Exception(); + } + + if (isset($payload['c'])) { + $c = array_values(array_map(static fn (array $cd) => $cd[0], $payload['c'])); + } + + $convertedPriceWithVat = null; + if (array_key_exists('cpwv', $payload)) { + $convertedAmount = (float) $payload['cpwv']['awv']; + + } + + return [ + $payload['cf'], + $payload['n'], + $payload['d'] ?? null, + $payload['mb'] ?? null, + $payload['cr'], + new DateTime($payload['p']), + $payload['pn'], + $payload['pd'] ?? null, + $payload['piu'] ?? null, + $convertedPriceWithVat, + $payload['vi'], + $payload['pi'], + $payload['user']['name'] ?? null, + $payload['user']['phone'] ?? null, + $payload['ac'] ?? null, + $c ?? null, + ]; + } + +}