diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index a26a86a066f..900c3f3fff1 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3680,10 +3680,6 @@ public function addTypeToExpression(Expr $expr, Type $type): self if ($originalExprType->equals($nativeType)) { $newType = TypeCombinator::intersect($type, $originalExprType); - if ($newType->isConstantScalarValue()->yes() && $newType->equals($originalExprType)) { - // don't add the same type over and over again to improve performance - return $this; - } return $this->specifyExpressionType($expr, $newType, $newType, TrinaryLogic::createYes()); } diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index f877c5dec77..76f6fc8f514 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -1155,4 +1155,10 @@ public function testBug11276(): void $this->analyse([__DIR__ . '/data/bug-11276.php'], []); } + public function testBug13526(): void + { + $this->reportPossiblyNonexistentConstantArrayOffset = true; + $this->analyse([__DIR__ . '/data/bug-13526.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-13526.php b/tests/PHPStan/Rules/Arrays/data/bug-13526.php new file mode 100644 index 00000000000..c323303e882 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-13526.php @@ -0,0 +1,26 @@ + 1, + 'MAP_1_ITEM_2' => 2, + ]; + $map2 = [ + 'MAP_2_ITEM_1' => 1, + 'MAP_2_ITEM_2' => 2, + ]; + + $map = $a ? $map1 : $map2; + + if (array_key_exists($item, $map)) { + return $map[$item]; + } + + return null; + } +}