Skip to content

Commit

Permalink
Deprecate InitializeDefaultEntityCollectionRector, use ExplicitRelati…
Browse files Browse the repository at this point in the history
…onCollectionRector instead (#304)

* cover many to many as well

* add annotation test

* deprecate InitializeDefaultEntityCollectionRector, use ExplicitRelationCollectionRector instead
  • Loading branch information
TomasVotruba authored Mar 22, 2024
1 parent d09e8a0 commit bbc48cc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 5 deletions.
6 changes: 3 additions & 3 deletions config/sets/doctrine-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\Config\RectorConfig;
use Rector\Doctrine\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector;
use Rector\Doctrine\CodeQuality\Rector\Class_\InitializeDefaultEntityCollectionRector;
use Rector\Doctrine\CodeQuality\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector;
use Rector\Doctrine\CodeQuality\Rector\Class_\RemoveEmptyTableAttributeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;
Expand All @@ -22,9 +21,10 @@
MakeEntityDateTimePropertyDateTimeInterfaceRector::class,
MoveCurrentDateTimeDefaultInEntityToConstructorRector::class,
CorrectDefaultTypesOnEntityPropertyRector::class,
ImproveDoctrineCollectionDocTypeInEntityRector::class,
InitializeDefaultEntityCollectionRector::class,

ExplicitRelationCollectionRector::class,
ImproveDoctrineCollectionDocTypeInEntityRector::class,

RemoveEmptyTableAttributeRector::class,

// typed properties in entities from annotations/attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

Expand All @@ -15,7 +16,7 @@ public function test(string $filePath): void
$this->doTestFile($filePath);
}

public static function provideData(): \Iterator
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToMany;

/**
* @Entity
*/
class IncludeAnnotation
{
/**
* @ManyToMany(targetEntity="SomeClass")
*/
private $items = [];
public function __construct()
{
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToMany;

/**
* @Entity
*/
class IncludeAnnotation
{
/**
* @ManyToMany(targetEntity="SomeClass")
*/
private \Doctrine\Common\Collections\Collection $items;
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToMany;

#[Entity]
class SomeManyToMany
{
#[ManyToMany(targetEntity: 'SomeClass')]
private $items = [];
public function __construct()
{
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\Fixture;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToMany;

#[Entity]
class SomeManyToMany
{
#[ManyToMany(targetEntity: 'SomeClass')]
private \Doctrine\Common\Collections\Collection $items;
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector\ExplicitRelationCollectionRectorTest
*
* @changelog https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/best-practices.html#initialize-collections-in-the-constructor
*/
final class ExplicitRelationCollectionRector extends AbstractRector
{
Expand Down Expand Up @@ -88,7 +90,10 @@ public function refactor(Node $node): ?Node
$arrayCollectionAssigns = [];

foreach ($node->getProperties() as $property) {
if (! $this->attrinationFinder->hasByOne($property, 'Doctrine\ORM\Mapping\OneToMany')) {
if (! $this->attrinationFinder->hasByMany($property, [
'Doctrine\ORM\Mapping\OneToMany',
'Doctrine\ORM\Mapping\ManyToMany',
])) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/best-practices.html#initialize-collections-in-the-constructor
*
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Class_\InitializeDefaultEntityCollectionRector\InitializeDefaultEntityCollectionRectorTest
*
* @deprecated This rule can create incomplete assign of object to an array. Use the @see \Rector\Doctrine\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector instead.
*/
final class InitializeDefaultEntityCollectionRector extends AbstractRector
{
Expand Down

0 comments on commit bbc48cc

Please sign in to comment.