@@ -42,7 +42,7 @@ final class ConstantArrayTypeBuilder
4242
4343 private bool $ oversized = false ;
4444
45- private bool $ initializedEmpty = false ;
45+ private bool $ forceUpdatesAutoIncrementKeyForNegativeValues = false ;
4646
4747 /**
4848 * @param list<Type> $keyTypes
@@ -76,20 +76,8 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
7676 );
7777
7878 $ phpVersion = PhpVersionStaticAccessor::getInstance ();
79- if (
80- !$ phpVersion ->updatesAutoIncrementKeyForNegativeValues ()
81- && $ phpVersion ->updatesAutoIncrementKeyForNegativeValuesInNonEmptyInitializer ()
82- ) {
83- if (count ($ startArrayType ->getKeyTypes ()) === 0 ) {
84- $ builder ->initializedEmpty = true ;
85- } elseif (max ($ startArrayType ->getNextAutoIndexes ()) === 0 ) {
86- foreach ($ startArrayType ->getKeyTypes () as $ keyType ) {
87- if ($ keyType instanceof ConstantIntegerType && $ keyType ->getValue () < 0 ) {
88- $ builder ->initializedEmpty = true ;
89- break ;
90- }
91- }
92- }
79+ if ($ phpVersion ->updatesAutoIncrementKeyForNegativeValuesOnlyInNonEmptyInitializer ()) {
80+ $ builder ->forceUpdatesAutoIncrementKeyForNegativeValues = !self ::wasInitializedEmpty ($ startArrayType );
9381 }
9482
9583 if (count ($ startArrayType ->getKeyTypes ()) > self ::ARRAY_COUNT_LIMIT ) {
@@ -450,12 +438,28 @@ private function shouldUpdateAutoIndex(int $offsetValue, int $max): bool
450438 }
451439
452440 $ phpVersion = PhpVersionStaticAccessor::getInstance ();
453-
454441 if ($ phpVersion ->updatesAutoIncrementKeyForNegativeValues ()) {
455442 return true ;
456443 }
457444
458- return !$ this ->initializedEmpty && $ phpVersion ->updatesAutoIncrementKeyForNegativeValuesInNonEmptyInitializer ();
445+ return !$ this ->forceUpdatesAutoIncrementKeyForNegativeValues ;
446+ }
447+
448+ private static function wasInitializedEmpty (ConstantArrayType $ startArrayType ): bool
449+ {
450+ if (count ($ startArrayType ->getKeyTypes ()) === 0 ) {
451+ return true ;
452+ }
453+
454+ if (max ($ startArrayType ->getNextAutoIndexes ()) === 0 ) {
455+ foreach ($ startArrayType ->getKeyTypes () as $ keyType ) {
456+ if ($ keyType instanceof ConstantIntegerType && $ keyType ->getValue () < 0 ) {
457+ return true ;
458+ }
459+ }
460+ }
461+
462+ return false ;
459463 }
460464
461465}
0 commit comments