Skip to content
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

Fix ArrayColumnFunctionReturnTypeExtension producing array key types #1033

Merged
merged 2 commits into from Mar 2, 2022

Conversation

jlherren
Copy link
Contributor

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also - what happens when the column isn't a valid array key? Maybe an opportunity for a new rule later?

{
$isArray = $type->isArray();
if ($isArray->yes()) {
return new IntegerType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it. Array gets cast to integer?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://sandbox.onlinephpfunctions.com/code/381d0182b85cc698b02b18b82cd9dae0da3f7a00
I intentionally used 'foo' instead of 'type' in the array_column call in this example. So this seems to be the behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly. In PHP < 8, whenever a value cannot be used as an array key (either because it is an array, or the value is missing entirely), the next available integer array key is used (as if you'd do $arr[] = $value). In PHP >= 8, using an array value as key causes a fatal error, which of course is an opportunity for PHPStan to detect in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I miss an if for this behaviour in this extension. Feel free to add in a future PR :)

@ondrejmirtes ondrejmirtes merged commit b384384 into phpstan:master Mar 2, 2022
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants