Skip to content
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

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

Merged
merged 4 commits into from
Jun 18, 2023
Merged

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

merged 4 commits into from
Jun 18, 2023

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Apr 18, 2023

@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.

@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
@@ -12,7 +12,7 @@ class Foo
*/
public function nonEmpty(array $a): void
{
assertType('array', array_slice($a, 1));
assertType('non-empty-array', array_slice($a, 1));
Copy link
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
Collaborator

This pull request has been marked as ready for review.

@@ -50,7 +51,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if ($valueType->isIterableAtLeastOnce()->yes()) {
return $valueType->toArray();
return TypeCombinator::union($valueType->toArray(), new NonEmptyArrayType());
Copy link
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
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
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.

function f(int|array $id_or_ids): array
{
if (is_array($id_or_ids)) {
\PHPStan\Testing\assertType('non-empty-list<int>', (array)$id_or_ids);
Copy link
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

fixed

{
assertType('array', array_slice($a, 1));
assertType('list', array_slice($b, 1));
Copy link
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
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
Contributor Author

zonuexe commented Jun 16, 2023

@ondrejmirtes Could you please review this PR?

@herndlm
Copy link
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
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
82 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@zonuexe
Copy link
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
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
5 participants