-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule - ExplicitRelationCollectionRector
- Loading branch information
1 parent
a1d50bb
commit cee4ddd
Showing
9 changed files
with
283 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...y/Rector/Class_/ExplicitRelationCollectionRector/ExplicitRelationCollectionRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ExplicitRelationCollectionRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): \Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...deQuality/Rector/Class_/ExplicitRelationCollectionRector/Fixture/already_assigned.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
|
||
#[Entity] | ||
class AlreadyAssigned | ||
{ | ||
#[OneToMany(targetEntity: 'SomeClass')] | ||
private $items = []; | ||
public function __construct() | ||
{ | ||
$this->items = new ArrayCollection(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
|
||
#[Entity] | ||
class AlreadyAssigned | ||
{ | ||
#[OneToMany(targetEntity: 'SomeClass')] | ||
private \Doctrine\Common\Collections\Collection $items; | ||
public function __construct() | ||
{ | ||
$this->items = new ArrayCollection(); | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...sts/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
|
||
#[Entity] | ||
class SomeClass | ||
{ | ||
#[OneToMany(targetEntity: 'SomeClass')] | ||
private $items = []; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
|
||
#[Entity] | ||
class SomeClass | ||
{ | ||
#[OneToMany(targetEntity: 'SomeClass')] | ||
private \Doctrine\Common\Collections\Collection $items; | ||
public function __construct() | ||
{ | ||
$this->items = new \Doctrine\Common\Collections\ArrayCollection(); | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...sts/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Doctrine\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ExplicitRelationCollectionRector::class); | ||
}; |
124 changes: 124 additions & 0 deletions
124
rules/CodeQuality/Rector/Class_/ExplicitRelationCollectionRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\CodeQuality\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder; | ||
use Rector\Doctrine\NodeFactory\ArrayCollectionAssignFactory; | ||
use Rector\NodeManipulator\ClassDependencyManipulator; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\ExplicitRelationCollectionRectorTest | ||
*/ | ||
final class ExplicitRelationCollectionRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly AttrinationFinder $attrinationFinder, | ||
private readonly ConstructorAssignDetector $constructorAssignDetector, | ||
private readonly ArrayCollectionAssignFactory $arrayCollectionAssignFactory, | ||
private readonly ClassDependencyManipulator $classDependencyManipulator, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Use explicit collection in one-to-many relations of Doctrine entity', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
use Doctrine\ORM\Mapping\Entity; | ||
#[Entity] | ||
class SomeClass | ||
{ | ||
#[OneToMany(targetEntity: 'SomeClass')] | ||
private $items = []; | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
#[Entity] | ||
class SomeClass | ||
{ | ||
#[OneToMany(targetEntity: 'SomeClass')] | ||
private Collection $items; | ||
public function __construct() | ||
{ | ||
$this->items = new ArrayCollection(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
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; | ||
} | ||
|
||
$arrayCollectionAssigns = []; | ||
|
||
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; | ||
} | ||
|
||
/** @var string $propertyName */ | ||
$propertyName = $this->getName($property); | ||
if ($this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) { | ||
continue; | ||
} | ||
|
||
$arrayCollectionAssigns[] = $this->arrayCollectionAssignFactory->createFromPropertyName($propertyName); | ||
|
||
// make sure it is initialized in constructor | ||
} | ||
|
||
if ($arrayCollectionAssigns === []) { | ||
return null; | ||
} | ||
|
||
$this->classDependencyManipulator->addStmtsToConstructorIfNotThereYet($node, $arrayCollectionAssigns); | ||
|
||
return $node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters