Skip to content

Commit 6eb6285

Browse files
authored
remover default null (#444)
1 parent 606ed78 commit 6eb6285

7 files changed

Lines changed: 90 additions & 15 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNullFromNullableCollectionTypeRector\Fixture;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
10+
final class RemoveDefaultNull
11+
{
12+
/**
13+
* @MongoDB\EmbedMany()
14+
* @var Collection<int, string>|null
15+
* @Assert\Valid()
16+
*/
17+
private ?Collection $collection = null;
18+
19+
public function __construct()
20+
{
21+
$this->collection = new ArrayCollection([]);
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNullFromNullableCollectionTypeRector\Fixture;
30+
31+
use Doctrine\Common\Collections\ArrayCollection;
32+
use Doctrine\Common\Collections\Collection;
33+
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
34+
use Symfony\Component\Validator\Constraints as Assert;
35+
36+
final class RemoveDefaultNull
37+
{
38+
/**
39+
* @MongoDB\EmbedMany()
40+
* @var Collection<int, string>|null
41+
* @Assert\Valid()
42+
*/
43+
private Collection $collection;
44+
45+
public function __construct()
46+
{
47+
$this->collection = new ArrayCollection([]);
48+
}
49+
}
50+
51+
?>

rules/CodeQuality/Rector/Class_/MoveCurrentDateTimeDefaultInEntityToConstructorRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Rector\Doctrine\NodeAnalyzer\ConstructorAssignPropertyAnalyzer;
2121
use Rector\Doctrine\NodeFactory\ValueAssignFactory;
2222
use Rector\Doctrine\NodeManipulator\ConstructorManipulator;
23+
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
2324
use Rector\PhpParser\Node\Value\ValueResolver;
2425
use Rector\Rector\AbstractRector;
2526
use Rector\ValueObject\MethodName;
@@ -42,6 +43,7 @@ public function __construct(
4243
private readonly DocBlockUpdater $docBlockUpdater,
4344
private readonly PhpDocInfoFactory $phpDocInfoFactory,
4445
private readonly ValueResolver $valueResolver,
46+
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
4547
) {
4648
}
4749

@@ -189,8 +191,7 @@ private function refactorClassWithRemovalDefault(Class_ $class, Property $proper
189191
}
190192

191193
// 3. remove default from property
192-
$onlyProperty = $property->props[0];
193-
$onlyProperty->default = null;
194+
$this->propertyDefaultNullRemover->remove($property);
194195

195196
$this->hasChanged = true;
196197
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\TypedCollections\NodeModifier;
6+
7+
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Stmt\Property;
9+
10+
final class PropertyDefaultNullRemover
11+
{
12+
public function remove(Property $property): void
13+
{
14+
$soleProperty = $property->props[0];
15+
if (! $soleProperty->default instanceof Expr) {
16+
return;
17+
}
18+
19+
$soleProperty->default = null;
20+
}
21+
}

rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1818
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
1919
use Rector\Doctrine\Enum\DoctrineClass;
20+
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
2021
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2122
use Rector\Rector\AbstractRector;
2223
use Rector\StaticTypeMapper\StaticTypeMapper;
@@ -32,7 +33,8 @@ public function __construct(
3233
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
3334
private readonly PhpDocInfoFactory $phpDocInfoFactory,
3435
private readonly PhpDocTypeChanger $phpDocTypeChanger,
35-
private readonly StaticTypeMapper $staticTypeMapper
36+
private readonly StaticTypeMapper $staticTypeMapper,
37+
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
3638
) {
3739
}
3840

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

137+
$this->propertyDefaultNullRemover->remove($property);
138+
135139
return $property;
136140
}
137141

rules/TypedCollections/Rector/Class_/InitializeCollectionInConstructorRector.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace Rector\Doctrine\TypedCollections\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr;
98
use PhpParser\Node\Stmt\Class_;
109
use Rector\Doctrine\NodeFactory\ArrayCollectionAssignFactory;
1110
use Rector\Doctrine\TypedCollections\NodeAnalyzer\EntityLikeClassDetector;
11+
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
1212
use Rector\NodeManipulator\ClassDependencyManipulator;
1313
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1414
use Rector\Rector\AbstractRector;
@@ -29,6 +29,7 @@ public function __construct(
2929
private readonly ArrayCollectionAssignFactory $arrayCollectionAssignFactory,
3030
private readonly ClassDependencyManipulator $classDependencyManipulator,
3131
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
32+
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
3233
) {
3334
}
3435

@@ -108,9 +109,7 @@ public function refactor(Node $node): ?Node
108109
}
109110

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

115114
/** @var string $propertyName */
116115
$propertyName = $this->getName($property);

rules/TypedCollections/Rector/Property/NarrowPropertyUnionToCollectionRector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Doctrine\Common\Collections\Collection;
88
use PhpParser\Node;
9-
use PhpParser\Node\Expr;
109
use PhpParser\Node\Name;
1110
use PhpParser\Node\Name\FullyQualified;
1211
use PhpParser\Node\Stmt\Class_;
@@ -18,6 +17,7 @@
1817
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1918
use Rector\Doctrine\Enum\DoctrineClass;
2019
use Rector\Doctrine\TypedCollections\DocBlockProcessor\UnionCollectionTagValueNodeNarrower;
20+
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
2121
use Rector\Rector\AbstractRector;
2222
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2323
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -30,7 +30,8 @@ final class NarrowPropertyUnionToCollectionRector extends AbstractRector
3030
public function __construct(
3131
private readonly PhpDocInfoFactory $phpDocInfoFactory,
3232
private readonly DocBlockUpdater $docBlockUpdater,
33-
private readonly UnionCollectionTagValueNodeNarrower $unionCollectionTagValueNodeNarrower
33+
private readonly UnionCollectionTagValueNodeNarrower $unionCollectionTagValueNodeNarrower,
34+
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover
3435
) {
3536
}
3637

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

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

140139
return true;
141140
}

rules/TypedCollections/Rector/Property/TypedPropertyFromToManyRelationTypeRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\UnionType;
1212
use Rector\Doctrine\Enum\DoctrineClass;
1313
use Rector\Doctrine\NodeManipulator\ToManyRelationPropertyTypeResolver;
14+
use Rector\Doctrine\TypedCollections\NodeModifier\PropertyDefaultNullRemover;
1415
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1516
use Rector\Rector\AbstractRector;
1617
use Rector\StaticTypeMapper\StaticTypeMapper;
@@ -27,6 +28,7 @@ final class TypedPropertyFromToManyRelationTypeRector extends AbstractRector imp
2728
public function __construct(
2829
private readonly ToManyRelationPropertyTypeResolver $toManyRelationPropertyTypeResolver,
2930
private readonly StaticTypeMapper $staticTypeMapper,
31+
private readonly PropertyDefaultNullRemover $propertyDefaultNullRemover,
3032
) {
3133
}
3234

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

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

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

0 commit comments

Comments
 (0)