Skip to content

Commit

Permalink
[DowngradePhp74] handle static arrow functions to static closures (#1036
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Lctrs committed Oct 21, 2021
1 parent 1f2a4e9 commit 831aca4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector\Fixture;

final class StaticFn
{
public function run()
{
$callable = static fn () => true;
}
}
?>
-----
<?php

namespace Rector\Tests\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector\Fixture;

final class StaticFn
{
public function run()
{
$callable = static function () {
return true;
};
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ public function refactor(Node $node): Closure
{
$stmts = [new Return_($node->expr)];

return $this->anonymousFunctionFactory->create($node->params, $stmts, $node->returnType);
return $this->anonymousFunctionFactory->create($node->params, $stmts, $node->returnType, $node->static);
}
}
7 changes: 6 additions & 1 deletion rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ public function __construct(
public function create(
array $params,
array $stmts,
Identifier | Name | NullableType | UnionType | ComplexType | null $returnTypeNode
Identifier | Name | NullableType | UnionType | ComplexType | null $returnTypeNode,
bool $static = false
): Closure {
$useVariables = $this->createUseVariablesFromParams($stmts, $params);
$anonymousFunctionNode = new Closure();
$anonymousFunctionNode->params = $params;
if ($static) {
$anonymousFunctionNode->static = $static;
}
foreach ($useVariables as $useVariable) {
$anonymousFunctionNode->uses[] = new ClosureUse($useVariable);
}
Expand Down

0 comments on commit 831aca4

Please sign in to comment.