Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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;
}

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

$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
if (count($expr->items) > 0) {
$arrayBuilder->setInitializedNonEmpty();
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.

Rather than an extra method, what about an optional param in createEmpty ?

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.

Done. Replaced setInitializedNonEmpty() with an optional bool $initializedNonEmpty = false parameter on createEmpty(), and updated the call site in InitializerExprTypeResolver to pass count($expr->items) > 0 directly. Both make phpstan and make tests pass.

}
$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 $initializedNonEmpty = false;

/**
* @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 setInitializedNonEmpty(): void
{
$this->initializedNonEmpty = 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->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