diff --git a/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/fixture.php.inc b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..eb25989d9ed --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/skip_different_type.php.inc b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/skip_different_type.php.inc new file mode 100644 index 00000000000..823854898ab --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/skip_different_type.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_array_empty.php.inc b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_array_empty.php.inc new file mode 100644 index 00000000000..ef60dd833cb --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_array_empty.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_if_equal.php.inc b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_if_equal.php.inc new file mode 100644 index 00000000000..f313c151152 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_if_equal.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_integer_zero.php.inc b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_integer_zero.php.inc new file mode 100644 index 00000000000..13f2160d9d9 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/Fixture/with_integer_zero.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/RemoveUselessTernaryRectorTest.php b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/RemoveUselessTernaryRectorTest.php new file mode 100644 index 00000000000..c0d2f7b0d16 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/RemoveUselessTernaryRectorTest.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/Ternary/RemoveUselessTernaryRector/config/configured_rule.php b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/config/configured_rule.php new file mode 100644 index 00000000000..abc80c166c5 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Ternary/RemoveUselessTernaryRector/config/configured_rule.php @@ -0,0 +1,9 @@ +withRules([RemoveUselessTernaryRector::class]); diff --git a/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php b/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php new file mode 100644 index 00000000000..7d30ee12c63 --- /dev/null +++ b/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php @@ -0,0 +1,100 @@ +> + */ + public function getNodeTypes(): array + { + return [Ternary::class]; + } + + /** + * @param Ternary $node + */ + public function refactor(Node $node): ?Node + { + /** + * if condition is negated, skip + * switch negated ternary condition early via SwitchNegatedTernaryRector for that + * if needed + */ + if ($node->cond instanceof BooleanNot) { + return null; + } + + $nativeType = $this->nodeTypeResolver->getNativeType($node->cond); + if ($nativeType instanceof BooleanType + && $node->if instanceof Expr + && $this->valueResolver->isTrue($node->if) + && $this->valueResolver->isFalse($node->else)) { + return $node->cond; + } + + if ($node->if instanceof Expr && ! $this->nodeComparator->areNodesEqual($node->if, $node->cond)) { + return null; + } + + if ($nativeType instanceof BooleanType && $this->valueResolver->isFalse($node->else)) { + return $node->cond; + } + + if ($nativeType instanceof ArrayType && $node->else instanceof Array_ && $node->else->items === []) { + return $node->cond; + } + + if ($nativeType instanceof IntegerType && $node->else instanceof Int_ && $node->else->value === 0) { + return $node->cond; + } + + return null; + } +} diff --git a/src/Config/Level/DeadCodeLevel.php b/src/Config/Level/DeadCodeLevel.php index 2beee307b47..3bb5770ca97 100644 --- a/src/Config/Level/DeadCodeLevel.php +++ b/src/Config/Level/DeadCodeLevel.php @@ -61,6 +61,7 @@ use Rector\DeadCode\Rector\Stmt\RemoveNextSameValueConditionRector; use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector; use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector; +use Rector\DeadCode\Rector\Ternary\RemoveUselessTernaryRector; use Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector; use Rector\DeadCode\Rector\TryCatch\RemoveDeadCatchRector; use Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector; @@ -100,6 +101,7 @@ final class DeadCodeLevel RemoveTypedPropertyDeadInstanceOfRector::class, TernaryToBooleanOrFalseToBooleanAndRector::class, + RemoveUselessTernaryRector::class, RemoveDoubleAssignRector::class, RemoveUselessAssignFromPropertyPromotionRector::class, RemoveConcatAutocastRector::class,