Skip to content

Commit 366f329

Browse files
authored
[tdd] Add AddParamArrayDocblockBasedOnArrayMapRector (#7435)
1 parent 9c1b5fe commit 366f329

File tree

10 files changed

+365
-2
lines changed

10 files changed

+365
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddParamArrayDocblockBasedOnArrayMapRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
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 OverrideMixedType
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 OverrideMixedType
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,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
4+
5+
final class SkipBetterExistingType
6+
{
7+
/**
8+
* @param array<string|\Stringable> $items
9+
*/
10+
public function run(array $items): void
11+
{
12+
array_map(fn (string $item) => trim($item), $items);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
4+
5+
final class SomeClass
6+
{
7+
public function run(array $items): void
8+
{
9+
array_map(fn (string $item) => trim($item), $items);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\Fixture;
18+
19+
final class SomeClass
20+
{
21+
/**
22+
* @param string[] $items
23+
*/
24+
public function run(array $items): void
25+
{
26+
array_map(fn (string $item) => trim($item), $items);
27+
}
28+
}
29+
30+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([AddParamArrayDocblockBasedOnArrayMapRector::class]);

rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
5050
* on php 7.x, substr() result can return false, so force (string) is needed
5151
* @see https://github.com/rectorphp/rector-src/pull/7436
5252
*/
53-
$subClassName = (string) substr($fullyQualifiedObjectType->getClassName(), 0, -strlen($fullyQualifiedObjectType->getShortName()) - 1);
53+
$subClassName = (string) substr(
54+
$fullyQualifiedObjectType->getClassName(),
55+
0,
56+
-strlen($fullyQualifiedObjectType->getShortName()) - 1
57+
);
5458
$fullyQualifiedObjectTypeNamespace = strtolower($subClassName);
5559

5660
foreach ($classLikeNames as $classLikeName) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclarationDocblocks\NodeFinder;
6+
7+
use PhpParser\Node\Expr\ArrowFunction;
8+
use PhpParser\Node\Expr\Closure;
9+
use PhpParser\Node\Expr\FuncCall;
10+
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\Function_;
13+
use Rector\NodeNameResolver\NodeNameResolver;
14+
use Rector\PhpParser\Node\BetterNodeFinder;
15+
16+
final readonly class ArrayMapClosureExprFinder
17+
{
18+
public function __construct(
19+
private BetterNodeFinder $betterNodeFinder,
20+
private NodeNameResolver $nodeNameResolver,
21+
) {
22+
}
23+
24+
/**
25+
* @return array<Closure|ArrowFunction>
26+
*/
27+
public function findByVariableName(ClassMethod|Function_ $functionLike, string $variableName): array
28+
{
29+
if ($functionLike->stmts === null) {
30+
return [];
31+
}
32+
33+
/** @var FuncCall[] $funcCalls */
34+
$funcCalls = $this->betterNodeFinder->findInstancesOfScoped($functionLike->stmts, FuncCall::class);
35+
36+
$arrayMapClosures = [];
37+
38+
foreach ($funcCalls as $funcCall) {
39+
if ($funcCall->isFirstClassCallable()) {
40+
continue;
41+
}
42+
43+
if (! $this->nodeNameResolver->isName($funcCall, 'array_map')) {
44+
continue;
45+
}
46+
47+
$secondArg = $funcCall->getArgs()[1];
48+
if (! $secondArg->value instanceof Variable) {
49+
continue;
50+
}
51+
52+
if (! $this->nodeNameResolver->isName($secondArg->value, $variableName)) {
53+
continue;
54+
}
55+
56+
$firstArg = $funcCall->getArgs()[0];
57+
if (! $firstArg->value instanceof Closure && ! $firstArg->value instanceof ArrowFunction) {
58+
continue;
59+
}
60+
61+
$arrayMapClosures[] = $firstArg->value;
62+
}
63+
64+
return $arrayMapClosures;
65+
}
66+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
6+
7+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Identifier;
10+
use PhpParser\Node\Param;
11+
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\Function_;
13+
use PHPStan\Type\ArrayType;
14+
use PHPStan\Type\MixedType;
15+
use PHPStan\Type\Type;
16+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
17+
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
18+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
19+
use Rector\Rector\AbstractRector;
20+
use Rector\StaticTypeMapper\StaticTypeMapper;
21+
use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayMapClosureExprFinder;
22+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
23+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
24+
25+
/**
26+
* @see \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\AddParamArrayDocblockBasedOnArrayMapRectorTest
27+
*/
28+
final class AddParamArrayDocblockBasedOnArrayMapRector extends AbstractRector
29+
{
30+
public function __construct(
31+
private readonly ArrayMapClosureExprFinder $arrayMapClosureExprFinder,
32+
private readonly StaticTypeMapper $staticTypeMapper,
33+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
34+
private readonly DocBlockUpdater $docBlockUpdater,
35+
private readonly PhpDocTypeChanger $phpDocTypeChanger,
36+
) {
37+
38+
}
39+
40+
public function getRuleDefinition(): RuleDefinition
41+
{
42+
return new RuleDefinition('Add @param array docblock if array_map is used on the parameter', [
43+
new CodeSample(
44+
<<<'CODE_SAMPLE'
45+
final class SomeClass
46+
{
47+
public function run(array $names): void
48+
{
49+
$names = array_map(fn(string $name) => trim($name), $names);
50+
}
51+
}
52+
CODE_SAMPLE
53+
,
54+
<<<'CODE_SAMPLE'
55+
final class SomeClass
56+
{
57+
/**
58+
* @param string[] $names
59+
*/
60+
public function run(array $names): void
61+
{
62+
$names = array_map(fn(string $name) => trim($name), $names);
63+
}
64+
}
65+
CODE_SAMPLE
66+
),
67+
]);
68+
}
69+
70+
/**
71+
* @return array<class-string<Node>>
72+
*/
73+
public function getNodeTypes(): array
74+
{
75+
return [ClassMethod::class, Function_::class];
76+
}
77+
78+
/**
79+
* @param ClassMethod|Function_ $node
80+
*/
81+
public function refactor(Node $node): ?Node
82+
{
83+
if ($node->getParams() === []) {
84+
return null;
85+
}
86+
87+
$hasChanged = false;
88+
$functionPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
89+
90+
foreach ($node->params as $param) {
91+
// handle only arrays
92+
if (! $this->isArrayParam($param)) {
93+
continue;
94+
}
95+
96+
$paramName = $this->getName($param);
97+
98+
$arrayMapClosures = $this->arrayMapClosureExprFinder->findByVariableName($node, $paramName);
99+
if ($arrayMapClosures === []) {
100+
continue;
101+
}
102+
103+
foreach ($arrayMapClosures as $arrayMapClosure) {
104+
$params = $arrayMapClosure->getParams();
105+
if ($params === []) {
106+
continue;
107+
}
108+
109+
$firstParam = $params[0];
110+
$paramTypeNode = $firstParam->type;
111+
if ($paramTypeNode === null) {
112+
continue;
113+
}
114+
115+
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($paramTypeNode);
116+
$arrayParamType = new ArrayType(new MixedType(), $paramType);
117+
118+
if ($this->isAlreadyNonMixedParamType($functionPhpDocInfo, $paramName)) {
119+
continue;
120+
}
121+
122+
$this->phpDocTypeChanger->changeParamType(
123+
$node,
124+
$functionPhpDocInfo,
125+
$arrayParamType,
126+
$param,
127+
$paramName
128+
);
129+
$hasChanged = true;
130+
}
131+
132+
}
133+
134+
if (! $hasChanged) {
135+
return null;
136+
}
137+
138+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
139+
140+
return $node;
141+
}
142+
143+
private function isArrayParam(Param $param): bool
144+
{
145+
if (! $param->type instanceof Identifier) {
146+
return false;
147+
}
148+
149+
return $this->isName($param->type, 'array');
150+
}
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+
}
177+
}

src/Config/Level/DeadCodeLevel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
2727
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector;
2828
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
29-
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
3029
use Rector\DeadCode\Rector\Closure\RemoveUnusedClosureVariableUseRector;
30+
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
3131
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
3232
use Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector;
3333
use Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector;

0 commit comments

Comments
 (0)