Skip to content

[deprecation] Deprecate CoalesceToTernaryRector as risky - #8324

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-coalesce-to-ternary-rector
Aug 8, 2026
Merged

[deprecation] Deprecate CoalesceToTernaryRector as risky#8324
TomasVotruba merged 1 commit into
mainfrom
deprecate-coalesce-to-ternary-rector

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

CoalesceToTernaryRector turns ?? into ?: when the left side is non-nullable. But those operators are not interchangeable: ?? only falls back on null, while ?: falls back on every falsy value.

 function run(string $a)
 {
-    return $a ?? 'foo';
+    return $a ?: 'foo';
 }

 run(''); // before: "", after: "foo"
 run('0'); // before: "0", after: "foo"

The same applies to 0, 0.0, [] and false. The rule is type-safe on paper, but the value change is silent and only shows up at runtime, so a regression has to be found and fixed manually.

Deprecated the same way as SwitchNegatedTernaryRector: implements DeprecatedInterface, refactor() throws, tests removed. The rule is not part of any set, so no set list changes.

The "??" and "?:" operators are not interchangeable. "?:" also falls back on falsy values like empty string, "0" and empty array, so the change can silently alter behavior. A regression has to be fixed manually, so the rule is deprecated instead.
@TomasVotruba
TomasVotruba merged commit 11c5274 into main Aug 8, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-coalesce-to-ternary-rector branch August 8, 2026 22:43
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