Skip to content

Commit 4b7b27d

Browse files
jlherrenondrejmirtes
authored andcommitted
Fix array_slice() with mixed argument
It was previously returning *NEVER*.
1 parent 3c25418 commit 4b7b27d

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

src/Type/Php/ArraySliceFunctionReturnTypeExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ public function getTypeFromFunctionCall(
6161

6262
$constantArrays = TypeUtils::getConstantArrays($valueType);
6363
if (count($constantArrays) === 0) {
64-
return TypeCombinator::union(...TypeUtils::getArrays($valueType));
64+
$arrays = TypeUtils::getArrays($valueType);
65+
if (count($arrays) !== 0) {
66+
return TypeCombinator::union(...$arrays);
67+
}
68+
return new ArrayType(
69+
new MixedType(),
70+
new MixedType()
71+
);
6572
}
6673

6774
if (isset($functionCall->args[3])) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10229,9 +10229,9 @@ public function dataBug3985(): array
1022910229
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3985.php');
1023010230
}
1023110231

10232-
public function dataArraySliceNonEmpty(): array
10232+
public function dataArraySlice(): array
1023310233
{
10234-
return $this->gatherAssertTypes(__DIR__ . '/data/array-slice-non-empty.php');
10234+
return $this->gatherAssertTypes(__DIR__ . '/data/array-slice.php');
1023510235
}
1023610236

1023710237
public function dataBug3990(): array
@@ -10347,7 +10347,7 @@ public function dataBug4016(): array
1034710347
* @dataProvider dataBug2816
1034810348
* @dataProvider dataBug2816Two
1034910349
* @dataProvider dataBug3985
10350-
* @dataProvider dataArraySliceNonEmpty
10350+
* @dataProvider dataArraySlice
1035110351
* @dataProvider dataBug3990
1035210352
* @dataProvider dataBug3991
1035310353
* @dataProvider dataBug3993

tests/PHPStan/Analyser/data/array-slice-non-empty.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ArraySlice;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param non-empty-array $a
12+
*/
13+
public function nonEmpty(array $a): void
14+
{
15+
assertType('array', array_slice($a, 1));
16+
}
17+
18+
/**
19+
* @param mixed $arr
20+
*/
21+
public function fromMixed($arr): void
22+
{
23+
assertType('array', array_slice($arr, 1, 2));
24+
}
25+
26+
/**
27+
* @param array<int, bool> $arr1
28+
* @param array<string, int> $arr2
29+
*/
30+
public function preserveTypes(array $arr1, array $arr2): void
31+
{
32+
assertType('array<int, bool>', array_slice($arr1, 1, 2));
33+
assertType('array<int, bool>', array_slice($arr1, 1, 2, true));
34+
assertType('array<string, int>', array_slice($arr2, 1, 2));
35+
assertType('array<string, int>', array_slice($arr2, 1, 2, true));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)