Skip to content

Handle preserve_keys in array_slice() for normal arrays #3946

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

Merged
merged 1 commit into from
Apr 19, 2025

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Apr 18, 2025

Closes phpstan/phpstan#12880

https://www.php.net/manual/en/function.array-slice.php

array_slice() will reorder and reset the integer array indices by default. This behaviour can be changed by setting preserve_keys to true. String keys are always preserved, regardless of this parameter.

https://3v4l.org/7njtg

@@ -442,6 +442,10 @@ public function shuffleArray(): Type

public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
{
if ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
Copy link
Contributor Author

@herndlm herndlm Apr 18, 2025

Choose a reason for hiding this comment

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

Could be replaced with return $this->getValuesArray(); if it should be more DRY. I wasn't sure, but this felt a bit less "hidden" to me.

@ondrejmirtes ondrejmirtes merged commit 9015e3b into phpstan:2.1.x Apr 19, 2025
417 of 418 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@herndlm herndlm deleted the array-slice-preserve-keys branch April 19, 2025 12:08
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 this pull request may close these issues.

array_slice doesn't convert array<int, mixed> to list
2 participants