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

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

use Doctrine\Common\Collections\ArrayCollection;

final class UnionDocblock
{
/**
* @var ArrayCollection|string[]
*/
private $items;
}

?>
-----
<?php

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

use Doctrine\Common\Collections\ArrayCollection;

final class UnionDocblock
{
/**
* @var \Doctrine\Common\Collections\Collection|string[]
*/
private $items;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
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 @@ -85,28 +86,12 @@ public function refactor(Node $node): ClassMethod|Property|null
return $this->refactorClassMethod($node);
}

$hasChanged = false;

if ($this->processNativeType($node)) {
$hasChanged = true;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if ($varTagValueNode instanceof VarTagValueNode && $this->processTagValueNode($varTagValueNode)) {
$hasChanged = true;
}

if ($hasChanged) {
return $node;
}

return null;
return $this->refactorProperty($node);
}

private function processTagValueNode(VarTagValueNode|ReturnTagValueNode|ParamTagValueNode $tagValueNode): bool
{
// 1. generic type
if ($tagValueNode->type instanceof GenericTypeNode) {
$genericTypeNode = $tagValueNode->type;
if ($genericTypeNode->type->name === 'ArrayCollection') {
Expand All @@ -115,6 +100,25 @@ private function processTagValueNode(VarTagValueNode|ReturnTagValueNode|ParamTag
}
}

// 2. union type
if ($tagValueNode->type instanceof UnionTypeNode) {
$unionTypeNode = $tagValueNode->type;
foreach ($unionTypeNode->types as $key => $unionedType) {
if (! $unionedType instanceof IdentifierTypeNode) {
continue;
}

if (! in_array($unionedType->name, ['ArrayCollection', DoctrineClass::ARRAY_COLLECTION], true)) {
continue;
}

$unionTypeNode->types[$key] = new IdentifierTypeNode('\\' . DoctrineClass::COLLECTION);

return true;
}
}

// 3. handle single type
if (! $tagValueNode->type instanceof IdentifierTypeNode) {
return false;
}
Expand Down Expand Up @@ -181,23 +185,10 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod

// docblocks
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $classMethodPhpDocInfo instanceof PhpDocInfo) {
return null;
}

// return tag
$returnTagValueNode = $classMethodPhpDocInfo->getReturnTagValue();
if ($returnTagValueNode instanceof ReturnTagValueNode && $this->processTagValueNode($returnTagValueNode)) {
if ($classMethodPhpDocInfo instanceof PhpDocInfo && $this->refactorClassMethodDocblock($classMethodPhpDocInfo)) {
$hasChanged = true;
}

// param tags
foreach ($classMethodPhpDocInfo->getParamTagValueNodes() as $paramTagValueNode) {
if ($this->processTagValueNode($paramTagValueNode)) {
$hasChanged = true;
}
}

if (! $hasChanged) {
return null;
}
Expand Down Expand Up @@ -264,4 +255,47 @@ private function hasCollectionName(Property|Param|ClassMethod $stmts): bool

return $collectionName instanceof Name;
}

private function refactorClassMethodDocblock(PhpDocInfo $classMethodPhpDocInfo): bool
{
$hasChanged = false;

// return tag
$returnTagValueNode = $classMethodPhpDocInfo->getReturnTagValue();
if ($returnTagValueNode instanceof ReturnTagValueNode && $this->processTagValueNode($returnTagValueNode)) {
$hasChanged = true;
}

// param tags
foreach ($classMethodPhpDocInfo->getParamTagValueNodes() as $paramTagValueNode) {
if ($this->processTagValueNode($paramTagValueNode)) {
$hasChanged = true;
}
}

return $hasChanged;
}

private function refactorProperty(Property $property): Property|null
{
$hasChanged = false;
if ($this->processNativeType($property)) {
$hasChanged = true;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if ($varTagValueNode instanceof VarTagValueNode && $this->processTagValueNode($varTagValueNode)) {
$hasChanged = true;
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
}

if ($hasChanged) {

return $property;
}

return null;
}
}