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

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

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class OverrideDummyArrayReturn extends TestCase
{
#[DataProvider('provideData')]
public function testSomething()
{
}

/**
* @return array
*/
public static function provideData()
{
return [
[125, 35],
[1252]
];
}
}

?>
-----
<?php

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

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class OverrideDummyArrayReturn extends TestCase
{
#[DataProvider('provideData')]
public function testSomething()
{
}

/**
* @return int[][]
*/
public static function provideData()
{
return [
[125, 35],
[1252]
];
}
}

?>
9 changes: 3 additions & 6 deletions rules/TypeDeclarationDocblocks/NodeDocblockTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ public function decorateGenericIterableParamType(
return false;
}

$normalizedType = $this->typeNormalizer->generalizeConstantTypes($type);
$typeNode = $this->createTypeNode($normalizedType);
$typeNode = $this->createTypeNode($type);

// no value iterable type
if ($typeNode instanceof IdentifierTypeNode) {
return false;
}

$this->phpDocTypeChanger->changeParamType($classMethod, $phpDocInfo, $normalizedType, $param, $parameterName);
$this->phpDocTypeChanger->changeParamTypeNode($classMethod, $phpDocInfo, $param, $parameterName, $typeNode);

return true;
}
Expand All @@ -76,9 +75,7 @@ public function decorateGenericIterableReturnType(
return false;
}

$returnTagValueNode = new ReturnTagValueNode($typeNode, '');

$this->addTagValueNodeAndUpdatePhpDocInfo($classMethodPhpDocInfo, $returnTagValueNode, $classMethod);
$this->phpDocTypeChanger->changeReturnTypeNode($classMethod, $classMethodPhpDocInfo, $typeNode);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public function refactor(Node $node): ?Node
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand All @@ -17,6 +16,7 @@
use Rector\TypeDeclarationDocblocks\NodeFinder\DataProviderMethodsFinder;
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
use Rector\TypeDeclarationDocblocks\NodeFinder\YieldNodeFinder;
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
use Rector\TypeDeclarationDocblocks\TypeResolver\YieldTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -33,6 +33,7 @@ public function __construct(
private readonly ReturnNodeFinder $returnNodeFinder,
private readonly YieldTypeResolver $yieldTypeResolver,
private readonly YieldNodeFinder $yieldNodeFinder,
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator,
) {
}
Expand Down Expand Up @@ -118,7 +119,7 @@ public function refactor(Node $node): ?Node
$returnTagValueNode = $classMethodPhpDocInfo->getReturnTagValue();

// already set
if ($returnTagValueNode instanceof ReturnTagValueNode) {
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($returnTagValueNode)) {
continue;
}

Expand Down
19 changes: 19 additions & 0 deletions src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand Down Expand Up @@ -113,6 +114,24 @@ public function changeReturnTypeNode(
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
}

public function changeParamTypeNode(
ClassMethod $functionLike,
PhpDocInfo $phpDocInfo,
Param $param,
string $paramName,
TypeNode $newTypeNode
): void {
$existingParamTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);
if ($existingParamTagValueNode instanceof ParamTagValueNode) {
$existingParamTagValueNode->type = $newTypeNode;
} else {
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($newTypeNode, $param);
$phpDocInfo->addTagValueNode($paramTagValueNode);
}

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

public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType): bool
{
// better not touch this, can crash
Expand Down