Skip to content

Fix array_slice() edge cases #3959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
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
5 changes: 1 addition & 4 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ public function shuffleArray(): Type

public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
{
if (
(new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()
&& ($lengthType->isNull()->yes() || IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($lengthType)->yes())
) {
if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes()) {
return $this;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ public function shuffleArray(): Type

public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
{
if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
return new ConstantArrayType([], []);
}

if ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
}
Expand Down
20 changes: 15 additions & 5 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,17 +944,27 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
->sliceArray($offsetType, $lengthType, $preserveKeys);
}

if ($keyTypesCount + $offset <= 0) {
// A negative offset cannot reach left outside the array twice
$offset = 0;
}

if ($keyTypesCount + $length <= 0) {
// A negative length cannot reach left outside the array twice
$length = 0;
}

if ($length === 0 || ($offset < 0 && $length < 0 && $offset - $length >= 0)) {
// 0 / 0, 3 / 0 or e.g. -3 / -3 or -3 / -4 and so on never extract anything
return new self([], []);
}

if ($length < 0) {
// Negative lengths prevent access to the most right n elements
return $this->removeLastElements($length * -1)
->sliceArray($offsetType, new NullType(), $preserveKeys);
}

if ($keyTypesCount + $offset <= 0) {
// A negative offset cannot reach left outside the array
$offset = 0;
}

if ($offset < 0) {
/*
* Transforms the problem with the negative offset in one with a positive offset using array reversion.
Expand Down
13 changes: 12 additions & 1 deletion src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,18 @@ public function shuffleArray(): Type

public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this->intersectTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys));
$result = $this->intersectTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys));

if (
$this->isList()->yes()
&& $this->isIterableAtLeastOnce()->yes()
&& (new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()
&& IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($lengthType)->yes()
) {
$result = TypeCombinator::intersect($result, new NonEmptyArrayType());
}

return $result;
}

public function getEnumCases(): array
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public function normalArrays(array $arr): void
/** @var array<string, int> $arr */
assertType('array<string, int>', array_slice($arr, 1, 2));
assertType('array<string, int>', array_slice($arr, 1, 2, true));

/** @var non-empty-array<string> $arr */
assertType('array{}', array_slice($arr, 0, 0));
assertType('array{}', array_slice($arr, 0, 0, true));

/** @var non-empty-array<string> $arr */
assertType('array<string>', array_slice($arr, 0, 1));
assertType('array<string>', array_slice($arr, 0, 1, true));

/** @var list<string> $arr */
assertType('list<string>', array_slice($arr, 0, 1));
assertType('list<string>', array_slice($arr, 0, 1, true));

/** @var non-empty-list<string> $arr */
assertType('non-empty-list<string>', array_slice($arr, 0, 1));
assertType('non-empty-list<string>', array_slice($arr, 0, 1, true));
}

public function constantArrays(array $arr): void
Expand All @@ -48,6 +64,14 @@ public function constantArrays(array $arr): void
/** @var array{17: 'foo', 19: 'bar', 21: 'baz'}|array{foo: 17, bar: 19, baz: 21} $arr */
assertType('array{\'bar\', \'baz\'}|array{bar: 19, baz: 21}', array_slice($arr, 1, 2));
assertType('array{19: \'bar\', 21: \'baz\'}|array{bar: 19, baz: 21}', array_slice($arr, 1, 2, true));

/** @var array{17: 'foo', b: 'bar', 19: 'baz'} $arr */
assertType('array{}', array_slice($arr, -1, -1));
assertType('array{}', array_slice($arr, -1, -1, true));

/** @var array{17: 'foo', b: 'bar', 19: 'baz'} $arr */
assertType('array{}', array_slice($arr, -1, -2));
assertType('array{}', array_slice($arr, -1, -2, true));
}

public function constantArraysWithOptionalKeys(array $arr): void
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-5017.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function doFoo()
assertType('non-empty-array<0|1|2|3|4, 0|1|2|3|4>', $items);
$batch = array_splice($items, 0, 2);
assertType('array<0|1|2|3|4, 0|1|2|3|4>', $items);
assertType('non-empty-list<0|1|2|3|4>', $batch);
assertType('list<0|1|2|3|4>', $batch);
Copy link
Contributor Author

@herndlm herndlm Apr 21, 2025

Choose a reason for hiding this comment

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

this is unfortunate, but it happens because the $items type is not as good as it can be. This will be improved in #3952 and then it will be non-empty-list again.

So there's unfortunately some kind of coupling here.

}
}

Expand All @@ -28,7 +28,7 @@ public function doBar($items)
assertType('non-empty-array<int>', $items);
$batch = array_splice($items, 0, 2);
assertType('array<int>', $items);
assertType('non-empty-array<int>', $batch);
assertType('array<int>', $batch);
}
}

Expand Down
Loading