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

usort changes type of passed variable, phpstan not picking it up #3312

Closed
d-claassen opened this issue May 19, 2020 · 4 comments
Closed

usort changes type of passed variable, phpstan not picking it up #3312

d-claassen opened this issue May 19, 2020 · 4 comments
Labels
Milestone

Comments

@d-claassen
Copy link

Bug report

usort accepts a passed-by-reference array of array<array-key, T> as first argument, but will always transform that argument into array<int, T>. This is also noted in the php docs:

Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.

So when giving usort an array of array<string, T> the passed array becomes array<int, T>. PHPStan however does not pick this up. I looked at updating the functionMap.php file, but I did not see how this can be documented in the current format. If I missed something I'd be happy to provide a pull request with an update to it!

Code snippet that reproduces the problem

The snippet that reproduces can be found here:
https://phpstan.org/r/cd33a591-5f93-4df3-b878-49af85ef487e

I've also confirmed the behavior as described in the php docs with the same code in this 3v4l.org test.

Expected output

I would've expected PHPStan not to return an error. Both Psalm and Phan do not return any error about this.

@ondrejmirtes
Copy link
Member

Hi, never heard about this, didn't know that usort changes these keys. This would have to be implemented somewhere around these lines: https://github.com/phpstan/phpstan-src/blob/b07b33d2ff9b888b8c7baa1ff64d4aa71624cf99/src/Analyser/NodeScopeResolver.php#L1384-L1435

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone May 19, 2020
@MidnightDesign
Copy link
Contributor

Small correction: the output type should be list<T>, not just array<int, T>. https://3v4l.org/P5COJ

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2891

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants