Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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';
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Privatization\TypeManipulator\TypeNormalizer;
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,7 +27,9 @@ final class AddParamArrayDocblockFromAssignsParamToParamReferenceRector extends
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ArrayDimFetchFinder $arrayDimFetchFinder,
private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator,
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
private readonly TypeNormalizer $typeNormalizer,
private readonly PhpDocTypeChanger $phpDocTypeChanger
) {
}

Expand Down Expand Up @@ -101,7 +105,9 @@ public function refactor(Node $node): ?Node
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);

// already defined, lets skip it
if ($paramTagValueNode instanceof ParamTagValueNode) {
if ($paramTagValueNode instanceof ParamTagValueNode && $this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag(
$paramTagValueNode
)) {
Comment thread
samsonasik marked this conversation as resolved.
Outdated
continue;
}

Expand All @@ -113,18 +119,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,
$paramName
$iterableType = $this->typeNormalizer->generalizeConstantTypes(
new ArrayType(new MixedType(), $assignedExprType)
);

if (! $hasParamTypeChanged) {
continue;
}
$this->phpDocTypeChanger->changeParamType($node, $phpDocInfo, $iterableType, $param, $paramName);

$hasChanged = true;
}
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