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

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

final class OverrideDummyArrayVar
{
/**
* @var array
*/
private array $names;

/**
* @return string[]
*/
public function getNames(): array
{
return $this->names;
}
}

?>
-----
<?php

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

final class OverrideDummyArrayVar
{
/**
* @var string[]
*/
private array $names;

/**
* @return string[]
*/
public function getNames(): array
{
return $this->names;
}
}

?>
19 changes: 1 addition & 18 deletions rules/TypeDeclarationDocblocks/NodeDocblockTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
Expand All @@ -20,7 +17,6 @@
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Privatization\TypeManipulator\TypeNormalizer;
use Rector\StaticTypeMapper\StaticTypeMapper;

Expand All @@ -29,7 +25,6 @@
public function __construct(
private TypeNormalizer $typeNormalizer,
private StaticTypeMapper $staticTypeMapper,
private DocBlockUpdater $docBlockUpdater,
private PhpDocTypeChanger $phpDocTypeChanger
) {
}
Expand Down Expand Up @@ -94,9 +89,7 @@ public function decorateGenericIterableVarType(Type $type, PhpDocInfo $phpDocInf
return false;
}

$varTagValueNode = new VarTagValueNode($typeNode, '', '');

$this->addTagValueNodeAndUpdatePhpDocInfo($phpDocInfo, $varTagValueNode, $property);
$this->phpDocTypeChanger->changeVarTypeNode($property, $phpDocInfo, $typeNode);

return true;
}
Expand All @@ -115,16 +108,6 @@ private function createTypeNode(Type $type): TypeNode
return $typeNode;
}

private function addTagValueNodeAndUpdatePhpDocInfo(
PhpDocInfo $phpDocInfo,
ParamTagValueNode|VarTagValueNode|ReturnTagValueNode $tagValueNode,
Property|ClassMethod $stmt
): void {
$phpDocInfo->addTagValueNode($tagValueNode);

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
}

private function isBareMixedType(Type $type): bool
{
if ($type instanceof MixedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator;
use Rector\TypeDeclarationDocblocks\NodeFinder\PropertyGetterFinder;
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -25,7 +25,8 @@ final class DocblockVarArrayFromGetterReturnRector extends AbstractRector
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PropertyGetterFinder $propertyGetterFinder,
private readonly DocBlockUpdater $docBlockUpdater
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator
) {
}

Expand Down Expand Up @@ -98,7 +99,7 @@ public function refactor(Node $node): ?Node
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

// type is already known, skip it
if ($propertyPhpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($propertyPhpDocInfo->getVarTagValueNode())) {
continue;
}

Expand All @@ -113,11 +114,15 @@ public function refactor(Node $node): ?Node
continue;
}

$varTagValeNode = new VarTagValueNode($returnTagValueNode->type, '', '');
$isPropertyChanged = $this->nodeDocblockTypeDecorator->decorateGenericIterableVarType(
$classMethodDocInfo->getReturnType(),
$propertyPhpDocInfo,
$property
);

// find matching getter and its @return docblock
$propertyPhpDocInfo->addTagValueNode($varTagValeNode);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
if (! $isPropertyChanged) {
continue;
}

$hasChanged = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;

final class UsefulArrayTagNodeAnalyzer
{
public function isUsefulArrayTag(null|ReturnTagValueNode|ParamTagValueNode $tagValueNode): bool
public function isUsefulArrayTag(null|ReturnTagValueNode|ParamTagValueNode|VarTagValueNode $tagValueNode): bool
{
if (! $tagValueNode instanceof ReturnTagValueNode && ! $tagValueNode instanceof ParamTagValueNode) {
if (! $tagValueNode instanceof ReturnTagValueNode && ! $tagValueNode instanceof ParamTagValueNode && ! $tagValueNode instanceof VarTagValueNode) {
return false;
}

Expand Down
15 changes: 10 additions & 5 deletions src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function changeReturnTypeNode(
}

public function changeParamTypeNode(
ClassMethod $functionLike,
ClassMethod $classMethod,
PhpDocInfo $phpDocInfo,
Param $param,
string $paramName,
Expand All @@ -129,7 +129,7 @@ public function changeParamTypeNode(
$phpDocInfo->addTagValueNode($paramTagValueNode);
}

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
}

public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType): bool
Expand Down Expand Up @@ -244,9 +244,14 @@ public function isAllowed(TypeNode $typeNode): bool
*/
public function changeVarTypeNode(Stmt $stmt, PhpDocInfo $phpDocInfo, TypeNode $typeNode): void
{
// add completely new one
$varTagValueNode = new VarTagValueNode($typeNode, '', '');
$phpDocInfo->addTagValueNode($varTagValueNode);
$existingVarTagValueNode = $phpDocInfo->getVarTagValueNode();
if ($existingVarTagValueNode instanceof VarTagValueNode) {
$existingVarTagValueNode->type = $typeNode;
} else {
// add completely new one
$varTagValueNode = new VarTagValueNode($typeNode, '', '');
$phpDocInfo->addTagValueNode($varTagValueNode);
}

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
}
Expand Down