Skip to content

Commit 273b3ce

Browse files
committed
Fix array_slice with non-empty-array
1 parent 0dee4c3 commit 273b3ce

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Type/Php/ArraySliceFunctionReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getTypeFromFunctionCall(
6161

6262
$constantArrays = TypeUtils::getConstantArrays($valueType);
6363
if (count($constantArrays) === 0) {
64-
return $valueType;
64+
return TypeCombinator::union(...TypeUtils::getArrays($valueType));
6565
}
6666

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

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -10225,6 +10225,11 @@ public function dataBug3985(): array
1022510225
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3985.php');
1022610226
}
1022710227

10228+
public function dataArraySliceNonEmpty(): array
10229+
{
10230+
return $this->gatherAssertTypes(__DIR__ . '/data/array-slice-non-empty.php');
10231+
}
10232+
1022810233
/**
1022910234
* @dataProvider dataBug2574
1023010235
* @dataProvider dataBug2577
@@ -10312,6 +10317,7 @@ public function dataBug3985(): array
1031210317
* @dataProvider dataBug2816
1031310318
* @dataProvider dataBug2816Two
1031410319
* @dataProvider dataBug3985
10320+
* @dataProvider dataArraySliceNonEmpty
1031510321
* @param string $assertType
1031610322
* @param string $file
1031710323
* @param mixed ...$args
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ArraySliceNonEmpty;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param non-empty-array $a
12+
*/
13+
public function doFoo(array $a): void
14+
{
15+
assertType('array', array_slice($a, 1));
16+
}
17+
18+
}

0 commit comments

Comments
 (0)