diff --git a/config/set/dead-code/dead-code.yaml b/config/set/dead-code/dead-code.yaml index 093b3f1aeff7..4500aa64dc96 100644 --- a/config/set/dead-code/dead-code.yaml +++ b/config/set/dead-code/dead-code.yaml @@ -29,3 +29,4 @@ services: Rector\DeadCode\Rector\Property\RemoveNullPropertyInitializationRector: ~ Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector: ~ Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector: ~ + Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector: ~ diff --git a/packages/DeadCode/src/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector.php b/packages/DeadCode/src/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector.php new file mode 100644 index 000000000000..6f57c1b2e579 --- /dev/null +++ b/packages/DeadCode/src/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector.php @@ -0,0 +1,85 @@ +getBool() : false; + } + + private function getBool(): bool + { + return (bool) 5; + } +} +PHP +, + <<<'PHP' +class SomeClass +{ + public function go() + { + return $value && $this->getBool(); + } + + private function getBool(): bool + { + return (bool) 5; + } +} +PHP + + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [Ternary::class]; + } + + /** + * @param Ternary $node + */ + public function refactor(Node $node): ?Node + { + if ($node->if === null) { + return null; + } + + if (! $this->isFalse($node->else)) { + return null; + } + + $ifType = $this->getStaticType($node->if); + if (! $ifType instanceof BooleanType) { + return null; + } + + return new Node\Expr\BinaryOp\BooleanAnd($node->cond, $node->if); + } +} diff --git a/packages/DeadCode/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/Fixture/fixture.php.inc b/packages/DeadCode/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..a904f39e438a --- /dev/null +++ b/packages/DeadCode/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/Fixture/fixture.php.inc @@ -0,0 +1,37 @@ +getBool() : false; + } + + private function getBool(): bool + { + return (bool) 5; + } +} + +?> +----- +getBool(); + } + + private function getBool(): bool + { + return (bool) 5; + } +} + +?> diff --git a/packages/DeadCode/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/TernaryToBooleanOrFalseToBooleanAndRectorTest.php b/packages/DeadCode/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/TernaryToBooleanOrFalseToBooleanAndRectorTest.php new file mode 100644 index 000000000000..26dc321d09f7 --- /dev/null +++ b/packages/DeadCode/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/TernaryToBooleanOrFalseToBooleanAndRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + public function provideDataForTest(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return TernaryToBooleanOrFalseToBooleanAndRector::class; + } +}