Skip to content

Commit 89bfc23

Browse files
authored
[TypeDeclarationDocblocks] Use existing UsefulArrayTagNodeAnalyzer and NodeDocblockTypeDecorator services that specifically handle/validate bare mixed already on AddParamArrayDocblockBasedOnArrayMapRector (#7439)
* [TypeDeclarationDocblocks] Use existing UsefulArrayTagNodeAnalyzer and NodeDocblockTypeDecorator services that specifically handle/validate bare mixed already on AddParamArrayDocblockBasedOnArrayMapRector * [TypeDeclarationDocblocks] Use existing UsefulArrayTagNodeAnalyzer and NodeDocblockTypeDecorator services that specifically handle/validate bare mixed already on AddParamArrayDocblockBasedOnArrayMapRector * [TypeDeclarationDocblocks] Use existing UsefulArrayTagNodeAnalyzer and NodeDocblockTypeDecorator services that specifically handle/validate bare mixed already on AddParamArrayDocblockBasedOnArrayMapRector * [TypeDeclarationDocblocks] Use existing UsefulArrayTagNodeAnalyzer and NodeDocblockTypeDecorator services that specifically handle/validate bare mixed already on AddParamArrayDocblockBasedOnArrayMapRector * add more fixture * fix
1 parent 366f329 commit 89bfc23

File tree

5 files changed

+86
-48
lines changed

5 files changed

+86
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
4+
5+
final class OverrideBareMixed
6+
{
7+
/**
8+
* @param mixed $items
9+
*/
10+
public function run(array $items): void
11+
{
12+
array_map(fn (string $item) => trim($item), $items);
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
21+
22+
final class OverrideBareMixed
23+
{
24+
/**
25+
* @param string[] $items
26+
*/
27+
public function run(array $items): void
28+
{
29+
array_map(fn (string $item) => trim($item), $items);
30+
}
31+
}
32+
33+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
4+
5+
final class OverrideDummyArray
6+
{
7+
/**
8+
* @param array $items
9+
*/
10+
public function run(array $items): void
11+
{
12+
array_map(fn (string $item) => trim($item), $items);
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
21+
22+
final class OverrideDummyArray
23+
{
24+
/**
25+
* @param string[] $items
26+
*/
27+
public function run(array $items): void
28+
{
29+
array_map(fn (string $item) => trim($item), $items);
30+
}
31+
}
32+
33+
?>

rules/TypeDeclarationDocblocks/NodeDocblockTypeDecorator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node\FunctionLike;
88
use PhpParser\Node\Param;
9-
use PhpParser\Node\Stmt\ClassMethod;
109
use PhpParser\Node\Stmt\Property;
1110
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
1211
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
@@ -33,7 +32,7 @@ public function __construct(
3332
public function decorateGenericIterableParamType(
3433
Type $type,
3534
PhpDocInfo $phpDocInfo,
36-
ClassMethod $classMethod,
35+
FunctionLike $functionLike,
3736
Param $param,
3837
string $parameterName
3938
): bool {
@@ -49,7 +48,7 @@ public function decorateGenericIterableParamType(
4948
return false;
5049
}
5150

52-
$this->phpDocTypeChanger->changeParamTypeNode($classMethod, $phpDocInfo, $param, $parameterName, $typeNode);
51+
$this->phpDocTypeChanger->changeParamTypeNode($functionLike, $phpDocInfo, $param, $parameterName, $typeNode);
5352

5453
return true;
5554
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddParamArrayDocblockBasedOnArrayMapRector.php

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44

55
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
66

7-
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
87
use PhpParser\Node;
98
use PhpParser\Node\Identifier;
109
use PhpParser\Node\Param;
1110
use PhpParser\Node\Stmt\ClassMethod;
1211
use PhpParser\Node\Stmt\Function_;
1312
use PHPStan\Type\ArrayType;
1413
use PHPStan\Type\MixedType;
15-
use PHPStan\Type\Type;
1614
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
17-
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
18-
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1915
use Rector\Rector\AbstractRector;
2016
use Rector\StaticTypeMapper\StaticTypeMapper;
17+
use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator;
2118
use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayMapClosureExprFinder;
19+
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
2220
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2321
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2422

@@ -31,8 +29,8 @@ public function __construct(
3129
private readonly ArrayMapClosureExprFinder $arrayMapClosureExprFinder,
3230
private readonly StaticTypeMapper $staticTypeMapper,
3331
private readonly PhpDocInfoFactory $phpDocInfoFactory,
34-
private readonly DocBlockUpdater $docBlockUpdater,
35-
private readonly PhpDocTypeChanger $phpDocTypeChanger,
32+
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
33+
private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator
3634
) {
3735

3836
}
@@ -112,21 +110,24 @@ public function refactor(Node $node): ?Node
112110
continue;
113111
}
114112

115-
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($paramTypeNode);
116-
$arrayParamType = new ArrayType(new MixedType(), $paramType);
117-
118-
if ($this->isAlreadyNonMixedParamType($functionPhpDocInfo, $paramName)) {
113+
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag(
114+
$functionPhpDocInfo->getParamTagValueByName($paramName)
115+
)) {
119116
continue;
120117
}
121118

122-
$this->phpDocTypeChanger->changeParamType(
123-
$node,
124-
$functionPhpDocInfo,
119+
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($paramTypeNode);
120+
$arrayParamType = new ArrayType(new MixedType(), $paramType);
121+
122+
if ($this->nodeDocblockTypeDecorator->decorateGenericIterableParamType(
125123
$arrayParamType,
124+
$functionPhpDocInfo,
125+
$node,
126126
$param,
127127
$paramName
128-
);
129-
$hasChanged = true;
128+
)) {
129+
$hasChanged = true;
130+
}
130131
}
131132

132133
}
@@ -135,8 +136,6 @@ public function refactor(Node $node): ?Node
135136
return null;
136137
}
137138

138-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
139-
140139
return $node;
141140
}
142141

@@ -148,30 +147,4 @@ private function isArrayParam(Param $param): bool
148147

149148
return $this->isName($param->type, 'array');
150149
}
151-
152-
private function isMixedArrayType(Type $type): bool
153-
{
154-
if (! $type instanceof ArrayType) {
155-
return false;
156-
}
157-
158-
if (! $type->getItemType() instanceof MixedType) {
159-
return false;
160-
}
161-
162-
return $type->getKeyType() instanceof MixedType;
163-
}
164-
165-
private function isAlreadyNonMixedParamType(
166-
PhpDocInfo $functionPhpDocInfo,
167-
string $paramName
168-
): bool {
169-
$currentParamType = $functionPhpDocInfo->getParamType($paramName);
170-
if ($currentParamType instanceof MixedType) {
171-
return false;
172-
}
173-
174-
// has useful param type already?
175-
return ! $this->isMixedArrayType($currentParamType);
176-
}
177150
}

src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function changeReturnTypeNode(
115115
}
116116

117117
public function changeParamTypeNode(
118-
ClassMethod $classMethod,
118+
FunctionLike $functionLike,
119119
PhpDocInfo $phpDocInfo,
120120
Param $param,
121121
string $paramName,
@@ -129,7 +129,7 @@ public function changeParamTypeNode(
129129
$phpDocInfo->addTagValueNode($paramTagValueNode);
130130
}
131131

132-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
132+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
133133
}
134134

135135
public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType): bool

0 commit comments

Comments
 (0)