[DeadCode] Add RemoveAndTrueRector

This commit is contained in:
Tomas Votruba 2019-05-05 18:48:45 +02:00
parent 82ad572707
commit 4f5469f62d
5 changed files with 156 additions and 0 deletions

View File

@ -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: ~

View File

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

View File

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

View File

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

View File

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