Skip to content

Commit aeeef9c

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 066b46e + 75327d1 commit aeeef9c

10 files changed

+18
-49
lines changed

Generator/UrlGenerator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,7 @@ protected function doGenerate(array $variables, array $defaults, array $requirem
269269
}
270270

271271
// add a query string if needed
272-
$extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, function ($a, $b) {
273-
return $a == $b ? 0 : 1;
274-
});
272+
$extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, fn ($a, $b) => $a == $b ? 0 : 1);
275273

276274
array_walk_recursive($extra, $caster = static function (&$v) use (&$caster) {
277275
if (\is_object($v)) {

Loader/AnnotationDirectoryLoader.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,11 @@ public function load(mixed $path, string $type = null): ?RouteCollection
3636
$files = iterator_to_array(new \RecursiveIteratorIterator(
3737
new \RecursiveCallbackFilterIterator(
3838
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
39-
function (\SplFileInfo $current) {
40-
return !str_starts_with($current->getBasename(), '.');
41-
}
39+
fn (\SplFileInfo $current) => !str_starts_with($current->getBasename(), '.')
4240
),
4341
\RecursiveIteratorIterator::LEAVES_ONLY
4442
));
45-
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
46-
return (string) $a > (string) $b ? 1 : -1;
47-
});
43+
usort($files, fn (\SplFileInfo $a, \SplFileInfo $b) => (string) $a > (string) $b ? 1 : -1);
4844

4945
foreach ($files as $file) {
5046
if (!$file->isFile() || !str_ends_with($file->getFilename(), '.php')) {

Loader/Psr4DirectoryLoader.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,11 @@ private function loadFromDirectory(string $directory, string $psr4Prefix): Route
6666
$files = iterator_to_array(new \RecursiveIteratorIterator(
6767
new \RecursiveCallbackFilterIterator(
6868
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
69-
function (\SplFileInfo $current) {
70-
return !str_starts_with($current->getBasename(), '.');
71-
}
69+
fn (\SplFileInfo $current) => !str_starts_with($current->getBasename(), '.')
7270
),
7371
\RecursiveIteratorIterator::SELF_FIRST
7472
));
75-
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
76-
return (string) $a > (string) $b ? 1 : -1;
77-
});
73+
usort($files, fn (\SplFileInfo $a, \SplFileInfo $b) => (string) $a > (string) $b ? 1 : -1);
7874

7975
/** @var \SplFileInfo $file */
8076
foreach ($files as $file) {

Matcher/ExpressionLanguageProvider.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ public function getFunctions(): array
3636
foreach ($this->functions->getProvidedServices() as $function => $type) {
3737
$functions[] = new ExpressionFunction(
3838
$function,
39-
static function (...$args) use ($function) {
40-
return sprintf('($context->getParameter(\'_functions\')->get(%s)(%s))', var_export($function, true), implode(', ', $args));
41-
},
42-
function ($values, ...$args) use ($function) {
43-
return $values['context']->getParameter('_functions')->get($function)(...$args);
44-
}
39+
static fn (...$args) => sprintf('($context->getParameter(\'_functions\')->get(%s)(%s))', var_export($function, true), implode(', ', $args)),
40+
fn ($values, ...$args) => $values['context']->getParameter('_functions')->get($function)(...$args)
4541
);
4642
}
4743

RouteCollection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ public function all(): array
103103
if ($this->priorities) {
104104
$priorities = $this->priorities;
105105
$keysOrder = array_flip(array_keys($this->routes));
106-
uksort($this->routes, static function ($n1, $n2) use ($priorities, $keysOrder) {
107-
return (($priorities[$n2] ?? 0) <=> ($priorities[$n1] ?? 0)) ?: ($keysOrder[$n1] <=> $keysOrder[$n2]);
108-
});
106+
uksort($this->routes, static fn ($n1, $n2) => (($priorities[$n2] ?? 0) <=> ($priorities[$n1] ?? 0)) ?: ($keysOrder[$n1] <=> $keysOrder[$n2]));
109107
}
110108

111109
return $this->routes;

Tests/Fixtures/CustomXmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ class CustomXmlFileLoader extends XmlFileLoader
2121
{
2222
protected function loadFile(string $file): \DOMDocument
2323
{
24-
return XmlUtils::loadFile($file, function () { return true; });
24+
return XmlUtils::loadFile($file, fn () => true);
2525
}
2626
}

Tests/Fixtures/glob/php_dsl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
namespace Symfony\Component\Routing\Loader\Configurator;
44

5-
return function (RoutingConfigurator $routes) {
6-
return $routes->import('php_dsl_ba?.php');
7-
};
5+
return fn (RoutingConfigurator $routes) => $routes->import('php_dsl_ba?.php');

Tests/Loader/AnnotationDirectoryLoaderTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ private function expectAnnotationsToBeReadFrom(array $classes)
112112
{
113113
$this->reader->expects($this->exactly(\count($classes)))
114114
->method('getClassAnnotation')
115-
->with($this->callback(function (\ReflectionClass $class) use ($classes) {
116-
return \in_array($class->getName(), $classes);
117-
}));
115+
->with($this->callback(fn (\ReflectionClass $class) => \in_array($class->getName(), $classes)));
118116
}
119117
}

Tests/Matcher/ExpressionLanguageProviderTest.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,12 @@ class ExpressionLanguageProviderTest extends TestCase
2525
protected function setUp(): void
2626
{
2727
$functionProvider = new ServiceLocator([
28-
'env' => function () {
29-
// function with one arg
30-
return function (string $arg) {
31-
return [
32-
'APP_ENV' => 'test',
33-
'PHP_VERSION' => '7.2',
34-
][$arg] ?? null;
35-
};
36-
},
37-
'sum' => function () {
38-
// function with multiple args
39-
return function ($a, $b) { return $a + $b; };
40-
},
41-
'foo' => function () {
42-
// function with no arg
43-
return function () { return 'bar'; };
44-
},
28+
'env' => fn () => fn (string $arg) => [
29+
'APP_ENV' => 'test',
30+
'PHP_VERSION' => '7.2',
31+
][$arg] ?? null,
32+
'sum' => fn () => fn ($a, $b) => $a + $b,
33+
'foo' => fn () => fn () => 'bar',
4534
]);
4635

4736
$this->context = new RequestContext();

Tests/RouteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testDefaults()
9898
$this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
9999
$this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not set');
100100

101-
$route->setDefault('_controller', $closure = function () { return 'Hello'; });
101+
$route->setDefault('_controller', $closure = fn () => 'Hello');
102102
$this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
103103

104104
$route->setDefaults(['foo' => 'foo']);

0 commit comments

Comments
 (0)