[DeadCode] Add empty and multi stmts support on RemoveAlwaysTrueIfConditionRector (#2192)

This commit is contained in:
Abdul Malik Ikhsan 2022-04-29 04:43:24 +07:00 committed by GitHub
parent a218dda059
commit b4a1edfb88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 5 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
class EmptyStmts
{
public function run()
{
if (true === true) {
}
return 'hello';
}
}
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
class EmptyStmts
{
public function run()
{
return 'hello';
}
}
?>

View File

@ -0,0 +1,34 @@
<?php
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
class MultiStmts
{
public function run()
{
if (true === true) {
echo 'test';
echo 'test 2';
}
return 'hello';
}
}
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
class MultiStmts
{
public function run()
{
echo 'test';
echo 'test 2';
return 'hello';
}
}
?>

View File

@ -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;
}
}