Skip to content

Commit 4a7fe14

Browse files
Rework
1 parent ee2c7a3 commit 4a7fe14

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/Php/PhpVersion.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ public function updatesAutoIncrementKeyForNegativeValues(): bool
516516
return $this->versionId >= 80300;
517517
}
518518

519-
public function updatesAutoIncrementKeyForNegativeValuesInNonEmptyInitializer(): bool
519+
public function updatesAutoIncrementKeyForNegativeValuesOnlyInNonEmptyInitializer(): bool
520520
{
521-
return $this->versionId >= 80000;
521+
return $this->versionId >= 80000 && $this->versionId < 80300;
522522
}
523523

524524
}

src/Type/Constant/ConstantArrayTypeBuilder.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)