Skip to content

Commit

Permalink
[DeadCode] Add new rule - ReduceAlwaysFalseIfOrRector (#5750)
Browse files Browse the repository at this point in the history
* [DeadCode] Add ReduceAlwaysFalseIfOrRector

* add to dead-code ruleset
  • Loading branch information
TomasVotruba committed Mar 21, 2024
1 parent 98a2a6f commit 0dd1a43
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture;

class SomeClass
{
public function run(int $number)
{
if (! is_int($number) || $number > 50) {
return 'yes';
}

return 'no';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture;

class SomeClass
{
public function run(int $number)
{
if ($number > 50) {
return 'yes';
}

return 'no';
}
}

?>
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReduceAlwaysFalseIfOrRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReduceAlwaysFalseIfOrRector::class);
};
88 changes: 88 additions & 0 deletions rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\Constant\ConstantBooleanType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\ReduceAlwaysFalseIfOrRectorTest
*/
final class ReduceAlwaysFalseIfOrRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Reduce always false in a if ( || ) condition', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(int $number)
{
if (! is_int($number) || $number > 50) {
return 'yes';
}
return 'no';
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(int $number)
{
if ($number > 50) {
return 'yes';
}
return 'no';
}
}
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [If_::class];
}

/**
* @param If_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->cond instanceof BooleanOr) {
return null;
}

$booleanOr = $node->cond;

$conditionStaticType = $this->getType($booleanOr->left);
if (! $conditionStaticType instanceof ConstantBooleanType) {
return null;
}

if ($conditionStaticType->getValue()) {
return null;
}

$node->cond = $booleanOr->right;

return $node;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/DeadCodeLevel.php
Expand Up @@ -29,6 +29,7 @@
use Rector\DeadCode\Rector\For_\RemoveDeadLoopRector;
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\DeadCode\Rector\If_\RemoveTypedPropertyDeadInstanceOfRector;
Expand Down Expand Up @@ -94,6 +95,7 @@ final class DeadCodeLevel
RemovePhpVersionIdCheckRector::class,

RemoveAlwaysTrueIfConditionRector::class,
ReduceAlwaysFalseIfOrRector::class,
RemoveUnusedPrivateClassConstantRector::class,
RemoveUnusedPrivatePropertyRector::class,

Expand Down

0 comments on commit 0dd1a43

Please sign in to comment.