英文:
Larastan complains about collection methods paramaters after upgrading to Laravel 10
问题
在升级到 Laravel 10 后,我在处理 Larastan 错误时遇到了困难。
以下代码在1小时前还完全正常:
return $this->articleRepository->getDefaultArticles($organizationId)
->toBase()
->map(function (Article $article) {
return new Content(
$article->id,
$article->title,
$article->language,
);
})
->toArray();
现在给我返回以下错误:
方法 Illuminate\Support\Collection<(int|string),Illuminate\Database\Eloquent\Model>::map() 的参数 #1 $callback 期望 callable(Illuminate\Database\Eloquent\Model, int|string): App\Academy\Content,但给出的是 Closure(App\Models\Article): App\Academy\Content
仓库方法具有正确的提示:
/**
* @return Collection<Article>
*/
public function getDefaultArticles(OrganizationId $organizationId): Collection
{
/** @var Collection<Article> */
return Article::query()
->where('organization_id', $organizationId)
->get()
->keyBy('id')
->values();
}
它给我返回了115个新的错误,其中大多数与此类似,与 map
和 reduce
等集合方法相关。
快速解决方法是使用临时变量并添加类型提示:
/** @var Collection<Article> $articles */
$articles = $this->articleRepository
->getDefaultArticles($organizationId)
->toBase();
但我不想这样做100次,即使IDE也在抱怨这是不必要的。
感谢您的帮助!
英文:
After upgrading to Laravel 10 I'm having hard times with Larastan errors.
The following code which was perfectly fine until 1 hour ago:
return $this->articleRepository->getDefaultArticles($organizationId)
->toBase()
->map(function (Article $article) {
return new Content(
$article->id,
$article->title,
$article->language,
);
})
->toArray();
now gives me the following error:
> Parameter #1 $callback of method Illuminate\Support\Collection<(int|string),Illuminate\Database\Eloquent\Model>::map() expects callable(Illuminate\Database\Eloquent\Model, int|string): App\Academy\Content,
Closure(App\Models\Article): App\Academy\Content given
The repository method has the correct hint:
/**
* @return Collection<Article>
*/
public function getDefaultArticles(OrganizationId $organizationId): Collection
{
/** @var Collection<Article> */
return Article::query()
->where('organization_id', $organizationId)
->get()
->keyBy('id')
->values();
}
It gives me 115 new errors and most of them are similar to this, related to collection methods like map
and reduce
.
The quick solution would be using a temporary variable and add a type hinting:
/** @var Collection<Article> $articles */
$articles = $this->articleRepository
->getDefaultArticles($organizationId)
->toBase();
but I don't want to do it 100 times and even the IDE is complaining that's unnecessary
Thanks in advance for you help!
答案1
得分: 1
我目前找到的唯一解决方法是使用类型提示进行两个步骤。
/** @var \Illuminate\Support\Collection $result */
$result = $this->articleRepository->getDefaultArticles($organizationId);
return $result->map(function (Article $article) {
return new Content(
$article->id,
$article->title,
$article->language,
);
})
->toArray();
这实际上并不帮助处理 115 个新错误。但如果您只有一些错误,它可能会有所帮助。
英文:
The only work around I found so far is to make it in 2 steps with type hinting.
/** @var \Illuminate\Support\Collection $result */
$result = $this->articleRepository->getDefaultArticles($organizationId);
return $result->map(function (Article $article) {
return new Content(
$article->id,
$article->title,
$article->language,
);
})
->toArray();
it does not really help to deal with 115 new errors. But if you would have couple errors, it would help.
答案2
得分: 0
解决方案是降级phpstan:
$ composer require phpstan/phpstan:1.10.11 --dev
英文:
the solution that worked for me was downgrading phpstan:
$ composer require phpstan/phpstan:1.10.11 --dev
答案3
得分: 0
尝试
/**
- @return Collection<int, Article>
*/
for getDefaultArticles PHP 文档类型
查看这个链接:https://github.com/nunomaduro/larastan/blob/master/UPGRADE.md#eloquent-collection-now-requires-2-generic-types
英文:
try
/**
* @return Collection<int,Article>
*/
for getDefaultArticles php doc type
答案4
得分: 0
尝试从map
的回调函数中移除Article
类型:
...
->map(function ($article) {
return new Content(
$article->id,
$article->title,
$article->language,
);
})
...
英文:
Try removing the Article type from the callback of map :
...
->map(function ($article) {
return new Content(
$article->id,
$article->title,
$article->language,
);
})
...
答案5
得分: 0
Static code analysis tools具有创建基准文件的可能性,该文件包含需要稍后修复的现有和已知错误 - 可以在代码中修复或者通过相应的解析器和推理工具的更新来修复。例如,PhpStan/LaraStan或者PsalmPHP支持此基准文件。
通过这种方式,您可以承认所描述的推理缺陷,并继续专注于实际的应用逻辑。
基准文件
在较旧的代码库中,可能难以花时间修复所有的代码以通过较高的PHPStan级别。
为了解决这个问题,可以生成一个基准文件。基准文件将创建一个配置文件,其中包含所有当前的错误,因此新代码可以按照比旧代码更高的标准编写。(PHPStan文档)
vendor/bin/phpstan analyse --generate-baseline
来源:https://github.com/nunomaduro/larastan#baseline-file
在PhpStan的基准文件部分查找更多详细信息。
英文:
Static code analysis tools have the possibility to create a baseline file, which contains existing and known errors to be fixed later - either fixed in the code, or with a corresponding update of the parser & inference tools. This baseline is for instance supported in PhpStan/LaraStan or PsalmPHP.
This way you can acknowledge the described inference flaws and continue with focussing again on the actual application logic.
> ### Baseline file
> In older codebases it might be hard to spend the time fixing all the code to pass a high PHPStan Level.
>
> To get around this a baseline file can be generated. The baseline file
> will create a configuration file with all of the current errors, so
> new code can be written following a higher standard than the old code.
> (PHPStan Docs)
> bash
> vendor/bin/phpstan analyse --generate-baseline
>
source: https://github.com/nunomaduro/larastan#baseline-file
Find more details in PhpStan's The Baseline section.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论