-
Notifications
You must be signed in to change notification settings - Fork 545
add array_first and array_last return type extensions #4499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\NullType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use function count; | ||
|
|
||
| #[AutowiredService] | ||
| final class ArrayFirstDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
| { | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_first' && $functionReflection->isBuiltin(); | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||
| { | ||
| $args = $functionCall->getArgs(); | ||
|
|
||
| if (count($args) < 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $argType = $scope->getType($args[0]->value); | ||
| $iterableAtLeastOnce = $argType->isIterableAtLeastOnce(); | ||
|
|
||
| if ($iterableAtLeastOnce->no()) { | ||
| return new NullType(); | ||
| } | ||
|
|
||
| $valueType = $argType->getFirstIterableValueType(); | ||
|
||
|
|
||
| if ($iterableAtLeastOnce->yes()) { | ||
| return $valueType; | ||
| } | ||
|
|
||
| return TypeCombinator::union($valueType, new NullType()); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\NullType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use function count; | ||
|
|
||
| #[AutowiredService] | ||
| final class ArrayLastDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
|
||
| { | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_last' && $functionReflection->isBuiltin(); | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||
| { | ||
| $args = $functionCall->getArgs(); | ||
|
|
||
| if (count($args) < 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $argType = $scope->getType($args[0]->value); | ||
| $iterableAtLeastOnce = $argType->isIterableAtLeastOnce(); | ||
|
|
||
| if ($iterableAtLeastOnce->no()) { | ||
| return new NullType(); | ||
| } | ||
|
|
||
| $valueType = $argType->getLastIterableValueType(); | ||
|
|
||
| if ($iterableAtLeastOnce->yes()) { | ||
| return $valueType; | ||
| } | ||
|
|
||
| return TypeCombinator::union($valueType, new NullType()); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php // lint >= 8.5 | ||
|
|
||
| namespace ArrayFirstLast; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param string[] $stringArray | ||
| * @param non-empty-array<int, string> $nonEmptyArray | ||
| */ | ||
| function doFoo(array $stringArray, array $nonEmptyArray, $mixed): void | ||
| { | ||
| assertType("'a'", array_first([1 => 'a', 0 => 'b', 2 => 'c'])); | ||
| assertType('string|null', array_first($stringArray)); | ||
| assertType('string', array_first($nonEmptyArray)); | ||
| assertType('mixed', array_first($mixed)); | ||
|
|
||
| assertType("'c'", array_last([1 => 'a', 0 => 'b', 2 => 'c'])); | ||
| assertType('string|null', array_last($stringArray)); | ||
| assertType('string', array_last($nonEmptyArray)); | ||
| assertType('mixed', array_last($mixed)); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the correct way. Maybe we can check the PHP version. Because I thought there might be userland functions named
array_firstthat would also be processed here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay like this, we don't check this anywhere for similar extensions. As a benefit it'd also work for polyfills running on lower PHP versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So should I remove the
$functionReflection->isBuiltin()part?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes 😊