From 1b05475028db7e89e326d431a247db6442ca0a12 Mon Sep 17 00:00:00 2001 From: geni_jaho Date: Fri, 18 Aug 2023 19:22:01 +0200 Subject: [PATCH 1/2] Remove the model property and add the extends annotation with the model name --- config/sets/laravel80.php | 5 + docs/rector_rules_overview.md | 22 ++- ...ModelPropertyToExtendsAnnotationRector.php | 138 ++++++++++++++++++ .../SleepFuncToSleepStaticCallRector.php | 4 +- .../Fixture/existing_extends_tag.php.inc | 30 ++++ .../Fixture/fixture.php.inc | 27 ++++ .../using_class_string_as_model_type.php.inc | 27 ++++ ...lPropertyToExtendsAnnotationRectorTest.php | 28 ++++ .../config/configured_rule.php | 13 ++ 9 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php create mode 100644 tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc create mode 100644 tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc create mode 100644 tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc create mode 100644 tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php create mode 100644 tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php diff --git a/config/sets/laravel80.php b/config/sets/laravel80.php index 5b5075d6..d4f5c475 100644 --- a/config/sets/laravel80.php +++ b/config/sets/laravel80.php @@ -12,6 +12,7 @@ use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector; use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Renaming\ValueObject\RenameProperty; +use RectorLaravel\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector; use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector; use RectorLaravel\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector; use RectorLaravel\ValueObject\AddArgumentDefaultValue; @@ -42,6 +43,10 @@ # https://github.com/laravel/framework/commit/f1289515b27e93248c09f04e3011bb7ce21b2737 $rectorConfig->rule(AddParentRegisterToEventServiceProviderRector::class); + // https://github.com/laravel/framework/pull/39169 + // https://github.com/laravel/framework/pull/39310 + $rectorConfig->rule(MoveFactoryModelPropertyToExtendsAnnotationRector::class); + $rectorConfig ->ruleWithConfiguration(RenamePropertyRector::class, [ # https://github.com/laravel/framework/pull/32092/files new RenameProperty('Illuminate\Support\Manager', 'app', 'container'), diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index e2374ad3..d7a687ce 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 37 Rules Overview +# 38 Rules Overview ## AddArgumentDefaultValueRector @@ -609,6 +609,26 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill
+## MoveFactoryModelPropertyToExtendsAnnotationRector + +Removes the `$model` property and adds `@extends` annotation to Factories. + +- class: [`RectorLaravel\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector`](../src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php) + +```diff + use Illuminate\Database\Eloquent\Factories\Factory; + ++/** ++ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> ++ */ + class UserFactory extends Factory + { +- protected $model = \App\Models\User::class; + } +``` + +
+ ## NotFilledBlankFuncCallToBlankFilledFuncCallRector Swap the use of NotBooleans used with `filled()` and `blank()` to the correct helper. diff --git a/src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php b/src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php new file mode 100644 index 00000000..568f2409 --- /dev/null +++ b/src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php @@ -0,0 +1,138 @@ + + */ +class UserFactory extends Factory +{ +} +CODE_SAMPLE + ), + ]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param Class_ $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isObjectType($node, new ObjectType(self::FACTORY_CLASS_NAME))) { + return null; + } + + foreach ($node->stmts as $index => $stmt) { + if (! $stmt instanceof Property) { + continue; + } + + if (! $this->isName($stmt, 'model')) { + continue; + } + + $this->addExtendsPhpDocTag($stmt, $node); + + unset($node->stmts[$index]); + + break; + } + + return $node; + } + + public function addExtendsPhpDocTag(Property $property, Node $node): void + { + if ($property->props === []) { + return; + } + + $modelName = $this->getModelName($property->props[0]->default); + + if ($modelName === null) { + return; + } + + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + + if ($phpDocInfo->hasByName(self::EXTENDS_TAG_NAME)) { + return; + } + + $phpDocTagNode = new PhpDocTagNode(self::EXTENDS_TAG_NAME, new ExtendsTagValueNode( + new GenericTypeNode( + new FullyQualifiedIdentifierTypeNode(self::FACTORY_CLASS_NAME), + [new FullyQualifiedIdentifierTypeNode($modelName)] + ), + '' + )); + + $phpDocInfo->addPhpDocTagNode($phpDocTagNode); + } + + private function getModelName(?Expr $defaultProp): ?string + { + if ($defaultProp instanceof ClassConstFetch) { + return $this->getName($defaultProp->class); + } + + if ($defaultProp instanceof String_) { + return $defaultProp->value; + } + + return null; + } +} diff --git a/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php b/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php index a718acff..642c7c48 100644 --- a/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php +++ b/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php @@ -23,11 +23,11 @@ public function getRuleDefinition(): RuleDefinition <<<'CODE_SAMPLE' sleep(5); CODE_SAMPLE -, + , <<<'CODE_SAMPLE' \Illuminate\Support\Sleep::sleep(5); CODE_SAMPLE -, + , ), ] ); diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc new file mode 100644 index 00000000..b38d32c0 --- /dev/null +++ b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc @@ -0,0 +1,30 @@ + + */ +class UserFactory extends Factory +{ + protected $model = \App\Models\User::class; +} + +?> +----- + + */ +class UserFactory extends Factory +{ +} + +?> diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..f384444f --- /dev/null +++ b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + + */ +class UserFactory extends Factory +{ +} + +?> diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc new file mode 100644 index 00000000..0a1cbca5 --- /dev/null +++ b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc @@ -0,0 +1,27 @@ + +----- + + */ +class UserFactory extends Factory +{ +} + +?> diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php new file mode 100644 index 00000000..6334cde1 --- /dev/null +++ b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php new file mode 100644 index 00000000..64a4a2a4 --- /dev/null +++ b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php @@ -0,0 +1,13 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(MoveFactoryModelPropertyToExtendsAnnotationRector::class); +}; From a66d7fef7869740709099cf74efd34393615cd56 Mon Sep 17 00:00:00 2001 From: geni_jaho Date: Fri, 18 Aug 2023 21:05:44 +0200 Subject: [PATCH 2/2] Extract AddExtendsAnnotationToModelFactoriesRector and RemoveModelPropertyFromFactoriesRector rules --- config/sets/laravel80.php | 5 -- config/sets/laravel90.php | 4 + docs/rector_rules_overview.md | 59 +++++++++----- ...tendsAnnotationToModelFactoriesRector.php} | 19 +++-- ...RemoveModelPropertyFromFactoriesRector.php | 80 +++++++++++++++++++ ...sAnnotationToModelFactoriesRectorTest.php} | 4 +- .../Fixture/fixture.php.inc | 5 +- .../Fixture/skip_existing_extends_tag.php.inc | 15 ++++ .../using_class_string_as_model_type.php.inc | 5 +- .../config/configured_rule.php | 4 +- .../Fixture/existing_extends_tag.php.inc | 30 ------- .../Fixture/fixture.php.inc | 24 ++++++ ...veModelPropertyFromFactoriesRectorTest.php | 28 +++++++ .../config/configured_rule.php | 13 +++ 14 files changed, 221 insertions(+), 74 deletions(-) rename src/Rector/Class_/{MoveFactoryModelPropertyToExtendsAnnotationRector.php => AddExtendsAnnotationToModelFactoriesRector.php} (81%) create mode 100644 src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php rename tests/Rector/Class_/{MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php => AddExtendsAnnotationToModelFactoriesRector/AddExtendsAnnotationToModelFactoriesRectorTest.php} (73%) rename tests/Rector/Class_/{MoveFactoryModelPropertyToExtendsAnnotationRector => AddExtendsAnnotationToModelFactoriesRector}/Fixture/fixture.php.inc (59%) create mode 100644 tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/skip_existing_extends_tag.php.inc rename tests/Rector/Class_/{MoveFactoryModelPropertyToExtendsAnnotationRector => AddExtendsAnnotationToModelFactoriesRector}/Fixture/using_class_string_as_model_type.php.inc (59%) rename tests/Rector/Class_/{MoveFactoryModelPropertyToExtendsAnnotationRector => AddExtendsAnnotationToModelFactoriesRector}/config/configured_rule.php (55%) delete mode 100644 tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc create mode 100644 tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/Fixture/fixture.php.inc create mode 100644 tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/RemoveModelPropertyFromFactoriesRectorTest.php create mode 100644 tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/config/configured_rule.php diff --git a/config/sets/laravel80.php b/config/sets/laravel80.php index d4f5c475..5b5075d6 100644 --- a/config/sets/laravel80.php +++ b/config/sets/laravel80.php @@ -12,7 +12,6 @@ use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector; use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Renaming\ValueObject\RenameProperty; -use RectorLaravel\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector; use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector; use RectorLaravel\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector; use RectorLaravel\ValueObject\AddArgumentDefaultValue; @@ -43,10 +42,6 @@ # https://github.com/laravel/framework/commit/f1289515b27e93248c09f04e3011bb7ce21b2737 $rectorConfig->rule(AddParentRegisterToEventServiceProviderRector::class); - // https://github.com/laravel/framework/pull/39169 - // https://github.com/laravel/framework/pull/39310 - $rectorConfig->rule(MoveFactoryModelPropertyToExtendsAnnotationRector::class); - $rectorConfig ->ruleWithConfiguration(RenamePropertyRector::class, [ # https://github.com/laravel/framework/pull/32092/files new RenameProperty('Illuminate\Support\Manager', 'app', 'container'), diff --git a/config/sets/laravel90.php b/config/sets/laravel90.php index ba558cee..a90e3bfb 100644 --- a/config/sets/laravel90.php +++ b/config/sets/laravel90.php @@ -10,6 +10,7 @@ use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Visibility\ValueObject\ChangeMethodVisibility; +use RectorLaravel\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector; use RectorLaravel\Rector\PropertyFetch\ReplaceFakerInstanceWithHelperRector; # see https://laravel.com/docs/9.x/upgrade @@ -70,6 +71,9 @@ // https://github.com/laravel/framework/commit/7746337149a7ffd6b4a862d9bd54593cf3520708 $rectorConfig->rule(ReplaceFakerInstanceWithHelperRector::class); + // https://github.com/laravel/framework/pull/39169 + $rectorConfig->rule(AddExtendsAnnotationToModelFactoriesRector::class); + $rectorConfig ->ruleWithConfiguration(RenameMethodRector::class, [ // https://github.com/laravel/framework/commit/9b4f011fb95c70444812f61d46c8e21fb5b66dd9 diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index d7a687ce..bd84a5e0 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 38 Rules Overview +# 39 Rules Overview ## AddArgumentDefaultValueRector @@ -40,6 +40,26 @@ return static function (RectorConfig $rectorConfig): void {
+## AddExtendsAnnotationToModelFactoriesRector + +Adds the `@extends` annotation to Factories. + +- class: [`RectorLaravel\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector`](../src/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector.php) + +```diff + use Illuminate\Database\Eloquent\Factories\Factory; + ++/** ++ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> ++ */ + class UserFactory extends Factory + { + protected $model = \App\Models\User::class; + } +``` + +
+ ## AddGenericReturnTypeToRelationsRector Add generic return type to relations in child of `Illuminate\Database\Eloquent\Model` @@ -609,26 +629,6 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill
-## MoveFactoryModelPropertyToExtendsAnnotationRector - -Removes the `$model` property and adds `@extends` annotation to Factories. - -- class: [`RectorLaravel\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector`](../src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php) - -```diff - use Illuminate\Database\Eloquent\Factories\Factory; - -+/** -+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> -+ */ - class UserFactory extends Factory - { -- protected $model = \App\Models\User::class; - } -``` - -
- ## NotFilledBlankFuncCallToBlankFilledFuncCallRector Swap the use of NotBooleans used with `filled()` and `blank()` to the correct helper. @@ -815,6 +815,23 @@ It will removes the dump data just like dd or dump functions from the code.`
+## RemoveModelPropertyFromFactoriesRector + +Removes the `$model` property from Factories. + +- class: [`RectorLaravel\Rector\Class_\RemoveModelPropertyFromFactoriesRector`](../src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php) + +```diff + use Illuminate\Database\Eloquent\Factories\Factory; + + class UserFactory extends Factory + { +- protected $model = \App\Models\User::class; + } +``` + +
+ ## ReplaceFakerInstanceWithHelperRector Replace `$this->faker` with the `fake()` helper function in Factories diff --git a/src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php b/src/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector.php similarity index 81% rename from src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php rename to src/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector.php index 568f2409..682b10ef 100644 --- a/src/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector.php +++ b/src/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector.php @@ -20,11 +20,11 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see https://github.com/laravel/framework/pull/39169 - * @see https://github.com/laravel/framework/pull/39310 - * @see \RectorLaravel\Tests\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector\MoveFactoryModelPropertyToExtendsAnnotationRectorTest + * @changelog https://github.com/laravel/framework/pull/39169 + * + * @see \RectorLaravel\Tests\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector\AddExtendsAnnotationToModelFactoriesRectorTest */ -final class MoveFactoryModelPropertyToExtendsAnnotationRector extends AbstractRector +final class AddExtendsAnnotationToModelFactoriesRector extends AbstractRector { private const EXTENDS_TAG_NAME = '@extends'; @@ -32,7 +32,7 @@ final class MoveFactoryModelPropertyToExtendsAnnotationRector extends AbstractRe public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Removes the $model property and adds @extends annotation to Factories.', [ + return new RuleDefinition('Adds the @extends annotation to Factories.', [ new CodeSample( <<<'CODE_SAMPLE' use Illuminate\Database\Eloquent\Factories\Factory; @@ -52,6 +52,7 @@ class UserFactory extends Factory */ class UserFactory extends Factory { + protected $model = \App\Models\User::class; } CODE_SAMPLE ), @@ -75,7 +76,7 @@ public function refactor(Node $node): ?Node return null; } - foreach ($node->stmts as $index => $stmt) { + foreach ($node->stmts as $stmt) { if (! $stmt instanceof Property) { continue; } @@ -84,9 +85,7 @@ public function refactor(Node $node): ?Node continue; } - $this->addExtendsPhpDocTag($stmt, $node); - - unset($node->stmts[$index]); + $this->addExtendsPhpDocTag($node, $stmt); break; } @@ -94,7 +93,7 @@ public function refactor(Node $node): ?Node return $node; } - public function addExtendsPhpDocTag(Property $property, Node $node): void + public function addExtendsPhpDocTag(Node $node, Property $property): void { if ($property->props === []) { return; diff --git a/src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php b/src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php new file mode 100644 index 00000000..bc890446 --- /dev/null +++ b/src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php @@ -0,0 +1,80 @@ +> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param Class_ $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isObjectType($node, new ObjectType('Illuminate\Database\Eloquent\Factories\Factory'))) { + return null; + } + + foreach ($node->stmts as $index => $stmt) { + if (! $stmt instanceof Property) { + continue; + } + + if (! $this->isName($stmt, 'model')) { + continue; + } + + unset($node->stmts[$index]); + + break; + } + + return $node; + } +} diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/AddExtendsAnnotationToModelFactoriesRectorTest.php similarity index 73% rename from tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php rename to tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/AddExtendsAnnotationToModelFactoriesRectorTest.php index 6334cde1..03be9229 100644 --- a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/MoveFactoryModelPropertyToExtendsAnnotationRectorTest.php +++ b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/AddExtendsAnnotationToModelFactoriesRectorTest.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace RectorLaravel\Tests\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector; +namespace RectorLaravel\Tests\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector; use Iterator; use PHPUnit\Framework\Attributes\DataProvider; use Rector\Testing\PHPUnit\AbstractRectorTestCase; -final class MoveFactoryModelPropertyToExtendsAnnotationRectorTest extends AbstractRectorTestCase +final class AddExtendsAnnotationToModelFactoriesRectorTest extends AbstractRectorTestCase { #[DataProvider('provideData')] public function test(string $filePath): void diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/fixture.php.inc similarity index 59% rename from tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc rename to tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/fixture.php.inc index f384444f..129bd4d2 100644 --- a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/fixture.php.inc +++ b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/fixture.php.inc @@ -1,6 +1,6 @@ diff --git a/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/skip_existing_extends_tag.php.inc b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/skip_existing_extends_tag.php.inc new file mode 100644 index 00000000..230c16ba --- /dev/null +++ b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/skip_existing_extends_tag.php.inc @@ -0,0 +1,15 @@ + + */ +class UserFactory extends Factory +{ + protected $model = \App\Models\User::class; +} + +?> diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/using_class_string_as_model_type.php.inc similarity index 59% rename from tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc rename to tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/using_class_string_as_model_type.php.inc index 0a1cbca5..996e194c 100644 --- a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/using_class_string_as_model_type.php.inc +++ b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/Fixture/using_class_string_as_model_type.php.inc @@ -1,6 +1,6 @@ diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/config/configured_rule.php similarity index 55% rename from tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php rename to tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/config/configured_rule.php index 64a4a2a4..5208b5c2 100644 --- a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/config/configured_rule.php +++ b/tests/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector/config/configured_rule.php @@ -4,10 +4,10 @@ use Rector\Config\RectorConfig; -use RectorLaravel\Rector\Class_\MoveFactoryModelPropertyToExtendsAnnotationRector; +use RectorLaravel\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); - $rectorConfig->rule(MoveFactoryModelPropertyToExtendsAnnotationRector::class); + $rectorConfig->rule(AddExtendsAnnotationToModelFactoriesRector::class); }; diff --git a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc b/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc deleted file mode 100644 index b38d32c0..00000000 --- a/tests/Rector/Class_/MoveFactoryModelPropertyToExtendsAnnotationRector/Fixture/existing_extends_tag.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - - */ -class UserFactory extends Factory -{ - protected $model = \App\Models\User::class; -} - -?> ------ - - */ -class UserFactory extends Factory -{ -} - -?> diff --git a/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/Fixture/fixture.php.inc b/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..8c0f8d5b --- /dev/null +++ b/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/Fixture/fixture.php.inc @@ -0,0 +1,24 @@ + +----- + diff --git a/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/RemoveModelPropertyFromFactoriesRectorTest.php b/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/RemoveModelPropertyFromFactoriesRectorTest.php new file mode 100644 index 00000000..8600366c --- /dev/null +++ b/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/RemoveModelPropertyFromFactoriesRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/config/configured_rule.php b/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/config/configured_rule.php new file mode 100644 index 00000000..d60363e2 --- /dev/null +++ b/tests/Rector/Class_/RemoveModelPropertyFromFactoriesRector/config/configured_rule.php @@ -0,0 +1,13 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(RemoveModelPropertyFromFactoriesRector::class); +};