1212use PhpParser \Node \Expr \BinaryOp \Coalesce ;
1313use PhpParser \Node \Expr \BinaryOp \Identical ;
1414use PhpParser \Node \Expr \BooleanNot ;
15+ use PhpParser \Node \Expr \CallLike ;
1516use PhpParser \Node \Expr \Closure ;
1617use PhpParser \Node \Expr \ConstFetch ;
1718use PhpParser \Node \Expr \Isset_ ;
2526use PhpParser \Node \Stmt \If_ ;
2627use PhpParser \Node \Stmt \Return_ ;
2728use Rector \NodeAnalyzer \CoalesceAnalyzer ;
29+ use Rector \NodeFactory \NamedVariableFactory ;
2830use Rector \NodeManipulator \BinaryOpManipulator ;
2931use Rector \Php72 \NodeFactory \AnonymousFunctionFactory ;
3032use Rector \PhpParser \Node \BetterNodeFinder ;
@@ -43,7 +45,8 @@ public function __construct(
4345 private readonly CoalesceAnalyzer $ coalesceAnalyzer ,
4446 private readonly BinaryOpManipulator $ binaryOpManipulator ,
4547 private readonly BetterNodeFinder $ betterNodeFinder ,
46- private readonly AnonymousFunctionFactory $ anonymousFunctionFactory
48+ private readonly AnonymousFunctionFactory $ anonymousFunctionFactory ,
49+ private readonly NamedVariableFactory $ namedVariableFactory
4750 ) {
4851 }
4952
@@ -100,6 +103,22 @@ public function refactor(Node $node): Node|array|null
100103 }
101104 }
102105
106+ if ($ node ->expr instanceof CallLike && ! $ node ->expr ->isFirstClassCallable ()) {
107+ $ args = $ node ->expr ->getArgs ();
108+ foreach ($ args as $ arg ) {
109+ if ($ arg ->value instanceof Ternary && $ arg ->value ->else instanceof Throw_) {
110+ $ refactorTernary = $ this ->refactorTernary ($ arg ->value , null , true );
111+ if (is_array ($ refactorTernary ) && $ refactorTernary [0 ] instanceof Expression && $ refactorTernary [0 ]->expr instanceof Assign) {
112+ $ arg ->value = $ refactorTernary [0 ]->expr ->var ;
113+
114+ return [...$ refactorTernary , $ node ];
115+ }
116+ }
117+ }
118+
119+ return null ;
120+ }
121+
103122 if ($ node ->expr instanceof Coalesce) {
104123 return $ this ->refactorCoalesce ($ node ->expr , null );
105124 }
@@ -153,18 +172,36 @@ private function refactorAssign(Assign $assign): If_ | Expression | null | array
153172 /**
154173 * @return If_|Stmt[]|null
155174 */
156- private function refactorTernary (Ternary $ ternary , ?Assign $ assign ): If_ |null |array
175+ private function refactorTernary (Ternary $ ternary , ?Assign $ assign, bool $ onArg = false ): If_ |null |array
157176 {
158- if (! $ ternary ->else instanceof Throw_) {
177+ if ($ ternary ->if instanceof Throw_) {
178+ $ else = $ ternary ->if ;
179+ } elseif ($ ternary ->else instanceof Throw_) {
180+ $ else = $ ternary ->else ;
181+ } else {
159182 return null ;
160183 }
161184
162- $ inversedTernaryExpr = $ this ->binaryOpManipulator ->inverseNode ($ ternary ->cond );
185+ $ inversedTernaryExpr = $ ternary ->if instanceof Throw_
186+ ? $ ternary ->cond
187+ : $ this ->binaryOpManipulator ->inverseNode ($ ternary ->cond );
163188
164189 $ if = new If_ ($ inversedTernaryExpr , [
165- 'stmts ' => [new Expression ($ ternary -> else )],
190+ 'stmts ' => [new Expression ($ else )],
166191 ]);
167192
193+ if (! $ assign instanceof Assign && $ onArg ) {
194+ $ tempVar = $ this ->namedVariableFactory ->createVariable ('arg ' , $ ternary );
195+ $ assign = new Assign ($ tempVar , $ ternary ->if ?? $ ternary ->cond );
196+
197+ $ inversedTernaryExpr = $ this ->binaryOpManipulator ->inverseNode ($ tempVar );
198+ $ if = new If_ ($ inversedTernaryExpr , [
199+ 'stmts ' => [new Expression ($ else )],
200+ ]);
201+
202+ return [new Expression ($ assign ), $ if ];
203+ }
204+
168205 if (! $ assign instanceof Assign) {
169206 return $ if ;
170207 }
@@ -267,7 +304,11 @@ private function refactorReturn(Return_ $return): ?array
267304 return null ;
268305 }
269306
270- return [$ if , new Return_ ($ return ->expr ->cond )];
307+ $ returnExpr = $ return ->expr ->if instanceof Throw_
308+ ? $ return ->expr ->else
309+ : ($ return ->expr ->if ?? $ return ->expr ->cond );
310+
311+ return [$ if , new Return_ ($ returnExpr )];
271312 }
272313
273314 return null ;
0 commit comments