diff --git a/rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_union_from_property_docblock.php.inc b/rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_union_from_property_docblock.php.inc new file mode 100644 index 00000000..ef0f957d --- /dev/null +++ b/rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_union_from_property_docblock.php.inc @@ -0,0 +1,43 @@ +|null + */ + private Collection $collection; + + public function __construct() + { + $this->collection = new ArrayCollection([]); + } +} + +?> +----- + + */ + private Collection $collection; + + public function __construct() + { + $this->collection = new ArrayCollection([]); + } +} + +?> diff --git a/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php b/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php index 42d94f16..b06cf1e2 100644 --- a/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php +++ b/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php @@ -10,7 +10,9 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Comments\NodeDocBlock\DocBlockUpdater; @@ -137,6 +139,31 @@ private function refactorProperty(Property $property): ?Property return null; } + if ($varTagValueNode->type instanceof UnionTypeNode) { + $hasChanged = false; + + $unionTypeNode = $varTagValueNode->type; + + foreach ($unionTypeNode->types as $key => $unionedType) { + if ($unionedType instanceof IdentifierTypeNode && $unionedType->name === 'null') { + unset($unionTypeNode->types[$key]); + $hasChanged = true; + } + } + + if ($hasChanged) { + // only one type left, lets use it directly + if (count($unionTypeNode->types) === 1) { + $onlyType = array_pop($unionTypeNode->types); + $varTagValueNode->type = $onlyType; + } + + $this->updateVarTagValueNode($phpDocInfo, $varTagValueNode, $property); + + return $property; + } + } + // remove nullable if has one if (! $varTagValueNode->type instanceof NullableTypeNode) { return null; @@ -144,11 +171,7 @@ private function refactorProperty(Property $property): ?Property // unwrap nullable type $varTagValueNode->type = $varTagValueNode->type->type; - - $phpDocInfo->removeByType(VarTagValueNode::class); - $phpDocInfo->addTagValueNode($varTagValueNode); - - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); + $this->updateVarTagValueNode($phpDocInfo, $varTagValueNode, $property); return $property; } @@ -161,4 +184,16 @@ private function hasNativeCollectionType(Property $property): bool return $this->isName($property->type, DoctrineClass::COLLECTION); } + + private function updateVarTagValueNode( + PhpDocInfo $phpDocInfo, + VarTagValueNode $varTagValueNode, + Property $property + ): void { + + $phpDocInfo->removeByType(VarTagValueNode::class); + $phpDocInfo->addTagValueNode($varTagValueNode); + + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); + } }