barryvdh / laravel-ide-helper

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

Make model factory method annotation optional in phpDoc #1571

Open sanfair opened 1 month ago

sanfair commented 1 month ago

Summary

Laravel v11.15.0 added generic support for the model factories (https://github.com/laravel/framework/pull/52005)

If you use the \Illuminate\Database\Eloquent\Factories\HasFactory trait with generic annotation, factory annotation in the class's phpDoc becomes redundant.

Proposal

Make factory generation optional (ideally in the config).

Probably add additional condition between the next lines: https://github.com/barryvdh/laravel-ide-helper/blob/591e7d665fbab8a3b682e451641706341573eb80/src/Console/ModelsCommand.php#L1333-L1339

sanfair commented 1 month ago

Userland solution

  1. Create class RemoveFactoryAnnotation in app/Support/IdeHelper/RemoveFactoryAnnotation.php
    
    <?php

declare(strict_types=1);

namespace App\Support\IdeHelper;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface; use Illuminate\Database\Eloquent\Model; use Override;

class RemoveFactoryAnnotation implements ModelHookInterface {

[Override]

public function run(ModelsCommand $command, Model $model): void
{
    $command->unsetMethod('factory');
}

}


2. Add or update `config/ide-helper.php`
```php
<?php

declare(strict_types=1);

return [

    /*
    |--------------------------------------------------------------------------
    | Models hooks
    |--------------------------------------------------------------------------
    |
    | Define which hook classes you want to run for models to add custom information
    |
    | Hooks should implement Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface.
    |
    */

    'model_hooks' => [
        App\Support\IdeHelper\RemoveFactoryAnnotation::class,
    ],

];