Skip to content

Retain list type via array_push() and array_unshift() #2451

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

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Jun 11, 2023

Closes phpstan/phpstan#8449

it has been a while :) I know I have still 2 open PRs, but I didn't feel like finishing them yet. soon™

I was wondering why the array here is even re-created like that. The reason is that this is after the isIterableAtLeastOnce / optional check and the re-creation is just there to remove a NonEmptyArrayType. So if there would be a simpler/better way of removing it (maybe calling ->popArray() but I'm not sure, that feels like a hack), the list would not be removed and this new code would not be needed 🤔

another more explicit way might be to intersect the union of ->getArrays() with a TypeUtils::getAccessoryTypes() where NonEmptyArrayType is filtered away. also feels ugly. I'm not a 100% happy with any of the solutions so far.. like e.g.

$arrayType = TypeCombinator::intersect(
	TypeCombinator::union(...$arrayType->getArrays()),
	...array_filter(
		TypeUtils::getAccessoryTypes($arrayType),
		static fn (AccessoryType $type) => !$type instanceof NonEmptyArrayType,
	),
);

@herndlm herndlm changed the base branch from 1.11.x to 1.10.x June 11, 2023 19:11
@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 1.11.x. If your code is relevant on 1.10.x and you want it to be released sooner, please rebase your pull request and change its target to 1.10.x.

@herndlm herndlm force-pushed the bug-8449-retain-list-in-array-push-unshift branch from dbc40d6 to 53b3eee Compare June 11, 2023 19:11
@herndlm
Copy link
Contributor Author

herndlm commented Jun 12, 2023

Ha, I just found that the array_slice return type extension is using toArray() for the same thing essentially! (removing non-empty-array). But IMO toArray() should NOT remove it, that's also a bug I believe. I'd like to fix that one, but we should agree on a way to remove non-empty-array first most likely. maybe even move that to the good old TypeUtils even then hmm

update: this all feels dirty, maybe new type methods for push, unshift and slice or so are needed after all, but that feels like overhead...

let me know what you prefer or if more info is needed please

Verified

This commit was signed with the committer’s verified signature.
ondrejmirtes Ondřej Mirtes
@herndlm herndlm force-pushed the bug-8449-retain-list-in-array-push-unshift branch from 53b3eee to d0c4702 Compare June 18, 2023 11:20
@herndlm
Copy link
Contributor Author

herndlm commented Jun 18, 2023

rebased with the trick the boss came up with in 9e84671 to make the array non-non-empty again (why did I not think of this..)

now I'm happy with this :)

@ondrejmirtes ondrejmirtes merged commit 1f421d4 into phpstan:1.10.x Jun 18, 2023
@ondrejmirtes
Copy link
Member

Thank you! :)

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.

list<int> is changed to array when using array_push with variadic parameter
3 participants