Skip to content

[CodeQuality] Add NegatedAndsToPositiveOrsRector#8082

Merged
TomasVotruba merged 2 commits into
mainfrom
tv-morgan
Jun 24, 2026
Merged

[CodeQuality] Add NegatedAndsToPositiveOrsRector#8082
TomasVotruba merged 2 commits into
mainfrom
tv-morgan

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jun 24, 2026

Copy link
Copy Markdown
Member

Inspiration from: mautic/mautic@657e1df#r3468116767

What

Adds a new rule NegatedAndsToPositiveOrsRector that applies De Morgan to a negated AND: !(A && B)!A || !B.

This is the missing counterpart to the existing SimplifyDeMorganBinaryRector, which only handles a negated OR (!(A || B)!A && !B). The two directions are kept as separate, focused rules.

Examples

Real-world case (empty() + in_array()):

-return !(!empty($limitToAssets) && !in_array($assetId, $limitToAssets));
+return empty($limitToAssets) || in_array($assetId, $limitToAssets);

Comparison operators:

 $a = 5;
 $b = 10;
-$result = !($a > 20 && $b <= 50);
+$result = $a <= 20 || $b > 50;

What changed

  • New rule rules/CodeQuality/Rector/BooleanNot/NegatedAndsToPositiveOrsRector.php — subscribes to BooleanNot, transforms a wrapped BooleanAnd into a BooleanOr of the inverted operands.
  • BinaryOpManipulator::inverseBooleanAnd() — new helper mirroring inverseBooleanOr(); inverts each operand and joins with ||. Skips nested && to avoid partial rewrites. The shared resolveInversedNodeClass() is intentionally untouched, since invertCondition() relies on it keeping operands in place.
  • Registered in CodeQualityLevel::RULES.
  • Tests under rules-tests/CodeQuality/Rector/BooleanNot/NegatedAndsToPositiveOrsRector/ — covers the empty/in_array case, comparison operators, and skip cases (negated OR, nested AND).

SimplifyDeMorganBinaryRector is left unchanged.

@TomasVotruba TomasVotruba changed the title include negated nested in Demorgan binary [CodeQuality] Add NegatedAndsToPositiveOrsRector Jun 24, 2026
@TomasVotruba TomasVotruba merged commit 18bab94 into main Jun 24, 2026
67 checks passed
@TomasVotruba TomasVotruba deleted the tv-morgan branch June 24, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant