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

Make model factory method annotation optional in phpDoc #1571

Open
sanfair opened this issue Jul 16, 2024 · 2 comments
Open

Make model factory method annotation optional in phpDoc #1571

sanfair opened this issue Jul 16, 2024 · 2 comments

Comments

@sanfair
Copy link

sanfair commented Jul 16, 2024

Summary

Laravel v11.15.0 added generic support for the model factories (laravel/framework#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:

protected function getFactoryMethods($model)
{
if (!class_exists(Factory::class)) {
return;
}
$modelName = get_class($model);

@sanfair
Copy link
Author

sanfair commented Jul 16, 2024

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');
    }
}
  1. Add or update config/ide-helper.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,
    ],

];

@makowskid
Copy link

Thanks, this helps a lot with redundant issues reported by IDE

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

No branches or pull requests

2 participants