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

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

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

final class RemoveUnionFromPropertyDocblock
{
/**
* @var Collection<int, string>|null
*/
private Collection $collection;

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;

final class RemoveUnionFromPropertyDocblock
{
/**
* @var Collection<int, string>
*/
private Collection $collection;

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

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,18 +139,39 @@ 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;
}

// 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;
}
Expand All @@ -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);
}
}