Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix downgrade throw on ArrowFunction return #238

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;

class FnReturnThrow
{
public function run()
{
$line = Callback::invokeSafe('fgets', [$f], fn($error) => throw new Nette\IOException(\sprintf("Unable to read file '%s'. %s", self::normalizePath($file), $error)));
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;

class FnReturnThrow
{
public function run()
{
$line = Callback::invokeSafe('fgets', [$f], function ($error) use ($file) {
throw new Nette\IOException(\sprintf("Unable to read file '%s'. %s", self::normalizePath($file), $error));
});
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
Expand All @@ -25,6 +27,7 @@
use PhpParser\Node\Stmt\Return_;
use Rector\NodeAnalyzer\CoalesceAnalyzer;
use Rector\NodeManipulator\BinaryOpManipulator;
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -41,6 +44,7 @@ public function __construct(
private readonly CoalesceAnalyzer $coalesceAnalyzer,
private readonly BinaryOpManipulator $binaryOpManipulator,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly AnonymousFunctionFactory $anonymousFunctionFactory
) {
}

Expand Down Expand Up @@ -69,15 +73,19 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [Expression::class, Return_::class];
return [ArrowFunction::class, Expression::class, Return_::class];
}

/**
* @param Expression|Return_ $node
* @param ArrowFunction|Expression|Return_ $node
* @return Node|Node[]|null
*/
public function refactor(Node $node): Node|array|null
{
if ($node instanceof ArrowFunction) {
return $this->refactorArrowFunctionReturn($node);
}

if ($node instanceof Return_) {
return $this->refactorReturn($node);
}
Expand All @@ -104,6 +112,21 @@ public function refactor(Node $node): Node|array|null
return $this->refactorDirectCoalesce($node);
}

private function refactorArrowFunctionReturn(ArrowFunction $arrowFunction): ?Closure
{
if (! $arrowFunction->expr instanceof Throw_) {
return null;
}

$stmts = [new Expression($arrowFunction->expr)];
return $this->anonymousFunctionFactory->create(
$arrowFunction->params,
$stmts,
$arrowFunction->returnType,
$arrowFunction->static
);
}

/**
* @return If_|Expression|Stmt[]|null
*/
Expand Down
Loading