Skip to content

array_slice doesn't convert array<int, mixed> to list #12880

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
Feolius opened this issue Apr 15, 2025 · 2 comments · Fixed by phpstan/phpstan-src#3946
Closed

array_slice doesn't convert array<int, mixed> to list #12880

Feolius opened this issue Apr 15, 2025 · 2 comments · Fixed by phpstan/phpstan-src#3946
Milestone

Comments

@Feolius
Copy link

Feolius commented Apr 15, 2025

Bug report

In array_slice docs it is said

array_slice() will reorder and reset the integer array indices by default. This behaviour can be changed by setting preserve_keys to true.

So, if preserve_keys flag is set to false (default) and array has integer keys then result of array_slice must be a list.

In the code snippet I used array_unique function to get array<int<0, max>, mixed>. After passing result to array_slice with no preserve_keys flags set, I expect no errors.

Code snippet that reproduces the problem

https://phpstan.org/r/95d7c1ee-199e-4ba8-9940-40c0c932d290

Expected output

No errors must be reported

Did PHPStan help you today? Did it make you happy in any way?

Yes, it helps a LOT. Thanks for all your work.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Apr 15, 2025
@VincentLanglet
Copy link
Contributor

Do you want to try fixing it @Feolius ?

I think ArrayType::sliceArray need to be updated to add new AccessoryArrayListType() when $preserveKeys->isTrue()->yes().
https://github.com/phpstan/phpstan-src/blob/db66d83f6ae3dadec529dd53bcde250344c2230f/src/Type/ArrayType.php#L443-L446

I assume the same is needed for NonEmptyArrayType and OversizedArrayType (?).

@stof
Copy link
Contributor

stof commented Apr 17, 2025

this is more complex than that, when you account for arrays where the key type is inferred as a subtype of int (for instance 12|15|16).
This is kinda related to #11917

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

Successfully merging a pull request may close this issue.

4 participants