Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Source\RelatedEntity;

/**
* @MongoDB\Document()
*/
final class IncludeBareCollection
{
/**
* @var Collection<int, RelatedEntity>
*/
protected Collection $items;
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Source\RelatedEntity;

/**
* @MongoDB\Document()
*/
final class IncludeBareCollection
{
/**
* @var Collection<int, RelatedEntity>
*/
protected 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,45 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Source\RelatedEntity;

/**
* @MongoDB\Document()
*/
final class IncludeBareNullableCollection
{
/**
* @var Collection<int, RelatedEntity>
*/
protected ?Collection $items;
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Rector\Doctrine\Tests\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector\Source\RelatedEntity;

/**
* @MongoDB\Document()
*/
final class IncludeBareNullableCollection
{
/**
* @var Collection<int, RelatedEntity>
*/
protected ?Collection $items;
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
}

?>
53 changes: 53 additions & 0 deletions rules/TypedCollections/NodeAnalyzer/CollectionPropertyDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\TypedCollections\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\NodeNameResolver\NodeNameResolver;

final readonly class CollectionPropertyDetector
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {

}

public function detect(Property $property): bool
{
if (! $property->type instanceof Node) {
return false;
}

// 1. direct type
if ($this->nodeNameResolver->isName($property->type, DoctrineClass::COLLECTION)) {
return true;
}

// 2. union type
if ($property->type instanceof UnionType) {
$unionType = $property->type;
foreach ($unionType->types as $unionedType) {
if ($this->nodeNameResolver->isName($unionedType, DoctrineClass::COLLECTION)) {
return true;
}
}
}

// 3. nullable type
if ($property->type instanceof NullableType) {
$directType = $property->type->type;
if ($this->nodeNameResolver->isName($directType, DoctrineClass::COLLECTION)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Doctrine\NodeFactory\ArrayCollectionAssignFactory;
use Rector\Doctrine\TypedCollections\NodeAnalyzer\CollectionPropertyDetector;
use Rector\Doctrine\TypedCollections\NodeAnalyzer\EntityLikeClassDetector;
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
use Rector\NodeManipulator\ClassDependencyManipulator;
Expand All @@ -29,7 +30,8 @@ public function __construct(
private readonly ArrayCollectionAssignFactory $arrayCollectionAssignFactory,
private readonly ClassDependencyManipulator $classDependencyManipulator,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover,
private readonly CollectionPropertyDetector $collectionPropertyDetector
) {
}

Expand Down Expand Up @@ -89,22 +91,13 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->entityLikeClassDetector->detect($node)) {
return null;
}

if ($this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if ($node->isAbstract()) {
if ($this->shouldSkipClass($node)) {
return null;
}

$arrayCollectionAssigns = [];

foreach ($node->getProperties() as $property) {
if (! $this->entityLikeClassDetector->isToMany($property)) {
if (! $this->isDefaultArrayCollectionPropertyCandidate($property)) {
continue;
}

Expand All @@ -128,4 +121,26 @@ public function refactor(Node $node): ?Node

return $node;
}

private function shouldSkipClass(Class_ $class): bool
{
if (! $this->entityLikeClassDetector->detect($class)) {
return true;
}

if ($this->testsNodeAnalyzer->isInTestClass($class)) {
return true;
}

return $class->isAbstract();
}

private function isDefaultArrayCollectionPropertyCandidate(mixed $property): bool
{
if ($this->entityLikeClassDetector->isToMany($property)) {
return true;
}

return $this->collectionPropertyDetector->detect($property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function refactor(Node $node): ?Class_
$hasChanged = true;
}

if ($this->refactorNativePropertyType($property)) {
if ($this->refactorNativeUnionPropertyType($property)) {
$hasChanged = true;
}
}
Expand Down Expand Up @@ -119,7 +119,7 @@ private function isCollectionName(Node $node): bool
return $this->isName($node, DoctrineClass::COLLECTION);
}

private function refactorNativePropertyType(Property $property): bool
private function refactorNativeUnionPropertyType(Property $property): bool
{
if (! $property->type instanceof UnionType) {
return false;
Expand Down