Skip to content

Commit 8971177

Browse files
authored
[TypeDeclaration] Keep Generic type docblock on TypedPropertyFromDocblockSetUpDefinedRector (#7140)
1 parent e298639 commit 8971177

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector\Source\SomeDocblockType;
7+
8+
final class GenericTypeDoc extends TestCase
9+
{
10+
/**
11+
* @var \Iterator<SomeType>
12+
*/
13+
private $someType;
14+
15+
protected function setUp(): void
16+
{
17+
$this->someType = $this->create('string');
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector\Source\SomeDocblockType;
29+
30+
final class GenericTypeDoc extends TestCase
31+
{
32+
/**
33+
* @var \Iterator<SomeType>
34+
*/
35+
private \Iterator $someType;
36+
37+
protected function setUp(): void
38+
{
39+
$this->someType = $this->create('string');
40+
}
41+
}
42+
43+
?>

rules/TypeDeclaration/Rector/Class_/TypedPropertyFromDocblockSetUpDefinedRector.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1414
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1515
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
16+
use Rector\DeadCode\PhpDoc\DeadVarTagValueNodeAnalyzer;
1617
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1718
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1819
use Rector\Rector\AbstractRector;
@@ -34,7 +35,8 @@ public function __construct(
3435
private readonly ConstructorAssignDetector $constructorAssignDetector,
3536
private readonly PhpDocInfoFactory $phpDocInfoFactory,
3637
private readonly StaticTypeMapper $staticTypeMapper,
37-
private readonly DocBlockUpdater $docBlockUpdater
38+
private readonly DocBlockUpdater $docBlockUpdater,
39+
private readonly DeadVarTagValueNodeAnalyzer $deadVarTagValueNodeAnalyzer
3840
) {
3941
}
4042

@@ -145,10 +147,11 @@ public function refactor(Node $node): ?Node
145147
continue;
146148
}
147149

150+
$property->type = $nativePropertyTypeNode;
151+
148152
// remove var tag
149153
$this->removeVarTag($propertyPhpDocInfo, $property);
150154

151-
$property->type = $nativePropertyTypeNode;
152155
$hasChanged = true;
153156
}
154157

@@ -166,6 +169,16 @@ public function provideMinPhpVersion(): int
166169

167170
private function removeVarTag(PhpDocInfo $propertyPhpDocInfo, Property $property): void
168171
{
172+
$varTagValueNode = $propertyPhpDocInfo->getVarTagValueNode();
173+
174+
if (! $varTagValueNode instanceof VarTagValueNode) {
175+
return;
176+
}
177+
178+
if (! $this->deadVarTagValueNodeAnalyzer->isDead($varTagValueNode, $property)) {
179+
return;
180+
}
181+
169182
$propertyPhpDocInfo->removeByType(VarTagValueNode::class);
170183
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
171184
}

0 commit comments

Comments
 (0)