Skip to content

Commit 0fe6597

Browse files
committed
[tdd] Add AddParamArrayDocblockBasedOnArrayMapRector
1 parent fe7432b commit 0fe6597

5 files changed

Lines changed: 143 additions & 0 deletions

File tree

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,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,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector;
7+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromAssignsParamToParamReferenceRector;
8+
9+
return RectorConfig::configure()
10+
->withRules([AddParamArrayDocblockBasedOnArrayMapRector::class]);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use Rector\Rector\AbstractRector;
9+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
10+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
11+
12+
final class AddParamArrayDocblockBasedOnArrayMapRector extends AbstractRector
13+
{
14+
15+
public function getRuleDefinition(): RuleDefinition
16+
{
17+
return new RuleDefinition('Add @param array docblock if array_map is used on the parameter', [
18+
new CodeSample(
19+
<<<'CODE_SAMPLE'
20+
final class SomeClass
21+
{
22+
public function run(array $names): void
23+
{
24+
$names = array_map(fn(string $name) => trim($name), $names);
25+
}
26+
}
27+
CODE_SAMPLE
28+
,
29+
<<<'CODE_SAMPLE'
30+
final class SomeClass
31+
{
32+
/**
33+
* @param string[] $names
34+
*/
35+
public function run(array $names): void
36+
{
37+
$names = array_map(fn(string $name) => trim($name), $names);
38+
}
39+
}
40+
CODE_SAMPLE
41+
42+
)
43+
]);
44+
}
45+
46+
/**
47+
* @return array<class-string<Node>>
48+
*/
49+
public function getNodeTypes(): array
50+
{
51+
return [Node\Stmt\ClassMethod::class, Node\Stmt\Function_::class];
52+
}
53+
54+
/**
55+
* @param Node\Stmt\ClassMethod|Node\Stmt\Function_ $node
56+
*/
57+
public function refactor(Node $node)
58+
{
59+
if ($node->getParams() === []) {
60+
return null;
61+
}
62+
63+
foreach ($node->params as $param) {
64+
// handle only arrays
65+
if (! $param->type instanceof Node\Identifier || ! $this->isName($param->type, 'array')) {
66+
continue;
67+
}
68+
}
69+
70+
71+
// TODO: Implement refactor() method.
72+
}
73+
}

src/Config/Level/TypeDeclarationDocblocksLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromGetterReturnRector;
1313
use Rector\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector;
1414
use Rector\TypeDeclarationDocblocks\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
15+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockBasedOnArrayMapRector;
1516
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromDataProviderRector;
1617
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromDimFetchAccessRector;
1718
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForArrayDimAssignedObjectRector;
@@ -37,6 +38,7 @@ final class TypeDeclarationDocblocksLevel
3738
// param
3839
AddParamArrayDocblockFromDimFetchAccessRector::class,
3940
ClassMethodArrayDocblockParamFromLocalCallsRector::class,
41+
AddParamArrayDocblockBasedOnArrayMapRector::class,
4042

4143
// return
4244
AddReturnDocblockForCommonObjectDenominatorRector::class,

0 commit comments

Comments
 (0)