-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add ReplaceBlockToItsStmtsRector (#6641)
* [DeadCode] Add ReplaceBlockToItsStmtsRector * add fixture for skip normal statments
- Loading branch information
1 parent
f3242bf
commit 66d9316
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
rules-tests/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector/Fixture/skip_normal_statements.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
28 changes: 28 additions & 0 deletions
28
...s/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector/ReplaceBlockToItsStmtsRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
rules-tests/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
rules/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters