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); + } +} + +?> 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; }