Skip to content

Commit dfa7069

Browse files
authored
[TypeDeclarationDocblocks] Skip docblock already defined in parent on DocblockVarArrayFromPropertyDefaultsRector (#8085)
* [TypeDeclarationDocblocks] Skip child with parent property docblock on DocblockVarArrayFromPropertyDefaultsRector * [TypeDeclarationDocblocks] Skip child with parent property docblock on DocblockVarArrayFromPropertyDefaultsRector * fix * use of scopefetcher
1 parent b163af7 commit dfa7069

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Source\BaseWithPropertyDocblock;
6+
7+
final class SkipAlreadyDefinedInParent extends BaseWithPropertyDocblock
8+
{
9+
public array $myArray = ['d', 'e', 'f'];
10+
}
11+
12+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Source;
4+
5+
abstract class BaseWithPropertyDocblock
6+
{
7+
/**
8+
* @var list<literal-string>
9+
*/
10+
public array $myArray = ['a', 'b', 'c'];
11+
}

‎rules/TypeDeclarationDocblocks/Rector/Class_/DocblockVarArrayFromPropertyDefaultsRector.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44

55
namespace Rector\TypeDeclarationDocblocks\Rector\Class_;
66

7+
use PhpParser\Comment\Doc;
78
use PhpParser\Node;
89
use PhpParser\Node\Expr\Array_;
910
use PhpParser\Node\Identifier;
11+
use PhpParser\Node\PropertyItem;
1012
use PhpParser\Node\Stmt\Class_;
13+
use PhpParser\Node\Stmt\Property;
14+
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
15+
use PHPStan\Reflection\ClassReflection;
16+
use PHPStan\Reflection\Php\PhpPropertyReflection;
17+
use PHPStan\Type\Type;
1118
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
19+
use Rector\PHPStan\ScopeFetcher;
1220
use Rector\Rector\AbstractRector;
1321
use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator;
1422
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
@@ -90,6 +98,10 @@ public function refactor(Node $node): ?Node
9098
continue;
9199
}
92100

101+
if ($this->hasUsefulParentPropertyVarTag($node, $property, $propertyDefaultType)) {
102+
continue;
103+
}
104+
93105
if ($this->nodeDocblockTypeDecorator->decorateGenericIterableVarType(
94106
$propertyDefaultType,
95107
$propertyPhpDocInfo,
@@ -105,4 +117,64 @@ public function refactor(Node $node): ?Node
105117

106118
return $node;
107119
}
120+
121+
private function hasUsefulParentPropertyVarTag(Class_ $class, Property $property, Type $propertyDefaultType): bool
122+
{
123+
$propertyName = $this->getName($property);
124+
if ($propertyName === null) {
125+
return false;
126+
}
127+
128+
// private property doesn't override parent property
129+
if ($property->isPrivate()) {
130+
return false;
131+
}
132+
133+
$scope = ScopeFetcher::fetch($class);
134+
135+
$classReflection = $scope->getClassReflection();
136+
if (! $classReflection instanceof ClassReflection) {
137+
return false;
138+
}
139+
140+
foreach ($classReflection->getParents() as $parentClassReflection) {
141+
if (! $parentClassReflection->hasNativeProperty($propertyName)) {
142+
continue;
143+
}
144+
145+
$parentPropertyReflection = $parentClassReflection->getNativeProperty($propertyName);
146+
if ($parentPropertyReflection->isPrivate()) {
147+
return false;
148+
}
149+
150+
if (! $parentPropertyReflection->hasPhpDocType()) {
151+
continue;
152+
}
153+
154+
$varTagValueNode = $this->resolveVarTagValueNode($parentPropertyReflection);
155+
if (! $this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($varTagValueNode)) {
156+
continue;
157+
}
158+
159+
if ($parentPropertyReflection->getPhpDocType()->isSuperTypeOf($propertyDefaultType)->yes()) {
160+
return true;
161+
}
162+
}
163+
164+
return false;
165+
}
166+
167+
private function resolveVarTagValueNode(PhpPropertyReflection $phpPropertyReflection): ?VarTagValueNode
168+
{
169+
$docComment = $phpPropertyReflection->getDocComment();
170+
if ($docComment === null) {
171+
return null;
172+
}
173+
174+
$property = new Property(0, [new PropertyItem($phpPropertyReflection->getName())]);
175+
$property->setDocComment(new Doc($docComment));
176+
177+
return $this->phpDocInfoFactory->createFromNodeOrEmpty($property)
178+
->getVarTagValueNode();
179+
}
108180
}

0 commit comments

Comments
 (0)