diff --git a/rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_default_null.php.inc b/rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_default_null.php.inc new file mode 100644 index 00000000..64afaa12 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_default_null.php.inc @@ -0,0 +1,51 @@ +|null + * @Assert\Valid() + */ + private ?Collection $collection = null; + + public function __construct() + { + $this->collection = new ArrayCollection([]); + } +} + +?> +----- +|null + * @Assert\Valid() + */ + private Collection $collection; + + public function __construct() + { + $this->collection = new ArrayCollection([]); + } +} + +?> diff --git a/rules/CodeQuality/Rector/Class_/MoveCurrentDateTimeDefaultInEntityToConstructorRector.php b/rules/CodeQuality/Rector/Class_/MoveCurrentDateTimeDefaultInEntityToConstructorRector.php index 1f443af3..57159595 100644 --- a/rules/CodeQuality/Rector/Class_/MoveCurrentDateTimeDefaultInEntityToConstructorRector.php +++ b/rules/CodeQuality/Rector/Class_/MoveCurrentDateTimeDefaultInEntityToConstructorRector.php @@ -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; @@ -42,6 +43,7 @@ public function __construct( private readonly DocBlockUpdater $docBlockUpdater, private readonly PhpDocInfoFactory $phpDocInfoFactory, private readonly ValueResolver $valueResolver, + private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover ) { } @@ -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; } diff --git a/rules/TypedCollections/NodeModifier/PropertyDefaultNullRemover.php b/rules/TypedCollections/NodeModifier/PropertyDefaultNullRemover.php new file mode 100644 index 00000000..9ab38f8e --- /dev/null +++ b/rules/TypedCollections/NodeModifier/PropertyDefaultNullRemover.php @@ -0,0 +1,21 @@ +props[0]; + if (! $soleProperty->default instanceof Expr) { + return; + } + + $soleProperty->default = null; + } +} diff --git a/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php b/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php index 5deae516..980ffb10 100644 --- a/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php +++ b/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php @@ -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; @@ -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 ) { } @@ -132,6 +134,8 @@ private function refactorProperty(Property $property): ?Property // unwrap nullable type $property->type = $property->type->type; + $this->propertyDefaultNullRemover->remove($property); + return $property; } diff --git a/rules/TypedCollections/Rector/Class_/InitializeCollectionInConstructorRector.php b/rules/TypedCollections/Rector/Class_/InitializeCollectionInConstructorRector.php index 681335d7..e5d6b5f5 100644 --- a/rules/TypedCollections/Rector/Class_/InitializeCollectionInConstructorRector.php +++ b/rules/TypedCollections/Rector/Class_/InitializeCollectionInConstructorRector.php @@ -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; @@ -29,6 +29,7 @@ public function __construct( private readonly ArrayCollectionAssignFactory $arrayCollectionAssignFactory, private readonly ClassDependencyManipulator $classDependencyManipulator, private readonly TestsNodeAnalyzer $testsNodeAnalyzer, + private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover ) { } @@ -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); diff --git a/rules/TypedCollections/Rector/Property/NarrowPropertyUnionToCollectionRector.php b/rules/TypedCollections/Rector/Property/NarrowPropertyUnionToCollectionRector.php index 4260d711..adffb723 100644 --- a/rules/TypedCollections/Rector/Property/NarrowPropertyUnionToCollectionRector.php +++ b/rules/TypedCollections/Rector/Property/NarrowPropertyUnionToCollectionRector.php @@ -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_; @@ -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; @@ -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 ) { } @@ -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; } diff --git a/rules/TypedCollections/Rector/Property/TypedPropertyFromToManyRelationTypeRector.php b/rules/TypedCollections/Rector/Property/TypedPropertyFromToManyRelationTypeRector.php index e12db832..e6a1bce4 100644 --- a/rules/TypedCollections/Rector/Property/TypedPropertyFromToManyRelationTypeRector.php +++ b/rules/TypedCollections/Rector/Property/TypedPropertyFromToManyRelationTypeRector.php @@ -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; @@ -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, ) { } @@ -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;