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
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 ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
Copy link
Contributor Author

@herndlm herndlm Apr 18, 2025

Choose a reason for hiding this comment

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

Could be replaced with return $this->getValuesArray(); if it should be more DRY. I wasn't sure, but this felt a bit less "hidden" to me.

}

return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function fromMixed($arr): void
public function normalArrays(array $arr): void
{
/** @var array<int, bool> $arr */
assertType('array<int, bool>', array_slice($arr, 1, 2));
assertType('list<bool>', array_slice($arr, 1, 2));
assertType('array<int, bool>', array_slice($arr, 1, 2, true));

/** @var array<string, int> $arr */
Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/bug-10721.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public function listVariants(): void
public function arrayVariants(array $strings, $maybeZero): void
{
assertType("array<int, string>", $strings);
assertType("array<int, string>", array_slice($strings, 0));
assertType("array<int, string>", array_slice($strings, 1));
assertType("array<int, string>", array_slice($strings, $maybeZero));
assertType("list<string>", array_slice($strings, 0));
assertType("list<string>", array_slice($strings, 1));
assertType("list<string>", array_slice($strings, $maybeZero));

if (count($strings) > 0) {
assertType("non-empty-array<int, string>", $strings);
assertType("non-empty-array<int, string>", array_slice($strings, 0));
assertType("array<int, string>", array_slice($strings, 1));
assertType("array<int, string>", array_slice($strings, $maybeZero));
assertType("non-empty-list<string>", array_slice($strings, 0));
assertType("list<string>", array_slice($strings, 1));
assertType("list<string>", array_slice($strings, $maybeZero));
}
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3606,4 +3606,14 @@ public function testBu12793(): void
$this->analyse([__DIR__ . '/data/bug-12793.php'], []);
}

public function testBug12880(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-12880.php'], []);
}

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

namespace Bug12880;

class HelloWorld
{
public function sayHello(): void
{
$this->test([1, 2, 3, true]);
}

/**
* @param list<mixed> $ids
*/
private function test(array $ids): void
{
$ids = array_unique($ids);
\PHPStan\dumpType($ids);
$ids = array_slice($ids, 0, 5);
\PHPStan\dumpType($ids);
$this->expectList($ids);
}

/**
* @param list<mixed> $ids
*/
private function expectList(array $ids): void
{
var_dump($ids);
}
}
Loading