Skip to content

Incorrect behavior of TernaryToNullCoalescingRector #9649

@hogejiro

Description

@hogejiro

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/8ac3d4d0-4865-4c55-a137-c593363911d4

<?php
$port = '8080';
$scheme = 'https';
$port = isset($port) ? $port : (($scheme == 'https') ? '443' : '80');

Responsible rules

  • TernaryToNullCoalescingRector

Expected Behavior

  • The parentheses around the nested ternary should be preserved when converting to null coalescing operator:
-$port = isset($port) ? $port : (($scheme == 'https') ? '443' : '80');
+$port = $port ?? (($scheme == 'https') ? '443' : '80');

Note

  • This worked correctly in Rector 1.x, where the parentheses were properly preserved during the transformation.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions