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
26 changes: 24 additions & 2 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,30 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni

$result = $this->intersectTypes(static fn (Type $type): Type => $type->setOffsetValueType($offsetType, $valueType, $unionValues));

if ($offsetType !== null && $this->isList()->yes() && $this->isIterableAtLeastOnce()->yes() && (new ConstantIntegerType(1))->isSuperTypeOf($offsetType)->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this previously existing logic, which was implemented for phpstan/phpstan#12131 - only worked for index 1. the new logic works for the generic case

$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
if (
$offsetType !== null
&& $this->isList()->yes()
&& !$result->isList()->yes()
) {
if ($this->isIterableAtLeastOnce()->yes() && (new ConstantIntegerType(1))->isSuperTypeOf($offsetType)->yes()) {
$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
} else {
foreach ($this->types as $type) {
if (!$type instanceof HasOffsetValueType && !$type instanceof HasOffsetType) {
continue;
}

foreach ($type->getOffsetType()->getConstantScalarValues() as $constantScalarValue) {
if (!is_int($constantScalarValue)) {
continue;
}
if (IntegerRangeType::fromInterval(0, $constantScalarValue + 1)->isSuperTypeOf($offsetType)->yes()) {
$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
break 2;
}
}
}
}
}

return $result;
Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/nsrt/list-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,45 @@ public function testSetOffsetExplicitlyWithGap(array $list): void
assertType('non-empty-array<int<0, max>, int>&hasOffsetValue(0, 17)&hasOffsetValue(2, 21)', $list);
}

/** @param list<int> $list */
function testAppendImmediatelyAfterLastElement(array $list): void
{
assertType('list<int>', $list);
$list[0] = 17;
assertType('non-empty-list<int>&hasOffsetValue(0, 17)', $list);
$list[1] = 19;
assertType('non-empty-list<int>&hasOffsetValue(0, 17)&hasOffsetValue(1, 19)', $list);
$list[2] = 21;
assertType('non-empty-list<int>&hasOffsetValue(0, 17)&hasOffsetValue(1, 19)&hasOffsetValue(2, 21)', $list);
$list[3] = 21;
assertType('non-empty-list<int>&hasOffsetValue(0, 17)&hasOffsetValue(1, 19)&hasOffsetValue(2, 21)&hasOffsetValue(3, 21)', $list);

// hole in the list -> turns it into a array

$list[5] = 21;
assertType('non-empty-array<int<0, max>, int>&hasOffsetValue(0, 17)&hasOffsetValue(1, 19)&hasOffsetValue(2, 21)&hasOffsetValue(3, 21)&hasOffsetValue(5, 21)', $list);
}


/** @param list<int> $list */
function testKeepListAfterLast(array $list): void
{
if (isset($list[5])) {
assertType('non-empty-list<int>&hasOffsetValue(5, int)', $list);
$list[6] = 21;
assertType('non-empty-list<int>&hasOffsetValue(5, int)&hasOffsetValue(6, 21)', $list);
}
assertType('list<int>', $list);
}

/** @param list<int> $list */
function testKeepListAfterLastArrayKey(array $list): void
{
if (array_key_exists(5, $list) && is_int($list[5])) {
assertType('non-empty-list<int>&hasOffsetValue(5, int)', $list);
$list[6] = 21;
assertType('non-empty-list<int>&hasOffsetValue(5, int)&hasOffsetValue(6, 21)', $list);
}
assertType('list<int>', $list);
}
}
Loading