Skip to content

Commit c4e86a8

Browse files
committed
remove nullable property type
1 parent bc57ed7 commit c4e86a8

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
8+
final class RemoveFromNullablePropertyType
9+
{
10+
/**
11+
* @var Collection<int, string>
12+
*/
13+
private ?Collection $collection;
14+
15+
public function __construct()
16+
{
17+
$this->collection = new ArrayCollection([]);
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNullFromNullableCollectionTypeRector\Fixture;
26+
27+
use Doctrine\Common\Collections\ArrayCollection;
28+
use Doctrine\Common\Collections\Collection;
29+
30+
final class RemoveFromNullablePropertyType
31+
{
32+
/**
33+
* @var Collection<int, string>
34+
*/
35+
private Collection $collection;
36+
37+
public function __construct()
38+
{
39+
$this->collection = new ArrayCollection([]);
40+
}
41+
}
42+
43+
?>

rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
public function getRuleDefinition(): RuleDefinition
4040
{
4141
return new RuleDefinition(
42-
'Remove null from a nullable Collection, as empty ArrayCollection is preferred instead to keep property type strict and always a collection',
42+
'Remove null from a nullable Collection, as empty ArrayCollection is preferred instead to keep node type strict and always a collection',
4343
[
4444
new CodeSample(
4545
<<<'CODE_SAMPLE'
@@ -128,6 +128,13 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod
128128

129129
private function refactorProperty(Property $property): ?Property
130130
{
131+
if ($property->type instanceof NullableType && $this->hasNativeCollectionType($property->type)) {
132+
// unwrap nullable type
133+
$property->type = $property->type->type;
134+
135+
return $property;
136+
}
137+
131138
if (! $this->hasNativeCollectionType($property)) {
132139
return null;
133140
}
@@ -186,12 +193,12 @@ private function refactorProperty(Property $property): ?Property
186193
return $property;
187194
}
188195

189-
private function hasNativeCollectionType(Property $property): bool
196+
private function hasNativeCollectionType(Property|NullableType $node): bool
190197
{
191-
if (! $property->type instanceof Name) {
198+
if (! $node->type instanceof Name) {
192199
return false;
193200
}
194201

195-
return $this->isName($property->type, DoctrineClass::COLLECTION);
202+
return $this->isName($node->type, DoctrineClass::COLLECTION);
196203
}
197204
}

0 commit comments

Comments
 (0)