Skip to content

Commit a559b9e

Browse files
authored
[TypeDeclarationDocblocks] Allow override dummy array return on AddReturnDocblockDataProviderRector (#7378)
* [TypeDeclarationDocblocks] Allow override dummy array return on AddReturnDocblockDataProviderRector * Fix
1 parent 9093d16 commit a559b9e

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class OverrideDummyArrayReturn extends TestCase
9+
{
10+
#[DataProvider('provideData')]
11+
public function testSomething()
12+
{
13+
}
14+
15+
/**
16+
* @return array
17+
*/
18+
public static function provideData()
19+
{
20+
return [
21+
[125, 35],
22+
[1252]
23+
];
24+
}
25+
}
26+
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;
32+
33+
use PHPUnit\Framework\Attributes\DataProvider;
34+
use PHPUnit\Framework\TestCase;
35+
36+
final class OverrideDummyArrayReturn extends TestCase
37+
{
38+
#[DataProvider('provideData')]
39+
public function testSomething()
40+
{
41+
}
42+
43+
/**
44+
* @return int[][]
45+
*/
46+
public static function provideData()
47+
{
48+
return [
49+
[125, 35],
50+
[1252]
51+
];
52+
}
53+
}
54+
55+
?>

rules/TypeDeclarationDocblocks/NodeDocblockTypeDecorator.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ public function decorateGenericIterableParamType(
4646
return false;
4747
}
4848

49-
$normalizedType = $this->typeNormalizer->generalizeConstantTypes($type);
50-
$typeNode = $this->createTypeNode($normalizedType);
49+
$typeNode = $this->createTypeNode($type);
5150

5251
// no value iterable type
5352
if ($typeNode instanceof IdentifierTypeNode) {
5453
return false;
5554
}
5655

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

5958
return true;
6059
}
@@ -76,9 +75,7 @@ public function decorateGenericIterableReturnType(
7675
return false;
7776
}
7877

79-
$returnTagValueNode = new ReturnTagValueNode($typeNode, '');
80-
81-
$this->addTagValueNodeAndUpdatePhpDocInfo($classMethodPhpDocInfo, $returnTagValueNode, $classMethod);
78+
$this->phpDocTypeChanger->changeReturnTypeNode($classMethod, $classMethodPhpDocInfo, $typeNode);
8279

8380
return true;
8481
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddParamArrayDocblockFromAssignsParamToParamReferenceRector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public function refactor(Node $node): ?Node
102102
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);
103103

104104
// already defined, lets skip it
105-
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag(
106-
$paramTagValueNode
107-
)) {
105+
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($paramTagValueNode)) {
108106
continue;
109107
}
110108

rules/TypeDeclarationDocblocks/Rector/Class_/AddReturnDocblockDataProviderRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Stmt\Class_;
1010
use PhpParser\Node\Stmt\Return_;
11-
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
1211
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1312
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1413
use Rector\Rector\AbstractRector;
@@ -17,6 +16,7 @@
1716
use Rector\TypeDeclarationDocblocks\NodeFinder\DataProviderMethodsFinder;
1817
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
1918
use Rector\TypeDeclarationDocblocks\NodeFinder\YieldNodeFinder;
19+
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
2020
use Rector\TypeDeclarationDocblocks\TypeResolver\YieldTypeResolver;
2121
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2222
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -33,6 +33,7 @@ public function __construct(
3333
private readonly ReturnNodeFinder $returnNodeFinder,
3434
private readonly YieldTypeResolver $yieldTypeResolver,
3535
private readonly YieldNodeFinder $yieldNodeFinder,
36+
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
3637
private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator,
3738
) {
3839
}
@@ -118,7 +119,7 @@ public function refactor(Node $node): ?Node
118119
$returnTagValueNode = $classMethodPhpDocInfo->getReturnTagValue();
119120

120121
// already set
121-
if ($returnTagValueNode instanceof ReturnTagValueNode) {
122+
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($returnTagValueNode)) {
122123
continue;
123124
}
124125

src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\FunctionLike;
88
use PhpParser\Node\Param;
99
use PhpParser\Node\Stmt;
10+
use PhpParser\Node\Stmt\ClassMethod;
1011
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
1112
use PHPStan\PhpDocParser\Ast\Node;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
@@ -113,6 +114,24 @@ public function changeReturnTypeNode(
113114
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
114115
}
115116

117+
public function changeParamTypeNode(
118+
ClassMethod $functionLike,
119+
PhpDocInfo $phpDocInfo,
120+
Param $param,
121+
string $paramName,
122+
TypeNode $newTypeNode
123+
): void {
124+
$existingParamTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);
125+
if ($existingParamTagValueNode instanceof ParamTagValueNode) {
126+
$existingParamTagValueNode->type = $newTypeNode;
127+
} else {
128+
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($newTypeNode, $param);
129+
$phpDocInfo->addTagValueNode($paramTagValueNode);
130+
}
131+
132+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
133+
}
134+
116135
public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType): bool
117136
{
118137
// better not touch this, can crash

0 commit comments

Comments
 (0)