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

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromAssignsParamToParamReferenceRector\Fixture;

final class OverrideDummyArrayParam
{
/**
* @param array $items
*/
public function run(array &$items)
{
$items[] = 'John';
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromAssignsParamToParamReferenceRector\Fixture;

final class OverrideDummyArrayParam
{
/**
* @param string[] $items
*/
public function run(array &$items)
{
$items[] = 'John';
}
}

?>
8 changes: 5 additions & 3 deletions rules/TypeDeclarationDocblocks/NodeDocblockTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclarationDocblocks;

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand All @@ -18,6 +19,7 @@
use PHPStan\Type\NeverType;
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 @@ -28,13 +30,15 @@ public function __construct(
private TypeNormalizer $typeNormalizer,
private StaticTypeMapper $staticTypeMapper,
private DocBlockUpdater $docBlockUpdater,
private PhpDocTypeChanger $phpDocTypeChanger
) {
}

public function decorateGenericIterableParamType(
Type $type,
PhpDocInfo $phpDocInfo,
ClassMethod $classMethod,
Param $param,
string $parameterName
): bool {
if ($this->isBareMixedType($type)) {
Expand All @@ -50,9 +54,7 @@ public function decorateGenericIterableParamType(
return false;
}

$paramTagValueNode = new ParamTagValueNode($typeNode, false, '$' . $parameterName, '', false);

$this->addTagValueNodeAndUpdatePhpDocInfo($phpDocInfo, $paramTagValueNode, $classMethod);
$this->phpDocTypeChanger->changeParamType($classMethod, $phpDocInfo, $normalizedType, $param, $parameterName);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\Rector\AbstractRector;
use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator;
use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayDimFetchFinder;
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -25,6 +26,7 @@ final class AddParamArrayDocblockFromAssignsParamToParamReferenceRector extends
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ArrayDimFetchFinder $arrayDimFetchFinder,
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator,
) {
}
Expand Down Expand Up @@ -101,7 +103,9 @@ public function refactor(Node $node): ?Node
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);

// already defined, lets skip it
if ($paramTagValueNode instanceof ParamTagValueNode) {
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag(
$paramTagValueNode
)) {
continue;
}

Expand All @@ -114,11 +118,11 @@ public function refactor(Node $node): ?Node

$assignedExprType = $this->getType($exprs[0]);
$iterableType = new ArrayType(new MixedType(), $assignedExprType);

$hasParamTypeChanged = $this->nodeDocblockTypeDecorator->decorateGenericIterableParamType(
$iterableType,
$phpDocInfo,
$node,
$param,
$paramName
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function refactor(Node $node): ?Node
$resolvedParameterType,
$classMethodPhpDocInfo,
$classMethod,
$param,
$parameterName
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

namespace Rector\TypeDeclarationDocblocks\TagNodeAnalyzer;

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

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

$type = $returnTagValueNode->type;
$type = $tagValueNode->type;
if (! $type instanceof IdentifierTypeNode) {
return true;
}
Expand Down