From c42ae64f3feb9b60733389886b15a75ff2d2bcdb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 29 Nov 2025 09:04:25 +0700 Subject: [PATCH 1/2] [Php70] Handle return ternary on IfIssetToCoalescingRector --- .../Fixture/with_ternary_return.php.inc | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 rules-tests/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector/Fixture/with_ternary_return.php.inc diff --git a/rules-tests/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector/Fixture/with_ternary_return.php.inc b/rules-tests/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector/Fixture/with_ternary_return.php.inc new file mode 100644 index 00000000000..cd2b6ddb0cc --- /dev/null +++ b/rules-tests/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector/Fixture/with_ternary_return.php.inc @@ -0,0 +1,37 @@ +values[$param])) { + return $this->values[$param]; + } + + return $this->nullable ? null : false; + } +} + +?> +----- +values[$param] ?? ($this->nullable ? null : false); + } +} + +?> From 46220625567b6ce1469f35e943e425b1b069a0d7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 29 Nov 2025 09:05:39 +0700 Subject: [PATCH 2/2] fix --- .../StmtsAwareInterface/IfIssetToCoalescingRector.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php b/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php index 123e227699a..0a1e76a39ee 100644 --- a/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php +++ b/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php @@ -8,10 +8,12 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\Expr\Isset_; +use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Enum\NodeGroup; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -113,6 +115,10 @@ public function refactor(Node $node): ?Node unset($node->stmts[$key - 1]); + if ($stmt->expr instanceof Ternary) { + $stmt->expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); + } + $stmt->expr = new Coalesce($ifOnlyStmt->expr, $stmt->expr); return $node; }