Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ivangretsky opened this issue Nov 13, 2023 · 3 comments
Labels

Comments

@ivangretsky
Copy link

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
Copy link
Collaborator

mfn commented Nov 15, 2023

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 :/

@olexoliinyk0
Copy link
Contributor

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?

@olexoliinyk0
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants