Skip to content

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