Skip to content

Commit 93eb3cd

Browse files
committed
[tdd] Add AddParamArrayDocblockBasedOnArrayMapRector
1 parent 9c1b5fe commit 93eb3cd

File tree

10 files changed

+364
-2
lines changed

10 files changed

+364
-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: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Param;
10+
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\Function_;
12+
use PHPStan\Type\ArrayType;
13+
use PHPStan\Type\MixedType;
14+
use PHPStan\Type\Type;
15+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
16+
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
17+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
18+
use Rector\Rector\AbstractRector;
19+
use Rector\StaticTypeMapper\StaticTypeMapper;
20+
use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayMapClosureExprFinder;
21+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
22+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
23+
24+
/**
25+
* @see \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector\AddParamArrayDocblockBasedOnArrayMapRectorTest
26+
*/
27+
final class AddParamArrayDocblockBasedOnArrayMapRector extends AbstractRector
28+
{
29+
public function __construct(
30+
private readonly ArrayMapClosureExprFinder $arrayMapClosureExprFinder,
31+
private readonly StaticTypeMapper $staticTypeMapper,
32+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
33+
private readonly DocBlockUpdater $docBlockUpdater,
34+
private readonly PhpDocTypeChanger $phpDocTypeChanger,
35+
) {
36+
37+
}
38+
39+
public function getRuleDefinition(): RuleDefinition
40+
{
41+
return new RuleDefinition('Add @param array docblock if array_map is used on the parameter', [
42+
new CodeSample(
43+
<<<'CODE_SAMPLE'
44+
final class SomeClass
45+
{
46+
public function run(array $names): void
47+
{
48+
$names = array_map(fn(string $name) => trim($name), $names);
49+
}
50+
}
51+
CODE_SAMPLE
52+
,
53+
<<<'CODE_SAMPLE'
54+
final class SomeClass
55+
{
56+
/**
57+
* @param string[] $names
58+
*/
59+
public function run(array $names): void
60+
{
61+
$names = array_map(fn(string $name) => trim($name), $names);
62+
}
63+
}
64+
CODE_SAMPLE
65+
),
66+
]);
67+
}
68+
69+
/**
70+
* @return array<class-string<Node>>
71+
*/
72+
public function getNodeTypes(): array
73+
{
74+
return [ClassMethod::class, Function_::class];
75+
}
76+
77+
/**
78+
* @param ClassMethod|Function_ $node
79+
*/
80+
public function refactor(Node $node): ?Node
81+
{
82+
if ($node->getParams() === []) {
83+
return null;
84+
}
85+
86+
$hasChanged = false;
87+
$functionPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
88+
89+
foreach ($node->params as $param) {
90+
// handle only arrays
91+
if (! $this->isArrayParam($param)) {
92+
continue;
93+
}
94+
95+
$paramName = $this->getName($param);
96+
97+
$arrayMapClosures = $this->arrayMapClosureExprFinder->findByVariableName($node, $paramName);
98+
if ($arrayMapClosures === []) {
99+
continue;
100+
}
101+
102+
foreach ($arrayMapClosures as $arrayMapClosure) {
103+
$params = $arrayMapClosure->getParams();
104+
if ($params === []) {
105+
continue;
106+
}
107+
108+
$firstParam = $params[0];
109+
$paramTypeNode = $firstParam->type;
110+
if ($paramTypeNode === null) {
111+
continue;
112+
}
113+
114+
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($paramTypeNode);
115+
$arrayParamType = new ArrayType(new MixedType(), $paramType);
116+
117+
if ($this->isAlreadyNonMixedParamType($functionPhpDocInfo, $paramName)) {
118+
continue;
119+
}
120+
121+
$this->phpDocTypeChanger->changeParamType(
122+
$node,
123+
$functionPhpDocInfo,
124+
$arrayParamType,
125+
$param,
126+
$paramName
127+
);
128+
$hasChanged = true;
129+
}
130+
131+
}
132+
133+
if (! $hasChanged) {
134+
return null;
135+
}
136+
137+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
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');
149+
}
150+
151+
private function isMixedArrayType(Type $type): bool
152+
{
153+
if (! $type instanceof ArrayType) {
154+
return false;
155+
}
156+
157+
if (! $type->getItemType() instanceof MixedType) {
158+
return false;
159+
}
160+
161+
return $type->getKeyType() instanceof MixedType;
162+
}
163+
164+
private function isAlreadyNonMixedParamType(
165+
\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $functionPhpDocInfo,
166+
string $paramName
167+
): bool {
168+
$currentParamType = $functionPhpDocInfo->getParamType($paramName);
169+
if ($currentParamType instanceof MixedType) {
170+
return false;
171+
}
172+
173+
// has useful param type already?
174+
return ! $this->isMixedArrayType($currentParamType);
175+
}
176+
}

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)