driftingly / rector-laravel

Rector upgrades rules for Laravel
http://getrector.org
MIT License
500 stars 49 forks source link

AddExtendsAnnotationToModelFactoriesRector rule are not applying any diff #137

Closed hmazter closed 11 months ago

hmazter commented 11 months ago

I'm trying to use the AddExtendsAnnotationToModelFactoriesRector to add the @extends annotation to factory classes, but nothing is applied. Am I missing something?

Factory class database/factories/CategoryFactory.php

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
    protected $model = \App\Models\Category::class;

    public function definition()
    {
        return [
            'name' => $this->faker->sentence(),
        ];
    }
}

Rector config rector.php

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/database',
    ]);

    // register a single rule
    $rectorConfig->rule(RectorLaravel\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector::class);
};
GeniJaho commented 11 months ago

You might need a backslash before the rule name. Maybe.

hmazter commented 11 months ago

You might need a backslash before the rule name. Maybe.

Thanks for the reply. Unfortenly it makes no difference, and neither does importing the class.

hmazter commented 11 months ago

The same thing happens in a fresh Laravel install if the @extends is removed from the default UserFactory

GeniJaho commented 11 months ago

@hmazter In that case I think it's because of the latest Rector release https://github.com/rectorphp/rector/releases/tag/0.18.3, which breaks rules that modify docblocks.

There is a PR to support that release https://github.com/driftingly/rector-laravel/pull/135, but until it gets merged, you can use a fixed older version of core Rector like 0.18.2: "rector/rector": "0.18.2",.

hmazter commented 11 months ago

Thanks that worked! 👍