File tree Expand file tree Collapse file tree
rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddParamArrayDocblockBasedOnArrayMapRector
rules/TypeDeclarationDocblocks/Rector/ClassMethod Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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]);
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1212use Rector \TypeDeclarationDocblocks \Rector \Class_ \DocblockVarArrayFromGetterReturnRector ;
1313use Rector \TypeDeclarationDocblocks \Rector \Class_ \DocblockVarArrayFromPropertyDefaultsRector ;
1414use Rector \TypeDeclarationDocblocks \Rector \Class_ \DocblockVarFromParamDocblockInConstructorRector ;
15+ use Rector \TypeDeclarationDocblocks \Rector \ClassMethod \AddParamArrayDocblockBasedOnArrayMapRector ;
1516use Rector \TypeDeclarationDocblocks \Rector \ClassMethod \AddParamArrayDocblockFromDataProviderRector ;
1617use Rector \TypeDeclarationDocblocks \Rector \ClassMethod \AddParamArrayDocblockFromDimFetchAccessRector ;
1718use 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,
You can’t perform that action at this time.
0 commit comments