diff --git a/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/some_class.php.inc b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/some_class.php.inc new file mode 100644 index 00000000000..583691319c7 --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/some_class.php.inc @@ -0,0 +1,35 @@ + 50) { + return 'yes'; + } + + return 'no'; + } +} + +?> +----- + 50) { + return 'yes'; + } + + return 'no'; + } +} + +?> diff --git a/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/ReduceAlwaysFalseIfOrRectorTest.php b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/ReduceAlwaysFalseIfOrRectorTest.php new file mode 100644 index 00000000000..b3350c9a97e --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/ReduceAlwaysFalseIfOrRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/config/configured_rule.php b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/config/configured_rule.php new file mode 100644 index 00000000000..52cfb81d996 --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(ReduceAlwaysFalseIfOrRector::class); +}; diff --git a/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php b/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php new file mode 100644 index 00000000000..565563b11b9 --- /dev/null +++ b/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php @@ -0,0 +1,88 @@ + 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> + */ + 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; + } +} diff --git a/src/Config/Level/DeadCodeLevel.php b/src/Config/Level/DeadCodeLevel.php index 203abc1c72e..7e3ca367ec2 100644 --- a/src/Config/Level/DeadCodeLevel.php +++ b/src/Config/Level/DeadCodeLevel.php @@ -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; @@ -94,6 +95,7 @@ final class DeadCodeLevel RemovePhpVersionIdCheckRector::class, RemoveAlwaysTrueIfConditionRector::class, + ReduceAlwaysFalseIfOrRector::class, RemoveUnusedPrivateClassConstantRector::class, RemoveUnusedPrivatePropertyRector::class,