Skip to content

Commit 94e9657

Browse files
phpstan-botclaude
andcommitted
Fix initializedEmpty not propagating through createFromConstantArray round-trips
When processing `$a = []; $a[-10] = 'a'; $a[-5] = 'b';`, the second assignment creates a new builder via createFromConstantArray with a non-empty array, losing the initializedEmpty flag. This caused shouldUpdateAutoIndex to incorrectly return true on PHP 8.0-8.2. Fix: in createFromConstantArray, also infer initializedEmpty when the source array has negative int keys but max(nextAutoIndexes) === 0, since a non-empty initializer on PHP 8.0+ would have already bumped the auto-index. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2ad0306 commit 94e9657

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/Type/Constant/ConstantArrayTypeBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
7777

7878
if (count($startArrayType->getKeyTypes()) === 0) {
7979
$builder->initializedEmpty = true;
80+
} elseif (max($startArrayType->getNextAutoIndexes()) === 0) {
81+
foreach ($startArrayType->getKeyTypes() as $keyType) {
82+
if ($keyType instanceof ConstantIntegerType && $keyType->getValue() < 0) {
83+
$builder->initializedEmpty = true;
84+
break;
85+
}
86+
}
8087
}
8188

8289
if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {

0 commit comments

Comments
 (0)