Skip to content

[CodeQuality] Deprecate SwitchNegatedTernaryRector - #8283

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-switch-negated-ternary
Aug 4, 2026
Merged

[CodeQuality] Deprecate SwitchNegatedTernaryRector#8283
TomasVotruba merged 1 commit into
mainfrom
deprecate-switch-negated-ternary

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Deprecates SwitchNegatedTernaryRector.

The rule flips ! $cond ? A : B into $cond ? B : A. The condition gets positive, but the branch values swap places — and that ordering often carried meaning.

Simple case is fine:

-return ! $upper
-    ? $name
-    : strtoupper($name);
+return $upper
+    ? strtoupper($name)
+    : $name;

Nested case is where it turns:

-return ! isset($result['validation_messages'])
-    ? []
-    : (isset($result['validation_messages']['http']) ? $result['validation_messages'] : $result);
+return isset($result['validation_messages'])
+    ? (isset($result['validation_messages']['http']) ? $result['validation_messages'] : $result)
+    : ([]);

The original reads guard-first: bail value [] up top, complex branch last. After the rule, the nested ternary is jammed into the ? branch and the trivial fallback sits at the bottom. Also note ([]) — parentheses added around a plain array literal.

Second fixture had the same artifact:

-return ! isset($a)
-    ? (isset($b) ? $b : 'b')
-    : $a;
+return isset($a)
+    ? ($a)
+    : (isset($b) ? $b : 'b');

($a) — parentheses around a bare variable.

Which form reads better depends on what the values mean, not on the shape of the condition. That is a personal preference, so the rule is deprecated rather than patched.

Changes:

  • SwitchNegatedTernaryRector implements DeprecatedInterface, refactor() throws
  • removed from CodeQualityLevel
  • rule tests and tests/Issues/Issue6480 (which existed only to cover this rule's interplay with RemoveDeadInstanceOfRector) removed
  • dropped the now-stale pointer to this rule from RemoveUselessTernaryRector

@TomasVotruba
TomasVotruba merged commit 1b106d2 into main Aug 4, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-switch-negated-ternary branch August 4, 2026 08:56
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