Skip to content

Commit 73651b5

Browse files
committed
use getIterableValueType
1 parent 4e29e4e commit 73651b5

File tree

3 files changed

+10
-54
lines changed

3 files changed

+10
-54
lines changed

src/Type/Php/ArrayFirstDynamicReturnTypeExtension.php renamed to src/Type/Php/ArrayFirstLastDynamicReturnTypeExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
use PHPStan\Type\Type;
1212
use PHPStan\Type\TypeCombinator;
1313
use function count;
14+
use function in_array;
1415

1516
#[AutowiredService]
16-
final class ArrayFirstDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
17+
final class ArrayFirstLastDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
1718
{
1819

1920
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2021
{
21-
return $functionReflection->getName() === 'array_first' && $functionReflection->isBuiltin();
22+
return in_array($functionReflection->getName(), ['array_first', 'array_last'], true);
2223
}
2324

2425
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
@@ -36,7 +37,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3637
return new NullType();
3738
}
3839

39-
$valueType = $argType->getFirstIterableValueType();
40+
$valueType = $argType->getIterableValueType();
4041

4142
if ($iterableAtLeastOnce->yes()) {
4243
return $valueType;

src/Type/Php/ArrayLastDynamicReturnTypeExtension.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/PHPStan/Analyser/nsrt/array_first_last.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
/**
88
* @param string[] $stringArray
99
* @param non-empty-array<int, string> $nonEmptyArray
10+
* @param array{1: 'bar', baz: 'foo'} $arrayShape
1011
*/
11-
function doFoo(array $stringArray, array $nonEmptyArray, $mixed): void
12+
function doFoo(array $stringArray, array $nonEmptyArray, $mixed, $arrayShape): void
1213
{
13-
assertType("'a'", array_first([1 => 'a', 0 => 'b', 2 => 'c']));
14+
assertType("'a'|'b'|'c'", array_first([1 => 'a', 0 => 'b', 2 => 'c']));
15+
assertType("'bar'|'foo'", array_first($arrayShape));
1416
assertType('string|null', array_first($stringArray));
1517
assertType('string', array_first($nonEmptyArray));
1618
assertType('mixed', array_first($mixed));
1719

18-
assertType("'c'", array_last([1 => 'a', 0 => 'b', 2 => 'c']));
20+
assertType("'a'|'b'|'c'", array_last([1 => 'a', 0 => 'b', 2 => 'c']));
21+
assertType("'bar'|'foo'", array_last($arrayShape));
1922
assertType('string|null', array_last($stringArray));
2023
assertType('string', array_last($nonEmptyArray));
2124
assertType('mixed', array_last($mixed));

0 commit comments

Comments
 (0)