Skip to content

Make NonEmptyArrayType::toArray() return $this#2349

Merged
ondrejmirtes merged 4 commits intophpstan:1.10.xfrom
zonuexe:fix/9208
Jun 18, 2023
Merged

Make NonEmptyArrayType::toArray() return $this#2349
ondrejmirtes merged 4 commits intophpstan:1.10.xfrom
zonuexe:fix/9208

Conversation

@zonuexe
Copy link
Copy Markdown
Contributor

@zonuexe zonuexe commented Apr 18, 2023

@phpstan-bot
Copy link
Copy Markdown
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.

@zonuexe zonuexe changed the base branch from 1.11.x to 0.12.x April 18, 2023 17:01
@zonuexe zonuexe changed the base branch from 0.12.x to 1.10.x April 18, 2023 17:02
@zonuexe zonuexe marked this pull request as draft April 18, 2023 17:20
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.

array_slice() can return empty arrays. https://3v4l.org/8VGrv

@zonuexe zonuexe marked this pull request as ready for review April 18, 2023 18:44
@phpstan-bot
Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

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.

intersect, not union

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.

I'm trying to make a non-empty-array&array<T> into an array<T>. Isn't it a union?

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.

So you mean that you want an array that’s non-empty to no longer be non-empty? I’m afraid you need to recreate it from scratch by doing new ArrayType.

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.

The file needs to have namespace Bug9208.

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.

The file isn't referenced in NodeScopeResolverTest

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.

Please use PHPStan\Testing\assertType in a use function statement.

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.

fixed

Copy link
Copy Markdown
Contributor

@staabm staabm Apr 19, 2023

Choose a reason for hiding this comment

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

seems this assertion fails atm in CI

Suggested change
assertType('list', array_slice($b, 1));
assertType('list<mixed>', array_slice($b, 1));

@zonuexe zonuexe marked this pull request as draft April 20, 2023 19:07
@zonuexe zonuexe marked this pull request as ready for review April 20, 2023 19:47
@phpstan-bot
Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@zonuexe zonuexe requested a review from ondrejmirtes May 4, 2023 18:11
@zonuexe
Copy link
Copy Markdown
Contributor Author

zonuexe commented Jun 16, 2023

@ondrejmirtes Could you please review this PR?

@herndlm
Copy link
Copy Markdown
Contributor

herndlm commented Jun 16, 2023

Ah interesting, this is the same problem I found independently via #2451 (comment) but with a different solution that not just only removes that one accessory

@ondrejmirtes
Copy link
Copy Markdown
Member

There's a much easier solution to make a non-empty array possibly empty again :) 9e84671

@ondrejmirtes ondrejmirtes merged commit 92deb3f into phpstan:1.10.x Jun 18, 2023
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you!

@zonuexe
Copy link
Copy Markdown
Contributor Author

zonuexe commented Jun 18, 2023

There's a much easier solution to make a non-empty array possibly empty again :) 9e84671

I agree. 😄

@ondrejmirtes
Copy link
Copy Markdown
Member

Oh wow, so you actually planted it in my brain two months ago :D Well I'm a bit embarassed now :D

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.

non-empty gets lost when non-empty-array is cast with (array)

5 participants