mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-20 18:12:24 +00:00
[DeadCode] Add empty and multi stmts support on RemoveAlwaysTrueIfConditionRector (#2192)
This commit is contained in:
parent
a218dda059
commit
b4a1edfb88
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user