Skip to content

Commit 9015e3b

Browse files
herndlmondrejmirtes
authored andcommitted
Handle preserve_keys in array_slice() for normal arrays
1 parent 0d9c974 commit 9015e3b

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

Diff for: src/Type/ArrayType.php

+4
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ public function shuffleArray(): Type
442442

443443
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
444444
{
445+
if ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
446+
return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
447+
}
448+
445449
return $this;
446450
}
447451

Diff for: tests/PHPStan/Analyser/nsrt/array-slice.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function fromMixed($arr): void
3030
public function normalArrays(array $arr): void
3131
{
3232
/** @var array<int, bool> $arr */
33-
assertType('array<int, bool>', array_slice($arr, 1, 2));
33+
assertType('list<bool>', array_slice($arr, 1, 2));
3434
assertType('array<int, bool>', array_slice($arr, 1, 2, true));
3535

3636
/** @var array<string, int> $arr */

Diff for: tests/PHPStan/Analyser/nsrt/bug-10721.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public function listVariants(): void
8686
public function arrayVariants(array $strings, $maybeZero): void
8787
{
8888
assertType("array<int, string>", $strings);
89-
assertType("array<int, string>", array_slice($strings, 0));
90-
assertType("array<int, string>", array_slice($strings, 1));
91-
assertType("array<int, string>", array_slice($strings, $maybeZero));
89+
assertType("list<string>", array_slice($strings, 0));
90+
assertType("list<string>", array_slice($strings, 1));
91+
assertType("list<string>", array_slice($strings, $maybeZero));
9292

9393
if (count($strings) > 0) {
9494
assertType("non-empty-array<int, string>", $strings);
95-
assertType("non-empty-array<int, string>", array_slice($strings, 0));
96-
assertType("array<int, string>", array_slice($strings, 1));
97-
assertType("array<int, string>", array_slice($strings, $maybeZero));
95+
assertType("non-empty-list<string>", array_slice($strings, 0));
96+
assertType("list<string>", array_slice($strings, 1));
97+
assertType("list<string>", array_slice($strings, $maybeZero));
9898
}
9999
}
100100
}

Diff for: tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -3606,4 +3606,14 @@ public function testBu12793(): void
36063606
$this->analyse([__DIR__ . '/data/bug-12793.php'], []);
36073607
}
36083608

3609+
public function testBug12880(): void
3610+
{
3611+
$this->checkThisOnly = false;
3612+
$this->checkNullables = true;
3613+
$this->checkUnionTypes = true;
3614+
$this->checkExplicitMixed = true;
3615+
3616+
$this->analyse([__DIR__ . '/data/bug-12880.php'], []);
3617+
}
3618+
36093619
}

Diff for: tests/PHPStan/Rules/Methods/data/bug-12880.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12880;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(): void
8+
{
9+
$this->test([1, 2, 3, true]);
10+
}
11+
12+
/**
13+
* @param list<mixed> $ids
14+
*/
15+
private function test(array $ids): void
16+
{
17+
$ids = array_unique($ids);
18+
\PHPStan\dumpType($ids);
19+
$ids = array_slice($ids, 0, 5);
20+
\PHPStan\dumpType($ids);
21+
$this->expectList($ids);
22+
}
23+
24+
/**
25+
* @param list<mixed> $ids
26+
*/
27+
private function expectList(array $ids): void
28+
{
29+
var_dump($ids);
30+
}
31+
}

0 commit comments

Comments
 (0)