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!