Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 updatesAutoIncrementKeyForNegativeValuesOnlyInNonEmptyInitializer(): bool
{
return $this->versionId >= 80000 && $this->versionId < 80300;
}

}
46 changes: 45 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 $initializedEmpty = false;

/**
* @param list<Type> $keyTypes
* @param array<int, Type> $valueTypes
Expand Down Expand Up @@ -72,6 +75,8 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
$startArrayType->isList(),
);

$builder->initializedEmpty = self::wasInitializedEmpty($startArrayType);

if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
$builder->degradeToGeneralArray(true);
}
Expand Down Expand Up @@ -209,7 +214,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 +424,43 @@ 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;
}

if ($phpVersion->updatesAutoIncrementKeyForNegativeValuesOnlyInNonEmptyInitializer()) {
return !$this->initializedEmpty;
}

return false;
}

private static function wasInitializedEmpty(ConstantArrayType $startArrayType): bool
{
if (count($startArrayType->getKeyTypes()) === 0) {
return true;
}

if (max($startArrayType->getNextAutoIndexes()) === 0) {
foreach ($startArrayType->getKeyTypes() as $keyType) {
if ($keyType instanceof ConstantIntegerType && $keyType->getValue() < 0) {
return true;
}
}
}

return false;
}

}
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);
};
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10862-php80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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);
};

// Non-empty string-key array: negative key should update auto-index
function () {
$a = ['foo' => 'bar'];
$a[-5] = 'x';
$a[] = 'y';

assertType("array{foo: 'bar', -5: 'x', -4: 'y'}", $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

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