diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_false.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_false.php.inc new file mode 100644 index 00000000000..f46d291f9f5 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_false.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_true.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_true.php.inc new file mode 100644 index 00000000000..8641a83101c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_true.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_false.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_false.php.inc new file mode 100644 index 00000000000..df6540fdb42 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_false.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_true.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_true.php.inc new file mode 100644 index 00000000000..15ae43f88a0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_true.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/if_condition.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/if_condition.php.inc new file mode 100644 index 00000000000..13c13f3b839 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/if_condition.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_false.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_false.php.inc new file mode 100644 index 00000000000..17ae28668d0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_false.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_true.php.inc b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_true.php.inc new file mode 100644 index 00000000000..a99ef6f2227 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_true.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/ReplaceConstantBooleanNotRectorTest.php b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/ReplaceConstantBooleanNotRectorTest.php new file mode 100644 index 00000000000..06f47e776dc --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/ReplaceConstantBooleanNotRectorTest.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/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/config/configured_rule.php new file mode 100644 index 00000000000..9a35874773d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/config/configured_rule.php @@ -0,0 +1,9 @@ +withRules([ReplaceConstantBooleanNotRector::class]); diff --git a/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php b/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php new file mode 100644 index 00000000000..9b70b143f1f --- /dev/null +++ b/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php @@ -0,0 +1,86 @@ +> + */ + public function getNodeTypes(): array + { + return [BooleanNot::class]; + } + + /** + * @param BooleanNot $node + */ + public function refactor(Node $node): ?Node + { + if (! $node instanceof BooleanNot) { + return null; + } + + if ($this->valueResolver->isFalse($node->expr)) { + return new ConstFetch(new Name('true')); + } + + if ($this->valueResolver->isTrue($node->expr)) { + return new ConstFetch(new Name('false')); + } + + return null; + } +}