diff --git a/src/Type/ArrayType.php b/src/Type/ArrayType.php index 68224a0f9b..e68a6a61d3 100644 --- a/src/Type/ArrayType.php +++ b/src/Type/ArrayType.php @@ -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()); + } + return $this; } diff --git a/tests/PHPStan/Analyser/nsrt/array-slice.php b/tests/PHPStan/Analyser/nsrt/array-slice.php index 847f535df1..79deb5576e 100644 --- a/tests/PHPStan/Analyser/nsrt/array-slice.php +++ b/tests/PHPStan/Analyser/nsrt/array-slice.php @@ -30,7 +30,7 @@ public function fromMixed($arr): void public function normalArrays(array $arr): void { /** @var array $arr */ - assertType('array', array_slice($arr, 1, 2)); + assertType('list', array_slice($arr, 1, 2)); assertType('array', array_slice($arr, 1, 2, true)); /** @var array $arr */ diff --git a/tests/PHPStan/Analyser/nsrt/bug-10721.php b/tests/PHPStan/Analyser/nsrt/bug-10721.php index 52d511c163..c82d2298f2 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-10721.php +++ b/tests/PHPStan/Analyser/nsrt/bug-10721.php @@ -86,15 +86,15 @@ public function listVariants(): void public function arrayVariants(array $strings, $maybeZero): void { assertType("array", $strings); - assertType("array", array_slice($strings, 0)); - assertType("array", array_slice($strings, 1)); - assertType("array", array_slice($strings, $maybeZero)); + assertType("list", array_slice($strings, 0)); + assertType("list", array_slice($strings, 1)); + assertType("list", array_slice($strings, $maybeZero)); if (count($strings) > 0) { assertType("non-empty-array", $strings); - assertType("non-empty-array", array_slice($strings, 0)); - assertType("array", array_slice($strings, 1)); - assertType("array", array_slice($strings, $maybeZero)); + assertType("non-empty-list", array_slice($strings, 0)); + assertType("list", array_slice($strings, 1)); + assertType("list", array_slice($strings, $maybeZero)); } } } diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 23a8d6194e..bc177e9d0b 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -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'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-12880.php b/tests/PHPStan/Rules/Methods/data/bug-12880.php new file mode 100644 index 0000000000..b82ecb820e --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-12880.php @@ -0,0 +1,31 @@ +test([1, 2, 3, true]); + } + + /** + * @param list $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 $ids + */ + private function expectList(array $ids): void + { + var_dump($ids); + } +}