Skip to content

Commit 2ad0306

Browse files
phpstan-botclaude
andcommitted
Replace initializedNonEmpty with initializedEmpty set in createFromConstantArray
Instead of setting initializedNonEmpty=true in createEmpty(), use the inverse property initializedEmpty and set it in createFromConstantArray() when the source array has no keys. This is cleaner because createEmpty() is always used to build arrays that will have items added, while the only truly "initialized empty" case is createFromConstantArray with an empty ConstantArrayType (representing $a = [] imperative assignment). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 40956df commit 2ad0306

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Type/Constant/ConstantArrayTypeBuilder.php

Lines changed: 7 additions & 6 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 $initializedNonEmpty = false;
45+
private bool $initializedEmpty = false;
4646

4747
/**
4848
* @param list<Type> $keyTypes
@@ -62,10 +62,7 @@ private function __construct(
6262

6363
public static function createEmpty(): self
6464
{
65-
$self = new self([], [], [0], [], TrinaryLogic::createYes());
66-
$self->initializedNonEmpty = true;
67-
68-
return $self;
65+
return new self([], [], [0], [], TrinaryLogic::createYes());
6966
}
7067

7168
public static function createFromConstantArray(ConstantArrayType $startArrayType): self
@@ -78,6 +75,10 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
7875
$startArrayType->isList(),
7976
);
8077

78+
if (count($startArrayType->getKeyTypes()) === 0) {
79+
$builder->initializedEmpty = true;
80+
}
81+
8182
if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
8283
$builder->degradeToGeneralArray(true);
8384
}
@@ -441,7 +442,7 @@ private function shouldUpdateAutoIndex(int $offsetValue, int $max): bool
441442
return true;
442443
}
443444

444-
return $this->initializedNonEmpty && $phpVersion->updatesAutoIncrementKeyForNegativeValuesInNonEmptyInitializer();
445+
return !$this->initializedEmpty && $phpVersion->updatesAutoIncrementKeyForNegativeValuesInNonEmptyInitializer();
445446
}
446447

447448
}

0 commit comments

Comments
 (0)