Skip to content

Commit 3a48a6d

Browse files
minor #48793 Leverage arrow function syntax for closure (tigitz)
This PR was merged into the 6.3 branch. Discussion ---------- Leverage arrow function syntax for closure | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #47658 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | <!-- required for new features --> Rationale in the RFC [here](https://wiki.php.net/rfc/arrow_functions_v2#introduction) It's also notable that using arrow function syntax rather than the classic one has been enforced in the past by symfony core member: symfony/symfony#48069 (comment) So this PR would be consistent. Commits ------- f5802d3a2a Leverage arrow function syntax for closure
2 parents 9022092 + 2c84bf5 commit 3a48a6d

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

ExpressionFunction.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,9 @@ public static function fromPhp(string $phpFunctionName, string $expressionFuncti
8282
throw new \InvalidArgumentException(sprintf('An expression function name must be defined when PHP function "%s" is namespaced.', $phpFunctionName));
8383
}
8484

85-
$compiler = function (...$args) use ($phpFunctionName) {
86-
return sprintf('\%s(%s)', $phpFunctionName, implode(', ', $args));
87-
};
85+
$compiler = fn (...$args) => sprintf('\%s(%s)', $phpFunctionName, implode(', ', $args));
8886

89-
$evaluator = function ($p, ...$args) use ($phpFunctionName) {
90-
return $phpFunctionName(...$args);
91-
};
87+
$evaluator = fn ($p, ...$args) => $phpFunctionName(...$args);
9288

9389
return new self($expressionFunctionName ?: end($parts), $compiler, $evaluator);
9490
}

Tests/Fixtures/TestProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ class TestProvider implements ExpressionFunctionProviderInterface
1919
public function getFunctions(): array
2020
{
2121
return [
22-
new ExpressionFunction('identity', function ($input) {
23-
return $input;
24-
}, function (array $values, $input) {
25-
return $input;
26-
}),
22+
new ExpressionFunction('identity', fn ($input) => $input, fn (array $values, $input) => $input),
2723

2824
ExpressionFunction::fromPhp('strtoupper'),
2925

Tests/Node/FunctionNodeTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ public function getDumpData()
4141
protected function getCallables()
4242
{
4343
return [
44-
'compiler' => function ($arg) {
45-
return sprintf('foo(%s)', $arg);
46-
},
47-
'evaluator' => function ($variables, $arg) {
48-
return $arg;
49-
},
44+
'compiler' => fn ($arg) => sprintf('foo(%s)', $arg),
45+
'evaluator' => fn ($variables, $arg) => $arg,
5046
];
5147
}
5248
}

0 commit comments

Comments
 (0)