Skip to content

Commit

Permalink
Fix array_slice with non-empty-array
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 23, 2020
1 parent 0dee4c3 commit 273b3ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Php/ArraySliceFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getTypeFromFunctionCall(

$constantArrays = TypeUtils::getConstantArrays($valueType);
if (count($constantArrays) === 0) {
return $valueType;
return TypeCombinator::union(...TypeUtils::getArrays($valueType));
}

if (isset($functionCall->args[3])) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10225,6 +10225,11 @@ public function dataBug3985(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3985.php');
}

public function dataArraySliceNonEmpty(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/array-slice-non-empty.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -10312,6 +10317,7 @@ public function dataBug3985(): array
* @dataProvider dataBug2816
* @dataProvider dataBug2816Two
* @dataProvider dataBug3985
* @dataProvider dataArraySliceNonEmpty
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/data/array-slice-non-empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ArraySliceNonEmpty;

use function PHPStan\Analyser\assertType;

class Foo
{

/**
* @param non-empty-array $a
*/
public function doFoo(array $a): void
{
assertType('array', array_slice($a, 1));
}

}

0 comments on commit 273b3ce

Please sign in to comment.