-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add early return check to RemoveUnusedNonEmptyArrayBeforeF…
…oreachRector (#3611) * [DeadCode] Add early return check to RemoveUnusedNonEmptyArrayBeforeForeachRector * skip edge case * Add fixture multi if -> foreach * Add the patch by loop jump to key recursive * [ci-review] Rector Rectify * clean up skip config * Revert clean up skip config This reverts commit 22c3df8. * Revert [ci-review] Rector Rectify This reverts commit 10a8bfa. * Revert Add the patch by loop jump to key recursive This reverts commit 9375b38. * Revert Add fixture multi if -> foreach This reverts commit ceb6b33. * add failing fixture throw inside empty * Add patch ensure if only return with no expr * add fixture for multi ifs and fixtures * Fixed 🎉 * rename fixture * skip has return with value inside foreach * rename fixture * optimize, allow multi if check, but last only * temporary pin phpunit to 10.0.19 due to make error on paratest * Fix phpstan * use paratest 7.1.3 --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
63b9e2b
commit 38a9b29
Showing
8 changed files
with
248 additions
and
32 deletions.
There are no files selected for viewing
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
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
35 changes: 35 additions & 0 deletions
35
...ctor/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector/Fixture/if_check_with_empty.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
final class IfCheckWithEmpty | ||
{ | ||
public function run(array $items) | ||
{ | ||
if (empty($items)) { | ||
return; | ||
} | ||
|
||
foreach ($items as $item) { | ||
echo $item; | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
final class IfCheckWithEmpty | ||
{ | ||
public function run(array $items) | ||
{ | ||
foreach ($items as $item) { | ||
echo $item; | ||
} | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...nusedNonEmptyArrayBeforeForeachRector/Fixture/if_with_return_value_inside_foreach.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
final class IfWithReturnValueInsideForeach | ||
{ | ||
public function run(array $items) | ||
{ | ||
if (empty($items)) { | ||
return; | ||
} | ||
|
||
foreach ($items as $item) { | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
final class IfWithReturnValueInsideForeach | ||
{ | ||
public function run(array $items) | ||
{ | ||
foreach ($items as $item) { | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
?> |
51 changes: 51 additions & 0 deletions
51
...f_/RemoveUnusedNonEmptyArrayBeforeForeachRector/Fixture/multi_if_check_with_empty.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,51 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
final class MultiIfCheckWithEmpty | ||
{ | ||
public function run(array $items, array $items2) | ||
{ | ||
if (empty($items)) { | ||
return; | ||
} | ||
|
||
foreach ($items as $item) { | ||
echo $item; | ||
} | ||
|
||
if (empty($items2)) { | ||
return; | ||
} | ||
|
||
foreach ($items2 as $item2) { | ||
echo $item2; | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
final class MultiIfCheckWithEmpty | ||
{ | ||
public function run(array $items, array $items2) | ||
{ | ||
if (empty($items)) { | ||
return; | ||
} | ||
|
||
foreach ($items as $item) { | ||
echo $item; | ||
} | ||
|
||
foreach ($items2 as $item2) { | ||
echo $item2; | ||
} | ||
} | ||
} | ||
|
||
?> |
19 changes: 19 additions & 0 deletions
19
...moveUnusedNonEmptyArrayBeforeForeachRector/Fixture/skip_if_check_throw_with_empty.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,19 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture; | ||
|
||
use Exception; | ||
|
||
final class SkipIfCheckThrowWithEmpty | ||
{ | ||
public function run(array $items) | ||
{ | ||
if (empty($items)) { | ||
throw new Exception('items must not be empty'); | ||
} | ||
|
||
foreach ($items as $item) { | ||
echo $item; | ||
} | ||
} | ||
} |
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
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