Skip to content

feat: add support for converting is_null ternaries to null coalescing operator#7890

Open
Soean wants to merge 3 commits intorectorphp:mainfrom
Soean:9648
Open

feat: add support for converting is_null ternaries to null coalescing operator#7890
Soean wants to merge 3 commits intorectorphp:mainfrom
Soean:9648

Conversation

@Soean
Copy link

@Soean Soean commented Feb 13, 2026

This pull request extends the TernaryToNullCoalescingRector to support converting ternary expressions using is_null and its negation to the null coalescing operator (??).

Fixes: rectorphp/rector#9648

@samsonasik
Copy link
Member

This can wait for PR:

to be merged first and need sync to add check parentheses fixture on the right side of ternary.

@samsonasik
Copy link
Member

samsonasik commented Feb 13, 2026

Please rebase latest main branch, add fixture like in PR:

and verify enhancement like in there to cover parentheses in the else/right part.

if ($ternary->else instanceof Ternary && $this->isTernaryParenthesized($this->file, $ternary->cond, $ternary)) {
     $ternary->else->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}

Thank you.

@samsonasik
Copy link
Member

samsonasik commented Feb 14, 2026

Could you add additional fixture with parentheses in ternary->else which ternary->else itself a ternary with parentheses like in PR:

Then apply the if patch if needed.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support is_null() in TernaryToNullCoalescingRector

2 participants