Skip to content

Commit 079dbef

Browse files
authored
add failing fixture (#432)
* add failing fixture * fixup! add failing fixture
1 parent 8389f0d commit 079dbef

4 files changed

Lines changed: 69 additions & 20 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
10+
final class PropertyWithOtherDoc
11+
{
12+
/**
13+
* @MongoDB\EmbedMany()
14+
* @var Collection<int, string>|null
15+
* @Assert\Valid()
16+
*/
17+
private Collection $collection;
18+
19+
public function __construct()
20+
{
21+
$this->collection = new ArrayCollection([]);
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNullFromNullableCollectionTypeRector\Fixture;
30+
31+
use Doctrine\Common\Collections\ArrayCollection;
32+
use Doctrine\Common\Collections\Collection;
33+
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
34+
use Symfony\Component\Validator\Constraints as Assert;
35+
36+
final class PropertyWithOtherDoc
37+
{
38+
/**
39+
* @MongoDB\EmbedMany()
40+
* @var \Doctrine\Common\Collections\Collection<int, string>
41+
* @Assert\Valid()
42+
*/
43+
private Collection $collection;
44+
45+
public function __construct()
46+
{
47+
$this->collection = new ArrayCollection([]);
48+
}
49+
}
50+
51+
?>

rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_from_property_docblock.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use Doctrine\Common\Collections\Collection;
3030
final class RemoveFromPropertyDocblock
3131
{
3232
/**
33-
* @var Collection<int, string>
33+
* @var \Doctrine\Common\Collections\Collection<int, string>
3434
*/
3535
private Collection $collection;
3636

rules-tests/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector/Fixture/remove_union_from_property_docblock.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use Doctrine\Common\Collections\Collection;
3030
final class RemoveUnionFromPropertyDocblock
3131
{
3232
/**
33-
* @var Collection<int, string>
33+
* @var \Doctrine\Common\Collections\Collection<int, string>
3434
*/
3535
private Collection $collection;
3636

rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
1616
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1717
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
18-
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
18+
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
1919
use Rector\Doctrine\Enum\DoctrineClass;
2020
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2121
use Rector\Rector\AbstractRector;
22+
use Rector\StaticTypeMapper\StaticTypeMapper;
2223
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2324
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2425

@@ -30,7 +31,8 @@ final class RemoveNullFromNullableCollectionTypeRector extends AbstractRector
3031
public function __construct(
3132
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
3233
private readonly PhpDocInfoFactory $phpDocInfoFactory,
33-
private readonly DocBlockUpdater $docBlockUpdater,
34+
private readonly PhpDocTypeChanger $phpDocTypeChanger,
35+
private readonly StaticTypeMapper $staticTypeMapper
3436
) {
3537
}
3638

@@ -96,6 +98,7 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod
9698
return null;
9799
}
98100

101+
// nullable might be on purpose, e.g. via data provider
99102
if ($this->testsNodeAnalyzer->isInTestClass($classMethod)) {
100103
return null;
101104
}
@@ -155,10 +158,13 @@ private function refactorProperty(Property $property): ?Property
155158
// only one type left, lets use it directly
156159
if (count($unionTypeNode->types) === 1) {
157160
$onlyType = array_pop($unionTypeNode->types);
158-
$varTagValueNode->type = $onlyType;
161+
$finalType = $onlyType;
162+
} else {
163+
$finalType = $unionTypeNode;
159164
}
160165

161-
$this->updateVarTagValueNode($phpDocInfo, $varTagValueNode, $property);
166+
$finalType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($finalType, $property);
167+
$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $finalType);
162168

163169
return $property;
164170
}
@@ -170,8 +176,12 @@ private function refactorProperty(Property $property): ?Property
170176
}
171177

172178
// unwrap nullable type
173-
$varTagValueNode->type = $varTagValueNode->type->type;
174-
$this->updateVarTagValueNode($phpDocInfo, $varTagValueNode, $property);
179+
$finalType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
180+
$varTagValueNode->type->type,
181+
$property
182+
);
183+
184+
$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $finalType);
175185

176186
return $property;
177187
}
@@ -184,16 +194,4 @@ private function hasNativeCollectionType(Property $property): bool
184194

185195
return $this->isName($property->type, DoctrineClass::COLLECTION);
186196
}
187-
188-
private function updateVarTagValueNode(
189-
PhpDocInfo $phpDocInfo,
190-
VarTagValueNode $varTagValueNode,
191-
Property $property
192-
): void {
193-
194-
$phpDocInfo->removeByType(VarTagValueNode::class);
195-
$phpDocInfo->addTagValueNode($varTagValueNode);
196-
197-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
198-
}
199197
}

0 commit comments

Comments
 (0)