From b4a1edfb887a550bc1b437d1a5d85167f7157a17 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 29 Apr 2022 04:43:24 +0700 Subject: [PATCH] [DeadCode] Add empty and multi stmts support on RemoveAlwaysTrueIfConditionRector (#2192) --- .../Fixture/empty_stmts.php.inc | 30 ++++++++++++++++ .../Fixture/multi_stmts.php.inc | 34 +++++++++++++++++++ .../If_/RemoveAlwaysTrueIfConditionRector.php | 12 ++++--- 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/empty_stmts.php.inc create mode 100644 rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/multi_stmts.php.inc diff --git a/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/empty_stmts.php.inc b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/empty_stmts.php.inc new file mode 100644 index 00000000000..a384ddcd2b9 --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/empty_stmts.php.inc @@ -0,0 +1,30 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/multi_stmts.php.inc b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/multi_stmts.php.inc new file mode 100644 index 00000000000..e676c3c8f19 --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/multi_stmts.php.inc @@ -0,0 +1,34 @@ + +----- + diff --git a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php index d1c3ece86b4..689763b16ca 100644 --- a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php +++ b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Rector\DeadCode\Rector\If_; use PhpParser\Node; +use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\If_; use PHPStan\Type\Constant\ConstantBooleanType; use Rector\Core\Rector\AbstractRector; @@ -59,8 +60,9 @@ CODE_SAMPLE /** * @param If_ $node + * @return If_|null|Stmt[] */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): If_|null|array { if ($node->else !== null) { return null; @@ -80,11 +82,11 @@ CODE_SAMPLE return null; } - if (count($node->stmts) !== 1) { - // unable to handle now - return null; + if ($node->stmts === []) { + $this->removeNode($node); + return $node; } - return $node->stmts[0]; + return $node->stmts; } }