Skip to content

Commit

Permalink
[DeadCode] Add ReplaceBlockToItsStmtsRector (#6641)
Browse files Browse the repository at this point in the history
* [DeadCode] Add ReplaceBlockToItsStmtsRector

* add fixture for skip normal statments
  • Loading branch information
samsonasik authored Jan 1, 2025
1 parent f3242bf commit 66d9316
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector\Fixture;

{
echo "statement 1";
echo PHP_EOL;
echo "statement 2";
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector\Fixture;

echo "statement 1";
echo PHP_EOL;
echo "statement 2";

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector\Fixture;

function skipNormalStatements()
{
echo "statement 1";
echo PHP_EOL;
echo "statement 2";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReplaceBlockToItsStmtsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;

return RectorConfig::configure()
->withRules([ReplaceBlockToItsStmtsRector::class]);
60 changes: 60 additions & 0 deletions rules/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\Block;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Block;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector\ReplaceBlockToItsStmtsRectorTest
* @see https://3v4l.org/ZUfEV
*/
final class ReplaceBlockToItsStmtsRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace Block Stmt with its stmts',
[
new CodeSample(
<<<'CODE_SAMPLE'
{
echo "statement 1";
echo PHP_EOL;
echo "statement 2";
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo "statement 1";
echo PHP_EOL;
echo "statement 2";
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Block::class];
}

/**
* @param Block $node
* @return Stmt[]
*/
public function refactor(Node $node): array
{
return $node->stmts;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/DeadCodeLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector;
Expand Down Expand Up @@ -82,6 +83,7 @@ final class DeadCodeLevel
RemoveUnusedNonEmptyArrayBeforeForeachRector::class,
RemoveNullPropertyInitializationRector::class,
RemoveUselessReturnExprInConstructRector::class,
ReplaceBlockToItsStmtsRector::class,

RemoveTypedPropertyDeadInstanceOfRector::class,
TernaryToBooleanOrFalseToBooleanAndRector::class,
Expand Down

0 comments on commit 66d9316

Please sign in to comment.