From 97a5d73c37a2600152ffb745dac4997ab19bc262 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 21 Mar 2024 21:59:54 +0100 Subject: [PATCH] Add new rule - ExplicitRelationCollectionRector --- .../ExplicitRelationCollectionRectorTest.php | 27 +++++ .../Fixture/some_class.php.inc | 38 +++++++ .../config/configured_rule.php | 10 ++ .../ExplicitRelationCollectionRector.php | 100 ++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/ExplicitRelationCollectionRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/Fixture/some_class.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector.php diff --git a/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/ExplicitRelationCollectionRectorTest.php b/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/ExplicitRelationCollectionRectorTest.php new file mode 100644 index 00000000..cd9c37e9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/ExplicitRelationCollectionRectorTest.php @@ -0,0 +1,27 @@ +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/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/Fixture/some_class.php.inc b/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/Fixture/some_class.php.inc new file mode 100644 index 00000000..dc4bc085 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/Fixture/some_class.php.inc @@ -0,0 +1,38 @@ + +----- +items = new ArrayCollection(); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/config/configured_rule.php new file mode 100644 index 00000000..5d8990f6 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(ExplicitRelationCollectionRector::class); +}; diff --git a/rules/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector.php b/rules/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector.php new file mode 100644 index 00000000..49ad3578 --- /dev/null +++ b/rules/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector.php @@ -0,0 +1,100 @@ +items = new ArrayCollection(); + } +} +CODE_SAMPLE + ), + ]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param Class_ $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->attrinationFinder->hasByOne($node, 'Doctrine\ORM\Mapping\Entity')) { + return null; + } + + foreach ($node->getProperties() as $property) { + if (! $this->attrinationFinder->hasByOne($property, 'Doctrine\ORM\Mapping\OneToMany')) { + continue; + } + + // make sure has collection + if (! $property->type instanceof Node) { + $property->type = new FullyQualified('Doctrine\Common\Collections\Collection'); + } + + // make sure is null + if ($property->props[0]->default instanceof Expr) { + $property->props[0]->default = null; + } + } + + return $node; + } +}