mirror of
https://github.com/rectorphp/rector.git
synced 2024-08-12 11:30:23 +00:00
[DeadCode] Add RemoveAndTrueRector
This commit is contained in:
parent
82ad572707
commit
4f5469f62d
@ -15,3 +15,4 @@ services:
|
||||
Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector: ~
|
||||
Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector: ~
|
||||
Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector: ~
|
||||
Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector: ~
|
||||
|
@ -0,0 +1,74 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Rector\BooleanAnd;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PHPStan\Type\Constant\ConstantBooleanType;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class RemoveAndTrueRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Remove and true that has no added value', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return true && 5 === 1;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return 5 === 1;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [BooleanAnd::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BooleanAnd $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($this->isConstantTrue($node->left)) {
|
||||
return $node->right;
|
||||
}
|
||||
|
||||
if ($this->isConstantTrue($node->right)) {
|
||||
return $node->left;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isConstantTrue(Node $node): bool
|
||||
{
|
||||
$leftStaticType = $this->getStaticType($node);
|
||||
if (! $leftStaticType instanceof ConstantBooleanType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $leftStaticType->getValue() === true;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\BooleanAnd\RemoveAndTrueRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$isTrue = true && 5 === 1;
|
||||
|
||||
$isFalse = true && true && 5 === 1;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\BooleanAnd\RemoveAndTrueRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$isTrue = 5 === 1;
|
||||
|
||||
$isFalse = 5 === 1;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\BooleanAnd\RemoveAndTrueRector\Fixture;
|
||||
|
||||
class KeepSomething
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$isTrue = true && true;
|
||||
|
||||
$isTrue = true && true && 1 === 14;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\BooleanAnd\RemoveAndTrueRector\Fixture;
|
||||
|
||||
class KeepSomething
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$isTrue = true;
|
||||
|
||||
$isTrue = 1 === 14;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\BooleanAnd\RemoveAndTrueRector;
|
||||
|
||||
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class RemoveAndTrueRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/keep_something.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveAndTrueRector::class;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user