Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,14 @@ public function throwsOnStringCast(): bool
return $this->versionId >= 70400;
}

public function updatesAutoIncrementKeyForNegativeValues(): bool
{
return $this->versionId >= 80300;
}

public function updatesAutoIncrementKeyForNegativeValuesInArrayLiteral(): bool
{
return $this->versionId >= 80000;
}

}
1 change: 1 addition & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type
}

$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$arrayBuilder->setLiteralArray();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it really make sense only in this scenario? don't we need this behaviour every time a array-shape with known/constant keys is involved?

$isList = null;
$hasOffsetValueTypes = [];
foreach ($expr->items as $arrayItem) {
Expand Down
29 changes: 28 additions & 1 deletion src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Constant;

use PHPStan\Reflection\PhpVersionStaticAccessor;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
Expand Down Expand Up @@ -41,6 +42,8 @@ final class ConstantArrayTypeBuilder

private bool $oversized = false;

private bool $isLiteralArray = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the "isLiteralArray" naming matches reality. we need to find a better name

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed. Here's what I did to address staabm's review:

Renamed isLiteralArrayisArrayExpression across all three files:

  • ConstantArrayTypeBuilder::$isLiteralArray$isArrayExpression
  • ConstantArrayTypeBuilder::setLiteralArray()setArrayExpression()
  • PhpVersion::updatesAutoIncrementKeyForNegativeValuesInArrayLiteral()updatesAutoIncrementKeyForNegativeValuesInArrayExpression()
  • InitializerExprTypeResolver call site updated accordingly

The term "array expression" better matches PHP parser terminology (Expr\Array_) and PHPStan's own naming conventions.

Regarding staabm's second concern (whether this is needed beyond InitializerExprTypeResolver::getArrayType()): The flag is correctly scoped. It only needs to be set when processing Expr\Array_ nodes because that's the only context where PHP 8.0-8.2 changed the negative auto-index behavior. When createFromConstantArray() is used (e.g., for $a[] = 2 after $a = [-4 => 1]), the nextAutoIndexes are already correctly inherited from the source ConstantArrayType — the flag was already applied when the array expression was first built. Other createEmpty() call sites build arrays from PHPDoc types, function return types, etc., where PHP runtime auto-index semantics don't apply.


/**
* @param list<Type> $keyTypes
* @param array<int, Type> $valueTypes
Expand All @@ -62,6 +65,11 @@ public static function createEmpty(): self
return new self([], [], [0], [], TrinaryLogic::createYes());
}

public function setLiteralArray(): void
{
$this->isLiteralArray = true;
}

public static function createFromConstantArray(ConstantArrayType $startArrayType): self
{
$builder = new self(
Expand Down Expand Up @@ -209,7 +217,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$this->isList = TrinaryLogic::createNo();
}

if ($offsetValue >= $max) {
if ($this->shouldUpdateAutoIndex($offsetValue, $max)) {
/** @var int|float $newAutoIndex */
$newAutoIndex = $offsetValue + 1;
if (is_float($newAutoIndex)) {
Expand Down Expand Up @@ -419,4 +427,23 @@ public function isList(): bool
return $this->isList->yes();
}

private function shouldUpdateAutoIndex(int $offsetValue, int $max): bool
{
if ($offsetValue >= $max) {
return true;
}

if ($offsetValue >= 0 || $max !== 0) {
return false;
}

$phpVersion = PhpVersionStaticAccessor::getInstance();

if ($phpVersion->updatesAutoIncrementKeyForNegativeValues()) {
return true;
}

return $this->isLiteralArray && $phpVersion->updatesAutoIncrementKeyForNegativeValuesInArrayLiteral();
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10862-php74.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint < 8.0

declare(strict_types = 1);

namespace Bug10862Php74;

use function PHPStan\Testing\assertType;

// Pre-PHP 8.0: negative keys never affect auto-index

// Imperative assignment
function () {
$a = [];
$a[-4] = 1;
$a[] = 2;

assertType('array{-4: 1, 0: 2}', $a);
};

// Array literal
function () {
$a = [-4 => 1];
$a[] = 2;

assertType('array{-4: 1, 0: 2}', $a);
};
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10862-php80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug10862Php80;

use function PHPStan\Testing\assertType;

// PHP 8.0+: array literal with negative keys updates auto-index

function () {
$a = [-4 => 1];
$a[] = 2;

assertType('array{-4: 1, -3: 2}', $a);
};

function () {
$a = [-10 => 'a', -5 => 'b'];
$a[] = 'c';

assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a);
};
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10862-php82.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php // lint <= 8.2
Comment thread
VincentLanglet marked this conversation as resolved.

declare(strict_types = 1);

namespace Bug10862Php82;

use function PHPStan\Testing\assertType;

// PHP <= 8.2: imperative assignment with negative keys does not affect auto-index

function () {
$a = [];
$a[-4] = 1;
$a[] = 2;

assertType('array{-4: 1, 0: 2}', $a);
};

function () {
$a = [];
$a[-1] = 'x';
$a[] = 'y';

assertType("array{-1: 'x', 0: 'y'}", $a);
};

function () {
$a = [];
$a[-10] = 'a';
$a[-5] = 'b';
$a[] = 'c';

assertType("array{-10: 'a', -5: 'b', 0: 'c'}", $a);
};

function () {
$a = [];
$a[-3] = 'a';
$a[5] = 'b';
$a[] = 'c';

assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a);
};
59 changes: 59 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10862-php83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php // lint >= 8.3

declare(strict_types = 1);

namespace Bug10862Php83;

use function PHPStan\Testing\assertType;

// PHP 8.3+: negative keys always affect auto-index (both imperative and literal)

// Imperative assignment
function () {
$a = [];
$a[-4] = 1;
$a[] = 2;

assertType('array{-4: 1, -3: 2}', $a);
};

function () {
$a = [];
$a[-1] = 'x';
$a[] = 'y';

assertType("array{-1: 'x', 0: 'y'}", $a);
};

function () {
$a = [];
$a[-10] = 'a';
$a[-5] = 'b';
$a[] = 'c';

assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a);
};

function () {
$a = [];
$a[-3] = 'a';
$a[5] = 'b';
$a[] = 'c';

assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a);
};

// Array literal
function () {
$a = [-4 => 1];
$a[] = 2;

assertType('array{-4: 1, -3: 2}', $a);
};

function () {
$a = [-10 => 'a', -5 => 'b'];
$a[] = 'c';

assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a);
};
Loading