hirasso/phpstan-illuminate-collections
phpstan-illuminate-collections
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
Related Packages
A PHPStan extension for analysis of Laravel applications and packages.
Dead code detector to find unused PHP code via PHPStan extension. Can automatica...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.0.0 | ^8.2 | ^11 || | MIT |