Skip to content

Commit 1570544

Browse files
phpstan-botclaude
andcommitted
Extract auto-index condition into private method and handle PHP 8.0+ array literal behavior
PHP 8.0 changed array literals to update the auto-increment key for negative values, while imperative assignment only got this fix in PHP 8.3. Extract the condition into shouldUpdateAutoIndexForOffset() for readability and add isLiteralArray flag to ConstantArrayTypeBuilder to distinguish the two cases. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 483652a commit 1570544

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Php/PhpVersion.php

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

519+
public function updatesAutoIncrementKeyForNegativeValuesInArrayLiteral(): bool
520+
{
521+
return $this->versionId >= 80000;
522+
}
523+
519524
}

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type
639639
}
640640

641641
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
642+
$arrayBuilder->setLiteralArray();
642643
$isList = null;
643644
$hasOffsetValueTypes = [];
644645
foreach ($expr->items as $arrayItem) {

src/Type/Constant/ConstantArrayTypeBuilder.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ final class ConstantArrayTypeBuilder
4242

4343
private bool $oversized = false;
4444

45+
private bool $isLiteralArray = false;
46+
4547
/**
4648
* @param list<Type> $keyTypes
4749
* @param array<int, Type> $valueTypes
@@ -63,6 +65,11 @@ public static function createEmpty(): self
6365
return new self([], [], [0], [], TrinaryLogic::createYes());
6466
}
6567

68+
public function setLiteralArray(): void
69+
{
70+
$this->isLiteralArray = true;
71+
}
72+
6673
public static function createFromConstantArray(ConstantArrayType $startArrayType): self
6774
{
6875
$builder = new self(
@@ -426,7 +433,17 @@ private function shouldUpdateAutoIndex(int $offsetValue, int $max): bool
426433
return true;
427434
}
428435

429-
return $offsetValue < 0 && $max === 0 && PhpVersionStaticAccessor::getInstance()->updatesAutoIncrementKeyForNegativeValues();
436+
if ($offsetValue >= 0 || $max !== 0) {
437+
return false;
438+
}
439+
440+
$phpVersion = PhpVersionStaticAccessor::getInstance();
441+
442+
if ($phpVersion->updatesAutoIncrementKeyForNegativeValues()) {
443+
return true;
444+
}
445+
446+
return $this->isLiteralArray && $phpVersion->updatesAutoIncrementKeyForNegativeValuesInArrayLiteral();
430447
}
431448

432449
}

0 commit comments

Comments
 (0)