larastan: Custom builder doc blocks are not parsed as builder generic

  • Larastan Version: 0.5.2
  • --level used: max

The behavior changed with PR https://github.com/nunomaduro/larastan/pull/457

Description

Calling collection methods on a result collection gives errors like Cannot call method pluck() on array<Illuminate\Database\Eloquent\Builder<mixed>>|Illuminate\Database\Eloquent\Collection.

This also happens to count(), contains(), avg(), first().

Laravel code where the issue was found

<?php

declare(strict_types=1);

namespace Tests\Unit;

use App\Models\Transaction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Topic extends Model
{
    public function categories(): HasMany
    {
        return $this->hasMany(Category::class);
    }
}

class Category extends Model
{
    public function topics(): HasMany
    {
        return $this->hasMany(Topic::class);
    }
}

$categoryIds = Transaction::query()
    ->groupBy('category_id')
    ->get(['category_id'])
    ->pluck('category_id');

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

That one was generated by myself before installing the ide-helper to do it for me, my apologies. If I now create a new model called Foo and run the ide-helper it would look like:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Foo
 *
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Foo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Foo newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Foo query()
 * @mixin \Eloquent
 */
class Foo extends Model
{
    //
}

Also, you can use @phpstan-method together with your normal docblocks.