add array_first and array_last return type extensions by canvural · Pull Request #4499 · phpstan/phpstan-src
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_first' && $functionReflection->isBuiltin(); |
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.
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.
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?
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 |
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
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(); |
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(); |
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!
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.
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.
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 array_key_first doesn't exist there.
I don't understand it fully, but I did this 3530dd2 Though it still fails locally 🤷🏽
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters