File tree Expand file tree Collapse file tree 6 files changed +130
-0
lines changed
rules-tests/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector
rules/DeadCode/Rector/Block Expand file tree Collapse file tree 6 files changed +130
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector \Fixture ;
4+
5+ {
6+ echo "statement 1 " ;
7+ echo PHP_EOL ;
8+ echo "statement 2 " ;
9+ }
10+
11+ ?>
12+ -----
13+ <?php
14+
15+ namespace Rector \Tests \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector \Fixture ;
16+
17+ echo "statement 1 " ;
18+ echo PHP_EOL ;
19+ echo "statement 2 " ;
20+
21+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector \Fixture ;
4+
5+ function skipNormalStatements ()
6+ {
7+ echo "statement 1 " ;
8+ echo PHP_EOL ;
9+ echo "statement 2 " ;
10+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Tests \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector ;
6+
7+ use Iterator ;
8+ use PHPUnit \Framework \Attributes \DataProvider ;
9+ use Rector \Testing \PHPUnit \AbstractRectorTestCase ;
10+
11+ final class ReplaceBlockToItsStmtsRectorTest 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+ declare (strict_types=1 );
4+
5+ use Rector \Config \RectorConfig ;
6+ use Rector \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector ;
7+
8+ return RectorConfig::configure ()
9+ ->withRules ([ReplaceBlockToItsStmtsRector::class]);
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \DeadCode \Rector \Block ;
6+
7+ use PhpParser \Node ;
8+ use PhpParser \Node \Stmt ;
9+ use PhpParser \Node \Stmt \Block ;
10+ use Rector \Rector \AbstractRector ;
11+ use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
12+ use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
13+
14+ /**
15+ * @see \Rector\Tests\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector\ReplaceBlockToItsStmtsRectorTest
16+ * @see https://3v4l.org/ZUfEV
17+ */
18+ final class ReplaceBlockToItsStmtsRector extends AbstractRector
19+ {
20+ public function getRuleDefinition (): RuleDefinition
21+ {
22+ return new RuleDefinition (
23+ 'Replace Block Stmt with its stmts ' ,
24+ [
25+ new CodeSample (
26+ <<<'CODE_SAMPLE'
27+ {
28+ echo "statement 1";
29+ echo PHP_EOL;
30+ echo "statement 2";
31+ }
32+ CODE_SAMPLE
33+ ,
34+ <<<'CODE_SAMPLE'
35+ echo "statement 1";
36+ echo PHP_EOL;
37+ echo "statement 2";
38+ CODE_SAMPLE
39+ ),
40+ ]
41+ );
42+ }
43+
44+ /**
45+ * @return array<class-string<Node>>
46+ */
47+ public function getNodeTypes (): array
48+ {
49+ return [Block::class];
50+ }
51+
52+ /**
53+ * @param Block $node
54+ * @return Stmt[]
55+ */
56+ public function refactor (Node $ node ): array
57+ {
58+ return $ node ->stmts ;
59+ }
60+ }
Original file line number Diff line number Diff line change 99use Rector \DeadCode \Rector \Array_ \RemoveDuplicatedArrayKeyRector ;
1010use Rector \DeadCode \Rector \Assign \RemoveDoubleAssignRector ;
1111use Rector \DeadCode \Rector \Assign \RemoveUnusedVariableAssignRector ;
12+ use Rector \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector ;
1213use Rector \DeadCode \Rector \BooleanAnd \RemoveAndTrueRector ;
1314use Rector \DeadCode \Rector \Cast \RecastingRemovalRector ;
1415use Rector \DeadCode \Rector \ClassConst \RemoveUnusedPrivateClassConstantRector ;
@@ -82,6 +83,7 @@ final class DeadCodeLevel
8283 RemoveUnusedNonEmptyArrayBeforeForeachRector::class,
8384 RemoveNullPropertyInitializationRector::class,
8485 RemoveUselessReturnExprInConstructRector::class,
86+ ReplaceBlockToItsStmtsRector::class,
8587
8688 RemoveTypedPropertyDeadInstanceOfRector::class,
8789 TernaryToBooleanOrFalseToBooleanAndRector::class,
You can’t perform that action at this time.
0 commit comments