Skip to content

Commit 829f969

Browse files
authored
Fix downgrade throw on ArrowFunction return (#238)
* Fix downgrade throw on ArrowFunction return * Fix downgrade throw on ArrowFunction return
1 parent 1d5cf1e commit 829f969

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
4+
5+
class FnReturnThrow
6+
{
7+
public function run()
8+
{
9+
$line = Callback::invokeSafe('fgets', [$f], fn($error) => throw new Nette\IOException(\sprintf("Unable to read file '%s'. %s", self::normalizePath($file), $error)));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
18+
19+
class FnReturnThrow
20+
{
21+
public function run()
22+
{
23+
$line = Callback::invokeSafe('fgets', [$f], function ($error) use ($file) {
24+
throw new Nette\IOException(\sprintf("Unable to read file '%s'. %s", self::normalizePath($file), $error));
25+
});
26+
}
27+
}
28+
29+
?>

rules/DowngradePhp80/Rector/Expression/DowngradeThrowExprRector.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\ArrayDimFetch;
10+
use PhpParser\Node\Expr\ArrowFunction;
1011
use PhpParser\Node\Expr\Assign;
1112
use PhpParser\Node\Expr\BinaryOp\Coalesce;
1213
use PhpParser\Node\Expr\BinaryOp\Identical;
1314
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1415
use PhpParser\Node\Expr\BooleanNot;
16+
use PhpParser\Node\Expr\Closure;
1517
use PhpParser\Node\Expr\ConstFetch;
1618
use PhpParser\Node\Expr\Isset_;
1719
use PhpParser\Node\Expr\PropertyFetch;
@@ -25,6 +27,7 @@
2527
use PhpParser\Node\Stmt\Return_;
2628
use Rector\NodeAnalyzer\CoalesceAnalyzer;
2729
use Rector\NodeManipulator\BinaryOpManipulator;
30+
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
2831
use Rector\PhpParser\Node\BetterNodeFinder;
2932
use Rector\Rector\AbstractRector;
3033
use 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

Comments
 (0)