From 8c97f9fbb4bac3ac4e9cdb3e691f890522d28bcb Mon Sep 17 00:00:00 2001 From: John Shaffer Date: Tue, 21 Oct 2025 09:07:32 -0500 Subject: [PATCH 1/2] [code-quality] Add ReplaceConstantBooleanNotRector --- .../Fixture/2_not_false.php.inc | 11 +++ .../Fixture/2_not_true.php.inc | 11 +++ .../Fixture/3_not_false.php.inc | 11 +++ .../Fixture/3_not_true.php.inc | 11 +++ .../Fixture/if_condition.php.inc | 33 +++++++ .../Fixture/not_false.php.inc | 11 +++ .../Fixture/not_true.php.inc | 11 +++ .../ReplaceConstantBooleanNotRectorTest.php | 28 ++++++ .../config/configured_rule.php | 9 ++ .../ReplaceConstantBooleanNotRector.php | 86 +++++++++++++++++++ 10 files changed, 222 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_false.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/2_not_true.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_false.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/3_not_true.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/if_condition.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_false.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/Fixture/not_true.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/ReplaceConstantBooleanNotRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php 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..2ad1b098ea5 --- /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 (! $node->expr instanceof ConstFetch) { + return null; + } + + $constantName = $this->getName($node->expr); + + if ($constantName === 'false') { + return new ConstFetch(new Name('true')); + } + + if ($constantName === 'true') { + return new ConstFetch(new Name('false')); + } + + return null; + } +} From cff6994fc73bf0b0dd0e7fa9e8eda27f61b29967 Mon Sep 17 00:00:00 2001 From: John Shaffer Date: Tue, 21 Oct 2025 09:42:08 -0500 Subject: [PATCH 2/2] Change constant lookups to valueResolver methods --- .../ReplaceConstantBooleanNotRector.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php b/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php index 2ad1b098ea5..9b70b143f1f 100644 --- a/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php +++ b/rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Name; +use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -19,6 +20,11 @@ */ final class ReplaceConstantBooleanNotRector extends AbstractRector { + public function __construct( + private readonly ValueResolver $valueResolver + ) { + } + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -67,17 +73,11 @@ public function refactor(Node $node): ?Node return null; } - if (! $node->expr instanceof ConstFetch) { - return null; - } - - $constantName = $this->getName($node->expr); - - if ($constantName === 'false') { + if ($this->valueResolver->isFalse($node->expr)) { return new ConstFetch(new Name('true')); } - if ($constantName === 'true') { + if ($this->valueResolver->isTrue($node->expr)) { return new ConstFetch(new Name('false')); }