Skip to content
Open
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,12 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Source\BaseWithPropertyDocblock;

final class SkipAlreadyDefinedInParent extends BaseWithPropertyDocblock
{
public array $myArray = ['d', 'e', 'f'];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Source;

abstract class BaseWithPropertyDocblock
{
/**
* @var list<literal-string>
*/
public array $myArray = ['a', 'b', 'c'];
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace Rector\TypeDeclarationDocblocks\Rector\Class_;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Identifier;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator;
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
Expand Down Expand Up @@ -90,6 +98,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->hasUsefulParentPropertyVarTag($node, $property, $propertyDefaultType)) {
continue;
}

if ($this->nodeDocblockTypeDecorator->decorateGenericIterableVarType(
$propertyDefaultType,
$propertyPhpDocInfo,
Expand All @@ -105,4 +117,64 @@ public function refactor(Node $node): ?Node

return $node;
}

private function hasUsefulParentPropertyVarTag(Class_ $class, Property $property, Type $propertyDefaultType): bool
{
$propertyName = $this->getName($property);
if ($propertyName === null) {
return false;
}

// private property doesn't override parent property
if ($property->isPrivate()) {
return false;
}

$scope = ScopeFetcher::fetch($class);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
Comment thread
TomasVotruba marked this conversation as resolved.
return false;
}

foreach ($classReflection->getParents() as $parentClassReflection) {
if (! $parentClassReflection->hasNativeProperty($propertyName)) {
continue;
}

$parentPropertyReflection = $parentClassReflection->getNativeProperty($propertyName);
if ($parentPropertyReflection->isPrivate()) {
return false;
}

if (! $parentPropertyReflection->hasPhpDocType()) {
continue;
}

$varTagValueNode = $this->resolveVarTagValueNode($parentPropertyReflection);
if (! $this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($varTagValueNode)) {
continue;
}

if ($parentPropertyReflection->getPhpDocType()->isSuperTypeOf($propertyDefaultType)->yes()) {
return true;
}
}

return false;
}

private function resolveVarTagValueNode(PhpPropertyReflection $phpPropertyReflection): ?VarTagValueNode
{
$docComment = $phpPropertyReflection->getDocComment();
if ($docComment === null) {
return null;
}

$property = new Property(0, [new PropertyItem($phpPropertyReflection->getName())]);
$property->setDocComment(new Doc($docComment));

return $this->phpDocInfoFactory->createFromNodeOrEmpty($property)
->getVarTagValueNode();
}
}
Loading