add array_first and array_last return type extensions#4499
add array_first and array_last return type extensions#4499ondrejmirtes merged 3 commits intophpstan:2.1.xfrom
Conversation
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_first' && $functionReflection->isBuiltin(); |
There was a problem hiding this comment.
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_first that would also be processed here.
There was a problem hiding this comment.
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.
So should I remove the $functionReflection->isBuiltin() part?
| use function count; | ||
|
|
||
| #[AutowiredService] | ||
| final class ArrayLastDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
There was a problem hiding this comment.
as this 2 extensions are very similar, you might consider using a single extension class for handling both
There was a problem hiding this comment.
With the recent refactoring they are now merged into one
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_first' && $functionReflection->isBuiltin(); |
There was a problem hiding this comment.
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.
| return new NullType(); | ||
| } | ||
|
|
||
| $valueType = $argType->getFirstIterableValueType(); |
There was a problem hiding this comment.
When I thought about the implementation, I'd do only getIterableValueType. The getFirstIterable*Type methods are highly misleading and we should get rid of it. Because array shapes do not enforce the order of the keys. Sorry to put more burden on you, but can you please deprecate Type::getFirstIterableKeyType, getLastIterableKeyType, getFirstIterableValueType, getLastIterableValueType and use getIterableKeyType and getIterableValueType in all places instead? And just update any test assertions because of that. Thank you!
There was a problem hiding this comment.
Alright. I can do that. But we will lose the precise type for constant arrays that way.
There was a problem hiding this comment.
Yeah, that's okay. Ideally we would have two separate implementations for literal arrays and PHPDoc array shapes but we don't.
|
Awesome, looks like it fixed phpstan/phpstan#8270. Please add a regression test. |
I'm not sure what got fixed. For PHP 7.3 - 8.2 there are no errors already. And for < 7.3 it doesn't make sense because |
see https://github.com/phpstan/phpstan-src/actions/runs/18969820143 looks like it fixed the "If condition is always true." error |
|
But that is already not reported https://phpstan.org/r/b63fa8a0-6ca6-4ac4-a8ca-b0a76cb22494 🤔 |
|
I think it is: https://phpstan.org/r/84f65522-eace-4181-90c1-98c6a0f85206 |
|
I don't understand it fully, but I did this 3530dd2 Though it still fails locally 🤷🏽 |
|
Probably not fixed then and we're just misinterpreting the results somehow 😊 |
3530dd2 to
5f3237a
Compare
|
Reverted then. |
|
Thank you! |
This PR adds return type extensions for
array_firstandarray_last