barryvdh/laravel-ide-helper

How to not include factory and query related generic @methods in models' PHPDoc

ivangretsky opened this issue · 3 comments

Versions:

  • ide-helper Version: 2.13.0
  • Laravel Version: 10.19.0
  • PHP Version: 8.2.8

Question:

Good day and thanks for the great tool!

I am using it to generate models' PHPDocs. I need to only include the properties and methods of the class itself, not the ones from the factory companion class and the \Eloquent\Builder query-related methods. Is there a way to do it? I already opted out of almost everything in the config)

I need this

 * @property int $id
 * @property string $name
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users

but not this

 * @method static \Database\Factories\OrganizationFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Organization newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Organization newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Organization query()
mfn commented

There is supposed to be a way using model hooks:

But due to the current behaviour, which I would classify as "bug", it does not work: because unsetMethod performs strtolower() but the names in internal array are not lowered cases.

It would work for methods not having mixed case (like query) but not newModelQuery :/

it does not work: because unsetMethod performs strtolower() but the names in internal array are not lowered cases

Stumbled upon that one too.. Looks like there's already a fix for that - #1441, but it was closed by the creator. Any ideas why and if are there workarounds?

Ok, so anyway I needed it to work so I did the following:

  1. Created my extension of \Barryvdh\LaravelIdeHelper\Console\ModelsCommand:
<?php

namespace App\Support\IdeHelper;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;

class ModelsCommandCustom extends ModelsCommand
{
    public function unsetMethod($name)
    {
        unset($this->methods[$name]); // new one
        unset($this->methods[strtolower($name)]); // original one
    }
}
  1. In the AppServiceProvider have added this to the register method:
$this->app->singleton(
    'command.ide-helper.models',
    function ($app) {
        return new ModelsCommandCustom($app['files']);
    }
);
  1. Now Laravel's App/Container uses my "implementation" of ModelsCommand, so the following works fine:
$command->unsetMethod('myCamelCaseMethod'); // this now works