hirasso / phpstan-illuminate-collections
PHPStan narrowing for filter()/reject()/where() on illuminate/collections, without Larastan
Package info
github.com/hirasso/phpstan-illuminate-collections
Type:phpstan-extension
pkg:composer/hirasso/phpstan-illuminate-collections
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/collections: ^11 || ^12 || ^13
- phpstan/phpstan: ^2.3.5
Requires (Dev)
- phpunit/phpunit: ^11.5 || ^12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PHPStan type narrowing for filter(), reject(), where() and values()->all() on illuminate/collections, for projects that use collections without Laravel (e.g. WordPress).
The filter/reject/where part is ported from Larastan (CollectionFilterRejectDynamicReturnTypeExtension), which can't be installed without large parts of the Laravel framework. If you use Laravel, use Larastan instead.
/** @var Collection<int, ?Post> $posts */ $posts->filter(); // Collection<int, Post> $posts->filter(fn ($p) => $p !== null); // Collection<int, Post> $posts->reject(); // Collection<int, null> $posts->filter()->values()->all(); // list<Post> (instead of array<int, Post>)
values()->all() only returns a list when chained directly, not via a variable holding the values() result.
Installation
composer require --dev hirasso/phpstan-illuminate-collections
With phpstan/extension-installer you're done. Otherwise, add this to your phpstan.neon:
includes: - vendor/hirasso/phpstan-illuminate-collections/extension.neon
Development
composer test
composer analyse