Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 13 additions & 7 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,22 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
if ($offsetType instanceof ConstantIntegerType) {
$min = min($this->nextAutoIndexes);
$max = max($this->nextAutoIndexes);
if ($offsetType->getValue() > $min) {
if ($offsetType->getValue() <= $max) {
$this->isList = $this->isList->and(TrinaryLogic::createMaybe());
} else {
$this->isList = TrinaryLogic::createNo();
$offsetValue = $offsetType->getValue();
if ($offsetValue >= 0) {
if ($offsetValue > $min) {
if ($offsetValue <= $max) {
$this->isList = $this->isList->and(TrinaryLogic::createMaybe());
} else {
$this->isList = TrinaryLogic::createNo();
}
}
} else {
$this->isList = TrinaryLogic::createNo();
}
if ($offsetType->getValue() >= $max) {

if ($offsetValue >= $max) {
/** @var int|float $newAutoIndex */
$newAutoIndex = $offsetType->getValue() + 1;
$newAutoIndex = $offsetValue + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $max;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/VarTagChangedExpressionTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,26 @@ public function testBug10130(): void
]);
}

public function testBug12708(): void
{
$this->analyse([__DIR__ . '/data/bug-12708.php'], [
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{1: 'b', 2: 'c'}.",
12,
],
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{0: 'a', 2: 'c'}.",
18,
],
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{-1: 'z', 0: 'a', 1: 'b', 2: 'c'}.",
24,
],
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{0: 'a', -1: 'z', 1: 'b', 2: 'c'}.",
30,
],
]);
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-12708.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

function do0()
{
/** @var list<string> */
return [0 => 'a', 1 => 'b', 2 => 'c'];
}

function do1()
{
/** @var list<string> */
return [1 => 'b', 2 => 'c'];
}

function do2()
{
/** @var list<string> */
return [0 => 'a', 2 => 'c'];
}

function do3()
{
/** @var list<string> */
return [-1 => 'z', 0 => 'a', 1 => 'b', 2 => 'c'];
}

function do4()
{
/** @var list<string> */
return [0 => 'a', -1 => 'z', 1 => 'b', 2 => 'c'];
}
Loading