-
Notifications
You must be signed in to change notification settings - Fork 544
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
Conversation
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_first' && $functionReflection->isBuiltin(); |
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_first that 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 😊
| use function count; | ||
|
|
||
| #[AutowiredService] | ||
| final class ArrayLastDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
| return new NullType(); | ||
| } | ||
|
|
||
| $valueType = $argType->getFirstIterableValueType(); |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright. I can do that. But we will lose the precise type for constant arrays that way.
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.
Yeah, that's okay. Ideally we would have two separate implementations for literal arrays and PHPDoc array shapes but we don't.
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.
|
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