Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Acknowledge that array<string, something> may still have integer keys #769

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

Closed
dktapps wants to merge 6 commits into phpstan:master from pmmp:array-type-keys
Closed

Acknowledge that array<string, something> may still have integer keys #769

dktapps wants to merge 6 commits into phpstan:master from pmmp:array-type-keys

Conversation

@dktapps
Copy link
Contributor

@dktapps dktapps commented Nov 14, 2021

This "feature" is extremely annoying to me, more so because PHPStan will complain if I cast a key that came from iterating over an array<string, something>.

Personally, I think this should not be benevolent at all - it's bit me on the ass more times than I can count, and I would much appreciate if PHPStan reported it properly.
But in keeping with the rest of the code, and to just shut it up about useless casts, I opted for the BUT.

Opening this to get CI feedback since it's a pain to run the tests on Windows.

Copy link
Contributor

staabm commented Nov 15, 2021
edited
Loading

could you create a snippet on phpstan.org to show a example when a array<string, ...> can contain an int?

Opening this to get CI feedback since it's a pain to run the tests on Windows.

agree have similar issues on my end.

Copy link
Contributor Author

dktapps commented Nov 15, 2021

@staabm it's really easy to reproduce. Use a numeric string as an array key and it gets casted to int.

I limited the change to getIterableKeyType() since iteration seems to be the only place where it's actually a problem. The rest of the time the key casting doesn't matter, but for iteration it's a major pain in the ass and the cause of a significant percentage of PM crashes.

Copy link
Contributor Author

dktapps commented Nov 15, 2021

On local, I changed this to use a non-benevolent union type, and discovered 25 places in PM where this problem could legitimately occur. I really think this should be treated with non-benevolence.

dktapps added a commit to pmmp/PocketMine-MP that referenced this pull request Nov 15, 2021
this is not as good as phpstan/phpstan-src#769 (e.g. array_key_first()/array_key_last() aren't covered by this, nor is array_rand()) but it does eliminate the most infuriating cases where this usually crops up.
Copy link
Contributor

This is indeed true. TIL. https://3v4l.org/o7VWB

Copy link
Contributor Author

dktapps commented Nov 17, 2021
edited
Loading

If you use strict_types=1 it will bite you on the ass at some point. It's very annoying to me that PHPStan doesn't report the problem. I ended up writing a custom rule to ban direct iteration on arrays with string keys (see pmmp/PocketMine-MP@269231c) but it doesn't completely solve the problem (e.g. array_key_first/last and array_rand() will still bite me at some point).

Copy link
Member

This is a hard problem and I don't want to make all of these errors rain on PHPStan's users...

Copy link
Contributor

It would be good to at least acknowledge this shortcoming in the docs somewhere though, right?

@dktapps Would you consider making your custom rule available as a separate extension? I might want to use it in my own project.

Copy link
Contributor Author

dktapps commented Jan 31, 2022

It would be good to at least acknowledge this shortcoming in the docs somewhere though, right?

@dktapps Would you consider making your custom rule available as a separate extension? I might want to use it in my own project.

I guess I can do that.

ZebulanStanphill reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /