File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
rules-tests/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector/Fixture
rules/Php70/Rector/StmtsAwareInterface Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \Php70 \Rector \StmtsAwareInterface \IfIssetToCoalescingRector \Fixture ;
4+
5+ class WithTernaryReturn
6+ {
7+ private $ values = [];
8+ private $ nullable = false ;
9+
10+ public function resolve ($ param )
11+ {
12+ if (isset ($ this ->values [$ param ])) {
13+ return $ this ->values [$ param ];
14+ }
15+
16+ return $ this ->nullable ? null : false ;
17+ }
18+ }
19+
20+ ?>
21+ -----
22+ <?php
23+
24+ namespace Rector \Tests \Php70 \Rector \StmtsAwareInterface \IfIssetToCoalescingRector \Fixture ;
25+
26+ class WithTernaryReturn
27+ {
28+ private $ values = [];
29+ private $ nullable = false ;
30+
31+ public function resolve ($ param )
32+ {
33+ return $ this ->values [$ param ] ?? ($ this ->nullable ? null : false );
34+ }
35+ }
36+
37+ ?>
Original file line number Diff line number Diff line change 88use PhpParser \Node \Expr ;
99use PhpParser \Node \Expr \BinaryOp \Coalesce ;
1010use PhpParser \Node \Expr \Isset_ ;
11+ use PhpParser \Node \Expr \Ternary ;
1112use PhpParser \Node \Stmt ;
1213use PhpParser \Node \Stmt \Else_ ;
1314use PhpParser \Node \Stmt \If_ ;
1415use PhpParser \Node \Stmt \Return_ ;
16+ use Rector \NodeTypeResolver \Node \AttributeKey ;
1517use Rector \PhpParser \Enum \NodeGroup ;
1618use Rector \Rector \AbstractRector ;
1719use Rector \ValueObject \PhpVersionFeature ;
@@ -113,6 +115,10 @@ public function refactor(Node $node): ?Node
113115
114116 unset($ node ->stmts [$ key - 1 ]);
115117
118+ if ($ stmt ->expr instanceof Ternary) {
119+ $ stmt ->expr ->setAttribute (AttributeKey::WRAPPED_IN_PARENTHESES , true );
120+ }
121+
116122 $ stmt ->expr = new Coalesce ($ ifOnlyStmt ->expr , $ stmt ->expr );
117123 return $ node ;
118124 }
You can’t perform that action at this time.
0 commit comments