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

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

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

final class PropertyWithOtherDoc
{
/**
* @MongoDB\EmbedMany()
* @var Collection<int, string>|null
* @Assert\Valid()
*/
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;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

final class PropertyWithOtherDoc
{
/**
* @MongoDB\EmbedMany()
* @var \Doctrine\Common\Collections\Collection<int, string>
* @Assert\Valid()
*/
private Collection $collection;

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

?>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use Doctrine\Common\Collections\Collection;
final class RemoveFromPropertyDocblock
{
/**
* @var Collection<int, string>
* @var \Doctrine\Common\Collections\Collection<int, string>
*/
private Collection $collection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use Doctrine\Common\Collections\Collection;
final class RemoveUnionFromPropertyDocblock
{
/**
* @var Collection<int, string>
* @var \Doctrine\Common\Collections\Collection<int, string>
*/
private Collection $collection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,7 +31,8 @@ final class RemoveNullFromNullableCollectionTypeRector extends AbstractRector
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly StaticTypeMapper $staticTypeMapper
) {
}

Expand Down Expand Up @@ -96,6 +98,7 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod
return null;
}

// nullable might be on purpose, e.g. via data provider
if ($this->testsNodeAnalyzer->isInTestClass($classMethod)) {
return null;
}
Expand Down Expand Up @@ -155,10 +158,13 @@ private function refactorProperty(Property $property): ?Property
// only one type left, lets use it directly
if (count($unionTypeNode->types) === 1) {
$onlyType = array_pop($unionTypeNode->types);
$varTagValueNode->type = $onlyType;
$finalType = $onlyType;
} else {
$finalType = $unionTypeNode;
}

$this->updateVarTagValueNode($phpDocInfo, $varTagValueNode, $property);
$finalType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($finalType, $property);
$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $finalType);

return $property;
}
Expand All @@ -170,8 +176,12 @@ private function refactorProperty(Property $property): ?Property
}

// unwrap nullable type
$varTagValueNode->type = $varTagValueNode->type->type;
$this->updateVarTagValueNode($phpDocInfo, $varTagValueNode, $property);
$finalType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
$varTagValueNode->type->type,
$property
);

$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $finalType);

return $property;
}
Expand All @@ -184,16 +194,4 @@ 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);
}
}