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

}
2 changes: 1 addition & 1 deletion src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type
return $this->oversizedArrayBuilder->build($expr, $getTypeCallback);
}

$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty(count($expr->items) > 0);
$isList = null;
$hasOffsetValueTypes = [];
foreach ($expr->items as $arrayItem) {
Expand Down
31 changes: 28 additions & 3 deletions 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 $initializedNonEmpty = false;

/**
* @param list<Type> $keyTypes
* @param array<int, Type> $valueTypes
Expand All @@ -57,9 +60,12 @@ private function __construct(
{
}

public static function createEmpty(): self
public static function createEmpty(bool $initializedNonEmpty = false): self
{
return new self([], [], [0], [], TrinaryLogic::createYes());
$self = new self([], [], [0], [], TrinaryLogic::createYes());
$self->initializedNonEmpty = $initializedNonEmpty;

return $self;
}

public static function createFromConstantArray(ConstantArrayType $startArrayType): self
Expand Down Expand Up @@ -209,7 +215,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 +425,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->initializedNonEmpty && $phpVersion->updatesAutoIncrementKeyForNegativeValuesInNonEmptyInitializer();
}

}
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