Skip to content

Commit 26d22aa

Browse files
committed
[type-declarations] Add AddClassMethodParamArrayWhereDimFetchRector
1 parent edadebb commit 26d22aa

File tree

6 files changed

+172
-1
lines changed

6 files changed

+172
-1
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\TypeDeclaration\Rector\ClassMethod\AddClassMethodParamArrayWhereDimFetchRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddClassMethodParamArrayWhereDimFetchRectorTest 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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddClassMethodParamArrayWhereDimFetchRector\Fixture;
4+
5+
final class Fixture
6+
{
7+
public function process($items)
8+
{
9+
return $items[0];
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddClassMethodParamArrayWhereDimFetchRector\Fixture;
18+
19+
final class Fixture
20+
{
21+
public function process(array $items)
22+
{
23+
return $items[0];
24+
}
25+
}
26+
27+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\AddClassMethodParamArrayWhereDimFetchRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AddClassMethodParamArrayWhereDimFetchRector::class);
10+
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclaration\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use Rector\Rector\AbstractRector;
11+
use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayDimFetchFinder;
12+
use Rector\ValueObject\PhpVersionFeature;
13+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddClassMethodParamArrayWhereDimFetchRector\AddClassMethodParamArrayWhereDimFetchRectorTest
19+
*/
20+
final class AddClassMethodParamArrayWhereDimFetchRector extends AbstractRector implements MinPhpVersionInterface
21+
{
22+
public function __construct(
23+
private readonly ArrayDimFetchFinder $arrayDimFetchFinder,
24+
) {
25+
}
26+
27+
public function getRuleDefinition(): RuleDefinition
28+
{
29+
return new RuleDefinition('Add method array type, if dim fetch is inside', [
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
final class SomeClass
33+
{
34+
public function run($data)
35+
{
36+
return $data['key'];
37+
}
38+
}
39+
CODE_SAMPLE
40+
41+
,
42+
<<<'CODE_SAMPLE'
43+
final class SomeClass
44+
{
45+
public function run(array $data)
46+
{
47+
return $data['key'];
48+
}
49+
}
50+
CODE_SAMPLE
51+
),
52+
]);
53+
}
54+
55+
/**
56+
* @return array<class-string<Node>>
57+
*/
58+
public function getNodeTypes(): array
59+
{
60+
return [ClassMethod::class];
61+
}
62+
63+
/**
64+
* @param ClassMethod $node
65+
*/
66+
public function refactor(Node $node): ?Node
67+
{
68+
$hasChanged = false;
69+
70+
foreach ($node->getParams() as $param) {
71+
if ($param->type instanceof Node) {
72+
continue;
73+
}
74+
75+
$paramName = $this->getName($param->var);
76+
77+
// find dim fetches in there
78+
$arrayDimFetches = $this->arrayDimFetchFinder->findByVariableName($node, $paramName);
79+
80+
// no dim fetches found
81+
if ($arrayDimFetches === []) {
82+
continue;
83+
}
84+
85+
// param type is array
86+
$param->type = new Identifier('array');
87+
$hasChanged = true;
88+
}
89+
90+
if ($hasChanged === false) {
91+
return null;
92+
}
93+
94+
return $node;
95+
}
96+
97+
public function provideMinPhpVersion(): int
98+
{
99+
return PhpVersionFeature::SCALAR_TYPES;
100+
}
101+
}

rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333

3434
public function getRuleDefinition(): RuleDefinition
3535
{
36-
return new RuleDefinition('Add function/closure param array type, if dim fetch is inside', [
36+
return new RuleDefinition('Add closure and arrow function param array type, if dim fetch is used inside', [
3737
new CodeSample(
3838
<<<'CODE_SAMPLE'
3939
$array = [['name' => 'John']];

src/Config/Level/TypeDeclarationLevel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
1616
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector;
1717
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector;
18+
use Rector\TypeDeclaration\Rector\ClassMethod\AddClassMethodParamArrayWhereDimFetchRector;
1819
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
1920
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;
2021
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector;
@@ -141,8 +142,12 @@ final class TypeDeclarationLevel
141142
StrictStringParamConcatRector::class,
142143
TypedPropertyFromJMSSerializerAttributeTypeRector::class,
143144

145+
// array parameter from dim fetch assign inside
146+
AddClassMethodParamArrayWhereDimFetchRector::class,
147+
144148
// possibly based on docblocks, but also helpful, intentionally last
145149
AddArrayFunctionClosureParamTypeRector::class,
146150
TypedPropertyFromDocblockSetUpDefinedRector::class,
151+
147152
];
148153
}

0 commit comments

Comments
 (0)