|
5 | 5 | namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Identifier; |
9 | | -use PhpParser\Node\Param; |
10 | 8 | use PhpParser\Node\Stmt\ClassMethod; |
11 | 9 | use PhpParser\Node\Stmt\Function_; |
12 | | -use PHPStan\Type\ArrayType; |
13 | | -use PHPStan\Type\MixedType; |
14 | | -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 10 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 11 | +use Rector\Exception\ShouldNotHappenException; |
15 | 12 | use Rector\Rector\AbstractRector; |
16 | | -use Rector\StaticTypeMapper\StaticTypeMapper; |
17 | | -use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator; |
18 | | -use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayMapClosureExprFinder; |
19 | | -use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; |
20 | 13 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
21 | 14 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
22 | 15 |
|
23 | 16 | /** |
24 | | - * @see \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\AddParamArrayDocblockBasedOnArrayMapRectorTest |
| 17 | + * @deprecated This rule is deprecated, as a single array_map() closure only proves what one call site reads. The param type is vague and unreliable. Add the @param docblock manually instead. |
25 | 18 | */ |
26 | | -final class AddParamArrayDocblockBasedOnArrayMapRector extends AbstractRector |
| 19 | +final class AddParamArrayDocblockBasedOnArrayMapRector extends AbstractRector implements DeprecatedInterface |
27 | 20 | { |
28 | | - public function __construct( |
29 | | - private readonly ArrayMapClosureExprFinder $arrayMapClosureExprFinder, |
30 | | - private readonly StaticTypeMapper $staticTypeMapper, |
31 | | - private readonly PhpDocInfoFactory $phpDocInfoFactory, |
32 | | - private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer, |
33 | | - private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator |
34 | | - ) { |
35 | | - |
36 | | - } |
37 | | - |
38 | 21 | public function getRuleDefinition(): RuleDefinition |
39 | 22 | { |
40 | 23 | return new RuleDefinition('Add @param array docblock if array_map is used on the parameter', [ |
@@ -78,73 +61,9 @@ public function getNodeTypes(): array |
78 | 61 | */ |
79 | 62 | public function refactor(Node $node): ?Node |
80 | 63 | { |
81 | | - if ($node->getParams() === []) { |
82 | | - return null; |
83 | | - } |
84 | | - |
85 | | - $hasChanged = false; |
86 | | - $functionPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
87 | | - |
88 | | - foreach ($node->params as $param) { |
89 | | - // handle only arrays |
90 | | - if (! $this->isArrayParam($param)) { |
91 | | - continue; |
92 | | - } |
93 | | - |
94 | | - $paramName = $this->getName($param); |
95 | | - |
96 | | - $arrayMapClosures = $this->arrayMapClosureExprFinder->findByVariableName($node, $paramName); |
97 | | - if ($arrayMapClosures === []) { |
98 | | - continue; |
99 | | - } |
100 | | - |
101 | | - foreach ($arrayMapClosures as $arrayMapClosure) { |
102 | | - $params = $arrayMapClosure->getParams(); |
103 | | - if ($params === []) { |
104 | | - continue; |
105 | | - } |
106 | | - |
107 | | - $firstParam = $params[0]; |
108 | | - $paramTypeNode = $firstParam->type; |
109 | | - if ($paramTypeNode === null) { |
110 | | - continue; |
111 | | - } |
112 | | - |
113 | | - if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag( |
114 | | - $functionPhpDocInfo->getParamTagValueByName($paramName) |
115 | | - )) { |
116 | | - continue; |
117 | | - } |
118 | | - |
119 | | - $paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($paramTypeNode); |
120 | | - $arrayParamType = new ArrayType(new MixedType(), $paramType); |
121 | | - |
122 | | - if ($this->nodeDocblockTypeDecorator->decorateGenericIterableParamType( |
123 | | - $arrayParamType, |
124 | | - $functionPhpDocInfo, |
125 | | - $node, |
126 | | - $param, |
127 | | - $paramName |
128 | | - )) { |
129 | | - $hasChanged = true; |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - } |
134 | | - |
135 | | - if (! $hasChanged) { |
136 | | - return null; |
137 | | - } |
138 | | - |
139 | | - return $node; |
140 | | - } |
141 | | - |
142 | | - private function isArrayParam(Param $param): bool |
143 | | - { |
144 | | - if (! $param->type instanceof Identifier) { |
145 | | - return false; |
146 | | - } |
147 | | - |
148 | | - return $this->isName($param->type, 'array'); |
| 64 | + throw new ShouldNotHappenException(sprintf( |
| 65 | + '"%s" rule is deprecated, as the param type guessed from a single array_map() closure is vague and unreliable. Add the @param docblock manually instead', |
| 66 | + self::class |
| 67 | + )); |
149 | 68 | } |
150 | 69 | } |
0 commit comments