fkupper / psalm-laravel-collections
为 Psalm 更好地理解 Laravel Collections 提供存根
v6.0.0
2020-08-12 16:38 UTC
Requires
- php: ^7.2
- vimeo/psalm: dev-master
Requires (Dev)
- codeception/codeception: ^4.0
- laravel/framework: ^6.0
- squizlabs/php_codesniffer: ^3.3
- weirdan/codeception-psalm-module: ^0.8.0
This package is auto-updated.
Last update: 2024-09-23 16:21:19 UTC
README
为 Laravel 的 \Illuminate\Support\Collection
插件(需要 Psalm v3)提供 Psalm 插件,以帮助您在某些使用集合的情况下找到错误。
安装
$ composer require --dev fkupper/psalm-laravel-collections $ vendor/bin/psalm-plugin enable fkupper/psalm-laravel-collections
示例
使用错误键类型访问数组
/** @var Collection<string,string> */ $c = new Collection(['a' => 'A', 'b' => 'B', 'c' => 'C']); $items = $c->all(); $items[1];
添加无效类型的项
/** @var Collection<int,string> */ $c = new Collection(["a", "b", "c"]); // items type is deinfed as string, cannot add int $c->add(1);
断言集合项的值类型
/** * @param Collection<string,\Exception> */ function(Collection $coll): void { $exception = $coll->first(); // psalm will report this typo $exception->getMassage(); } /** * @param Collection<int,string> */ function(Collection $coll): int { $value = $coll->first(); // psalm will remind you forgot to cast that $value to int return $value + 1; }
更好地断言辅助方法,如 Collection::filter
/** * This function is using wrong types in the filter Closure params. * @param Collection<int,string> */ function(Collection $coll): void { $filteredValues = $coll->filter( // psalm will tell you that the Closure params are wrong function (bool $value, float $key): bool { return true; } ) } /** * @param Collection<int,string> */ function(Collection $coll): void { $filteredValues = $coll->filter( function (int $value, string $key): bool { return true; } ) // psalm understands that the result of the filter call // is a collection of same type as it was before, so the value // type is still string, therefore array cannot be added $filteredValues->add(['something']); }
工作进行中
这是一个正在进行中的项目,因此并非所有集合方法都已扩展模板或测试。
请报告您可能发现的所有问题,甚至更好的,提交一个修复的 pull request!