Skip to content

Commit 66d9316

Browse files
authored
[DeadCode] Add ReplaceBlockToItsStmtsRector (#6641)
* [DeadCode] Add ReplaceBlockToItsStmtsRector * add fixture for skip normal statments
1 parent f3242bf commit 66d9316

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 28 additions & 0 deletions
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\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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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]);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/Config/Level/DeadCodeLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
1010
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
1111
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
12+
use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;
1213
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
1314
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
1415
use 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,

0 commit comments

Comments
 (0)