Skip to content

Commit 67fd8d5

Browse files
committed
add MakeUseOfContaintsStmtsRector
1 parent 0952f96 commit 67fd8d5

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

rector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
__DIR__ . '/config',
3636
__DIR__ . '/build/build-preload.php',
3737
])
38+
->withRules([
39+
\Rector\Utils\Rector\MakeUseOfContaintsStmtsRector::class,
40+
])
3841
->withRootFiles()
3942
->withImportNames(removeUnusedImports: true)
4043
->withSkip([

tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ public function testUpdateNestedClassAnnotation(): void
5555
continue;
5656
}
5757

58-
if ($newStmt->stmts === null) {
59-
continue;
60-
}
61-
62-
foreach ($newStmt->stmts as $stmt) {
58+
foreach ($newStmt->getStmts() as $stmt) {
6359
if (! $stmt instanceof Class_) {
6460
continue;
6561
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Utils\Rector;
6+
7+
// e.g. to move PhpDocInfo to the particular rule itself
8+
use PhpParser\Node;
9+
use PhpParser\Node\ContainsStmts;
10+
use PhpParser\Node\Expr\MethodCall;
11+
use PhpParser\Node\Expr\PropertyFetch;
12+
use PHPStan\Type\ObjectType;
13+
use Rector\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
final class MakeUseOfContaintsStmtsRector extends AbstractRector
17+
{
18+
public function getRuleDefinition(): RuleDefinition
19+
{
20+
// @see https://github.com/nikic/PHP-Parser/pull/1113
21+
return new RuleDefinition('Move $node->stmts to $node->getStmts() for ContaintsStmts', []);
22+
}
23+
24+
public function getNodeTypes(): array
25+
{
26+
return [PropertyFetch::class];
27+
}
28+
29+
/**
30+
* @param PropertyFetch $node
31+
*/
32+
public function refactor(Node $node): ?Node
33+
{
34+
if (! $this->isName($node->name, 'stmts')) {
35+
return null;
36+
}
37+
38+
if (! $this->isObjectType($node->var, new ObjectType(ContainsStmts::class))) {
39+
return null;
40+
}
41+
42+
return new MethodCall($node->var, 'getStmts');
43+
}
44+
}

0 commit comments

Comments
 (0)