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

[DowngradePhp74] handle static arrow functions to static closures #1036

Merged
merged 1 commit into from
Oct 21, 2021
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,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