77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
99use PhpParser \Node \Expr \ArrayDimFetch ;
10+ use PhpParser \Node \Expr \ArrowFunction ;
1011use PhpParser \Node \Expr \Assign ;
1112use PhpParser \Node \Expr \BinaryOp \Coalesce ;
1213use PhpParser \Node \Expr \BinaryOp \Identical ;
1314use PhpParser \Node \Expr \BinaryOp \NotIdentical ;
1415use PhpParser \Node \Expr \BooleanNot ;
16+ use PhpParser \Node \Expr \Closure ;
1517use PhpParser \Node \Expr \ConstFetch ;
1618use PhpParser \Node \Expr \Isset_ ;
1719use PhpParser \Node \Expr \PropertyFetch ;
2527use PhpParser \Node \Stmt \Return_ ;
2628use Rector \NodeAnalyzer \CoalesceAnalyzer ;
2729use Rector \NodeManipulator \BinaryOpManipulator ;
30+ use Rector \Php72 \NodeFactory \AnonymousFunctionFactory ;
2831use Rector \PhpParser \Node \BetterNodeFinder ;
2932use Rector \Rector \AbstractRector ;
3033use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
@@ -41,6 +44,7 @@ public function __construct(
4144 private readonly CoalesceAnalyzer $ coalesceAnalyzer ,
4245 private readonly BinaryOpManipulator $ binaryOpManipulator ,
4346 private readonly BetterNodeFinder $ betterNodeFinder ,
47+ private readonly AnonymousFunctionFactory $ anonymousFunctionFactory
4448 ) {
4549 }
4650
@@ -69,15 +73,19 @@ public function getRuleDefinition(): RuleDefinition
6973 */
7074 public function getNodeTypes (): array
7175 {
72- return [Expression::class, Return_::class];
76+ return [ArrowFunction::class, Expression::class, Return_::class];
7377 }
7478
7579 /**
76- * @param Expression|Return_ $node
80+ * @param ArrowFunction| Expression|Return_ $node
7781 * @return Node|Node[]|null
7882 */
7983 public function refactor (Node $ node ): Node |array |null
8084 {
85+ if ($ node instanceof ArrowFunction) {
86+ return $ this ->refactorArrowFunctionReturn ($ node );
87+ }
88+
8189 if ($ node instanceof Return_) {
8290 return $ this ->refactorReturn ($ node );
8391 }
@@ -104,6 +112,21 @@ public function refactor(Node $node): Node|array|null
104112 return $ this ->refactorDirectCoalesce ($ node );
105113 }
106114
115+ private function refactorArrowFunctionReturn (ArrowFunction $ arrowFunction ): ?Closure
116+ {
117+ if (! $ arrowFunction ->expr instanceof Throw_) {
118+ return null ;
119+ }
120+
121+ $ stmts = [new Expression ($ arrowFunction ->expr )];
122+ return $ this ->anonymousFunctionFactory ->create (
123+ $ arrowFunction ->params ,
124+ $ stmts ,
125+ $ arrowFunction ->returnType ,
126+ $ arrowFunction ->static
127+ );
128+ }
129+
107130 /**
108131 * @return If_|Expression|Stmt[]|null
109132 */
0 commit comments