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

LazyCollection::every() type inferences by PHPStan are completely broken #41369

Closed
mpyw opened this issue Mar 7, 2022 · 10 comments
Closed

LazyCollection::every() type inferences by PHPStan are completely broken #41369

mpyw opened this issue Mar 7, 2022 · 10 comments

Comments

@mpyw
Copy link
Contributor

mpyw commented Mar 7, 2022

  • Laravel Version: 9.3.1
  • PHPStan Version: 1.4.5
  • Larastan Version: 1.0.3
  • PHP Version: 8.1

Description & Reproduction Steps:

$collection = new LazyCollection([new \stdClass]);
\PHPStan\dumpType($collection->every(...));

Expected:

Dumped type: Closure((callable(stdClass, int): bool)|stdClass|string, mixed, mixed): bool

Actual:

Dumped type: Closure((callable(int, int): bool)|int|string, mixed, mixed): bool

Though I haven't determined yet whether it is a PHPStan bug or a Laravel bug, I'll submit it as a Laravel issue for the time being.

@mpyw
Copy link
Contributor Author

mpyw commented Mar 7, 2022

I also hope that #40889 should be resolved although it is not directly related. It is very inconvenient that Collection<Item> is NOT considered a subtype of Collection<ItemInterface> whereas Item[] is considered a subtype of ItemInterface[].

I'm currently trying to upgrade Laravel apps from 8.x to 9.x, but getting stuck with the PHPStan type inference problems of collection generics.

@driesvints
Copy link
Member

Please just attempt a PR if you want to adjust anything. No code is broken.

@mpyw
Copy link
Contributor Author

mpyw commented Mar 8, 2022

@driesvints I confirmed that it's not reproducible in newly cloned laravel/laravel apps, however, it actually appears in my existing app. I'll research some more.


Screenshot:

image

image

@mpyw
Copy link
Contributor Author

mpyw commented Mar 8, 2022

It's very odd, looks like LazyCollection::every() specific.

image

image

@mpyw
Copy link
Contributor Author

mpyw commented Mar 8, 2022

wtf???

image

image

Method Collection LazyCollection
every()
some()
map() filter()

@mpyw
Copy link
Contributor Author

mpyw commented Mar 8, 2022

A clean install solved the problem. I can't reproducd the bug anymore regardless I haven't touch directly inside vendor and prepare the same version of composer.lock as before. I have no idea what's going on. Was the cache corrupted?

@mpyw
Copy link
Contributor Author

mpyw commented Mar 8, 2022

Reproduced again

@mpyw
Copy link
Contributor Author

mpyw commented Mar 8, 2022

Confirmed that the TValue inferencing is unexpectedly replaced with TKey.

image
image
image

@mpyw
Copy link
Contributor Author

mpyw commented Mar 9, 2022

Re-published as a PHPStan issue: phpstan/phpstan#6768

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

No branches or pull requests

2 participants