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,51 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNullFromNullableCollectionTypeRector\Fixture;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

final class RemoveDefaultNull
{
/**
* @MongoDB\EmbedMany()
* @var Collection<int, string>|null
* @Assert\Valid()
*/
private ?Collection $collection = null;

public function __construct()
{
$this->collection = new ArrayCollection([]);
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNullFromNullableCollectionTypeRector\Fixture;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

final class RemoveDefaultNull
{
/**
* @MongoDB\EmbedMany()
* @var Collection<int, string>|null
* @Assert\Valid()
*/
private Collection $collection;

public function __construct()
{
$this->collection = new ArrayCollection([]);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\Doctrine\NodeAnalyzer\ConstructorAssignPropertyAnalyzer;
use Rector\Doctrine\NodeFactory\ValueAssignFactory;
use Rector\Doctrine\NodeManipulator\ConstructorManipulator;
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
Expand All @@ -42,6 +43,7 @@ public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ValueResolver $valueResolver,
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
) {
}

Expand Down Expand Up @@ -189,8 +191,7 @@ private function refactorClassWithRemovalDefault(Class_ $class, Property $proper
}

// 3. remove default from property
$onlyProperty = $property->props[0];
$onlyProperty->default = null;
$this->propertyDefaultNullRemover->remove($property);

$this->hasChanged = true;
}
Expand Down
21 changes: 21 additions & 0 deletions rules/TypedCollections/NodeModifier/PropertyDefaultNullRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\TypedCollections\NodeModifier;

use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Property;

final class PropertyDefaultNullRemover
{
public function remove(Property $property): void
{
$soleProperty = $property->props[0];
if (! $soleProperty->default instanceof Expr) {
return;
}

$soleProperty->default = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -32,7 +33,8 @@ public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly StaticTypeMapper $staticTypeMapper
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
) {
}

Expand Down Expand Up @@ -132,6 +134,8 @@ private function refactorProperty(Property $property): ?Property
// unwrap nullable type
$property->type = $property->type->type;

$this->propertyDefaultNullRemover->remove($property);

return $property;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Rector\Doctrine\TypedCollections\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use Rector\Doctrine\NodeFactory\ArrayCollectionAssignFactory;
use Rector\Doctrine\TypedCollections\NodeAnalyzer\EntityLikeClassDetector;
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
use Rector\NodeManipulator\ClassDependencyManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand All @@ -29,6 +29,7 @@ public function __construct(
private readonly ArrayCollectionAssignFactory $arrayCollectionAssignFactory,
private readonly ClassDependencyManipulator $classDependencyManipulator,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
) {
}

Expand Down Expand Up @@ -108,9 +109,7 @@ public function refactor(Node $node): ?Node
}

// make sure is null
if ($property->props[0]->default instanceof Expr) {
$property->props[0]->default = null;
}
$this->propertyDefaultNullRemover->remove($property);

/** @var string $propertyName */
$propertyName = $this->getName($property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Common\Collections\Collection;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -18,6 +17,7 @@
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\Doctrine\TypedCollections\DocBlockProcessor\UnionCollectionTagValueNodeNarrower;
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -30,7 +30,8 @@ final class NarrowPropertyUnionToCollectionRector extends AbstractRector
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly UnionCollectionTagValueNodeNarrower $unionCollectionTagValueNodeNarrower
private readonly UnionCollectionTagValueNodeNarrower $unionCollectionTagValueNodeNarrower,
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
) {
}

Expand Down Expand Up @@ -133,9 +134,7 @@ private function refactorNativePropertyType(Property $property): bool
$property->type = new FullyQualified(DoctrineClass::COLLECTION);

// remove default, as will be defined in constructor by another rule
if ($property->props[0]->default instanceof Expr) {
$property->props[0]->default = null;
}
$this->propertyDefaultNullRemover->remove($property);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\UnionType;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\Doctrine\NodeManipulator\ToManyRelationPropertyTypeResolver;
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -27,6 +28,7 @@ final class TypedPropertyFromToManyRelationTypeRector extends AbstractRector imp
public function __construct(
private readonly ToManyRelationPropertyTypeResolver $toManyRelationPropertyTypeResolver,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover,
) {
}

Expand Down Expand Up @@ -102,9 +104,7 @@ public function refactor(Node $node): ?Property
}

// remove default null value if any
if ($node->props[0]->default !== null) {
$node->props[0]->default = null;
}
$this->propertyDefaultNullRemover->remove($node);

if (! $propertyType instanceof UnionType) {
$node->type = $typeNode;
Expand Down