-
-
Notifications
You must be signed in to change notification settings - Fork 426
[DeadCode] Add RemoveDeadIfBlocksRector #7528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
john-shaffer
wants to merge
7
commits into
rectorphp:main
Choose a base branch
from
john-shaffer:add-remove-dead-if-blocks-rector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+494
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0bf5355
[DeadCode] Add RemoveDeadIfBlocksRector
john-shaffer 6ce2fcb
[DeadCode] Add RemoveDeadIfBlocksRector to levels
john-shaffer 02d3dca
Fix removing elseifs that should be skipped
john-shaffer 36b3d4f
Use SideEffectNodeDetector
john-shaffer ea69bfb
Rename to singular to match other class names
john-shaffer c1cfff7
Test that RemoveDeadIfBlock preserves calls
john-shaffer 85c6f5c
Fix typo
john-shaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/fixture.php.inc
This file contains hidden or 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,52 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class RemoveDeadIfBlock | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($value) { | ||
| } elseif ($differentValue) { | ||
| } else { | ||
| } | ||
|
|
||
| if ($differentValue) { | ||
| echo 'different'; | ||
| } elseif ($value) { | ||
| } else { | ||
| } | ||
|
|
||
| if ($differentValue) { | ||
| } elseif ($value) { | ||
| echo 'value'; | ||
| } else { | ||
| } | ||
|
|
||
| return $differentValue; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class RemoveDeadIfBlock | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($differentValue) { | ||
| echo 'different'; | ||
| } | ||
|
|
||
| if (!$differentValue && $value) { | ||
| echo 'value'; | ||
| } | ||
|
|
||
| return $differentValue; | ||
| } | ||
| } | ||
|
|
||
| ?> |
32 changes: 32 additions & 0 deletions
32
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/if_else_no_stmt.php.inc
This file contains hidden or 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,32 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfElseNoStmt | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($condition) { | ||
| echo "something"; | ||
| } else { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfElseNoStmt | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($condition) { | ||
| echo "something"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> |
36 changes: 36 additions & 0 deletions
36
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/if_elseif_no_stmt.php.inc
This file contains hidden or 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,36 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfElseNoStmt | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($condition) { | ||
| echo "something"; | ||
| } elseif (!$condition) { | ||
| } elseif (true) { | ||
| echo "global"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfElseNoStmt | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($condition) { | ||
| echo "something"; | ||
| } elseif (true) { | ||
| echo "global"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> |
32 changes: 32 additions & 0 deletions
32
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/if_no_stmt_with_else.php.inc
This file contains hidden or 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,32 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfNoStmtWithElse | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($condition) { | ||
| } else { | ||
| echo "something"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfNoStmtWithElse | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if (!$condition) { | ||
| echo "something"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> |
28 changes: 28 additions & 0 deletions
28
.../DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/if_no_stmt_with_else_no_stmt.php.inc
This file contains hidden or 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 | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfNoStmtWithElseNoStmt | ||
| { | ||
| public function run($condition) | ||
| { | ||
| if ($condition) { | ||
| } else { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class IfNoStmtWithElseNoStmt | ||
| { | ||
| public function run($condition) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> |
45 changes: 45 additions & 0 deletions
45
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/side_effect_checks.php.inc
This file contains hidden or 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,45 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class SideEffectChecks | ||
| { | ||
| public function run() | ||
| { | ||
| if (10) { | ||
| } | ||
|
|
||
| if (true) { | ||
| } | ||
|
|
||
| if (5 + 10) { | ||
| } | ||
|
|
||
| if ($this->someCall()) { | ||
| } | ||
|
|
||
| if (false) { | ||
| } elseif ($this->anotherCall()) { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class SideEffectChecks | ||
| { | ||
| public function run() | ||
| { | ||
| if ($this->someCall()) { | ||
| } | ||
|
|
||
| if (!false && $this->anotherCall()) { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
19 changes: 19 additions & 0 deletions
19
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/skip_if_comment.php.inc
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class MergeComments | ||
| { | ||
| public function run($b) | ||
| { | ||
| // toplevel | ||
| if ($b) { | ||
| } elseif (!$b) { | ||
| // first inner | ||
| f(); | ||
| // second inner | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> |
17 changes: 17 additions & 0 deletions
17
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/skip_property_fetch.php.inc
This file contains hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class SkipPropertyFetch | ||
| { | ||
| public function run($obj, $condition) | ||
| { | ||
| if ($condition) { | ||
| echo '1'; | ||
| } elseif ($obj->v) { | ||
| echo '2'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> |
19 changes: 19 additions & 0 deletions
19
...-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/skip_used_in_next_stmt.php.inc
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class SkipUsedInNextStmt | ||
| { | ||
| public function run($resultLen, $result, $decimalSep) | ||
| { | ||
| for ($i = $resultLen - 1; $i >= 0 && $result[$i] === '0'; $i--) { | ||
| ; | ||
| } | ||
|
|
||
| if ($i >= 0 && $result[$i] === $decimalSep) { | ||
| $i--; | ||
| } | ||
|
|
||
| $result = substr($result, 0, $i + 1); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/Fixture/skip_used_in_next_stmt2.php.inc
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture; | ||
|
|
||
| class SkipUsedInNextStmt2 | ||
| { | ||
| public function run($resultLen, $result, $decimalSep) | ||
| { | ||
| foreach ($result as $i => $value) { | ||
| ; | ||
| } | ||
|
|
||
| if ($i >= 0 && $result[$i] === $decimalSep) { | ||
| $i--; | ||
| } | ||
|
|
||
| $result = substr($result, 0, $i + 1); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
rules-tests/DeadCode/Rector/If_/RemoveDeadIfBlockRector/RemoveDeadIfBlockRectorTest.php
This file contains hidden or 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\If_\RemoveDeadIfBlockRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class RemoveDeadIfBlockRectorTest 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/If_/RemoveDeadIfBlockRector/config/configured_rule.php
This file contains hidden or 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\If_\RemoveDeadIfBlockRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([RemoveDeadIfBlockRector::class]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.