diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 85867200a20..667e388a0ac 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -4557,6 +4557,14 @@ private function preserveVacuousConditionalExpressions( continue; } + // Array shapes are unsealed in isSuperTypeOf(): array{a?: int} is a supertype + // of array{a?: int, b: int}, yet applying the holder would drop the 'b' key + // the other branch tracks. The consequent only holds on both paths when the + // other branch's state adds nothing to it. + if (!TypeCombinator::union($typeHolder->getType(), $otherTargetHolder->getType())->equals($typeHolder->getType())) { + continue; + } + $currentConditionalExpressions[$exprString][$key] = $holder; } } diff --git a/tests/PHPStan/Analyser/Bug15534Test.php b/tests/PHPStan/Analyser/Bug15534Test.php new file mode 100644 index 00000000000..f0314f12fb8 --- /dev/null +++ b/tests/PHPStan/Analyser/Bug15534Test.php @@ -0,0 +1,29 @@ +assertFileAsserts($assertType, $file, ...$args); + } + +} diff --git a/tests/PHPStan/Analyser/data/bug-15534.php b/tests/PHPStan/Analyser/data/bug-15534.php new file mode 100644 index 00000000000..44b780ab1b4 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-15534.php @@ -0,0 +1,38 @@ + + */ +class NullCoalesceRuleNoBleedingEdgeTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new NullCoalesceRule( + new IssetCheck( + new PropertyDescriptor(), + true, + $this->shouldTreatPhpDocTypesAsCertain(), + ), + self::getContainer()->getByType(ConstantConditionInTraitHelper::class), + true, + ); + } + + #[Override] + public static function getAdditionalConfigFiles(): array + { + return []; + } + + public function testBug15534(): void + { + $this->analyse([__DIR__ . '/data/bug-15534.php'], []); + } + +} diff --git a/tests/PHPStan/Rules/Variables/data/bug-15534.php b/tests/PHPStan/Rules/Variables/data/bug-15534.php new file mode 100644 index 00000000000..4c2aca54d6d --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-15534.php @@ -0,0 +1,66 @@ +