barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.19k stars 1.16k forks source link

Precedence of model mixins? #1213

Open brendt opened 3 years ago

brendt commented 3 years ago

I've generated model IDE helper mixins using ide-helper:models -M.

One example of a model is this:

/**
 * @mixin IdeHelperDegreeType
 */
class DegreeType extends Model

Looking in the _ide_helper_models.php file, IdeHelperDegreeType looks like this:

namespace App\Context\User\Models{
/**
 * App\Context\User\Models\DegreeType
 *
 * @mixin IdeHelperDegreeType
 * @property string $uuid
 * @property string $name
 * @property \Carbon\CarbonImmutable|null $created_at
 * @property \Carbon\CarbonImmutable|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Context\User\Models\DegreeType whereUuid($value)
 */
    class IdeHelperDegreeType extends \Eloquent {}
}

All looks good up until this point, however, both PhpStorm and Psalm report errors on DegreeType::query()->whereName("B.A./B.S.")->first(), they don't know that query() can return a DegreeType model. In fact, if I "go to definition" on the query method, PhpStorm sends me to Laravel's Model class, and not the mixin file. This makes me believe that either the mixin file isn't recognised, or that Laravel's code somehow gets precedence over the mixin file.

Any ideas?

mfn commented 3 years ago

Does this also apply if you do not use _ide_helper_models.php (i.e. remove it) and just write the metadata into the models?

brendt commented 3 years ago

No, but we really need the mixin approach.

mfn commented 3 years ago

but we really need the mixin approach.

May I inquire as to why?

brendt commented 3 years ago

Because mixins prevent the duplicate class issue, and because we use class doc comments for other stuff as well, so we can't override them every time we generate new model meta data.

freshleafmedia commented 1 year ago

I can confirm this bug still exists. @brendt Did you find any kind of workaround or fix?