Skip to content

Add Type::popArray() and Type::shiftArray()#1847

Merged
ondrejmirtes merged 2 commits intophpstan:1.9.xfrom
herndlm:pop-array-shift-array
Oct 16, 2022
Merged

Add Type::popArray() and Type::shiftArray()#1847
ondrejmirtes merged 2 commits intophpstan:1.9.xfrom
herndlm:pop-array-shift-array

Conversation

@herndlm
Copy link
Copy Markdown
Contributor

@herndlm herndlm commented Oct 14, 2022

This is a boring one, but at least I could get rid of the TypeTraverser usage. I should have never added that..

I wasn't sure at first, but indeed, array_shift re-indexes the array and preserves the list.

@herndlm
Copy link
Copy Markdown
Contributor Author

herndlm commented Oct 14, 2022

oh, this is more interesting after all..

here are 2 failures

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this fixes the first failure, see also #1849
at least here removing it causes a test failure. but as mentioned there - I did not fully understand it tbh, it was do-while specific though

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I guess it makes sense, the same thing is done ($bodyScope = $bodyScope->mergeWith($scope);) before the final analysis on line 978.

@herndlm herndlm marked this pull request as ready for review October 15, 2022 13:02
@herndlm herndlm force-pushed the pop-array-shift-array branch from 48b3e22 to 5506009 Compare October 15, 2022 14:45
$arrayArg,
$arrayArgType,
$functionReflection->getName() === 'array_pop' ? $arrayArgType->popArray() : $arrayArgType->shiftArray(),
$scope->getNativeType($arrayArg),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel like we should get the native array type and call the same method on it too.

@ondrejmirtes ondrejmirtes merged commit 05c4137 into phpstan:1.9.x Oct 16, 2022
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you.

@herndlm herndlm deleted the pop-array-shift-array branch October 16, 2022 12:07
@staabm
Copy link
Copy Markdown
Contributor

staabm commented Oct 16, 2022

@herndlm did you intentiomally not use the new Type methods in the array-pop and array-shift return type extension?

@herndlm
Copy link
Copy Markdown
Contributor Author

herndlm commented Oct 16, 2022

Yes, because those return the first or last element. I mixed this up recently as well :)

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.

3 participants