Skip to content

Commit 56d70c6

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 f05704a + ab70fbb commit 56d70c6

File tree

7 files changed

+11
-29
lines changed

7 files changed

+11
-29
lines changed

Command/DebugCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ private function displayPathsText(SymfonyStyle $io, string $name)
162162
[$namespace, $shortname] = $this->parseTemplateName($name);
163163
$alternatives = $this->findAlternatives($shortname, $shortnames);
164164
if (FilesystemLoader::MAIN_NAMESPACE !== $namespace) {
165-
$alternatives = array_map(function ($shortname) use ($namespace) {
166-
return '@'.$namespace.'/'.$shortname;
167-
}, $alternatives);
165+
$alternatives = array_map(fn ($shortname) => '@'.$namespace.'/'.$shortname, $alternatives);
168166
}
169167
}
170168

@@ -543,7 +541,7 @@ private function findAlternatives(string $name, array $collection): array
543541
}
544542

545543
$threshold = 1e3;
546-
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
544+
$alternatives = array_filter($alternatives, fn ($lev) => $lev < 2 * $threshold);
547545
ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE);
548546

549547
return array_keys($alternatives);

Extension/CodeExtension.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?stri
120120
// remove main code/span tags
121121
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
122122
// split multiline spans
123-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
124-
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
125-
}, $code);
123+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn ($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
126124
$content = explode('<br />', $code);
127125

128126
$lines = [];
@@ -188,9 +186,7 @@ public function getFileRelative(string $file): ?string
188186

189187
public function formatFileFromText(string $text): string
190188
{
191-
return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
192-
return 'in '.$this->formatFile($match[2], $match[3]);
193-
}, $text);
189+
return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', fn ($match) => 'in '.$this->formatFile($match[2], $match[3]), $text);
194190
}
195191

196192
/**

Tests/Command/LintCommandTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ private function createCommandTester(): CommandTester
159159
private function createCommand(): Command
160160
{
161161
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
162-
$environment->addFilter(new TwigFilter('deprecated_filter', function ($v) {
163-
return $v;
164-
}, ['deprecated' => true]));
162+
$environment->addFilter(new TwigFilter('deprecated_filter', fn ($v) => $v, ['deprecated' => true]));
165163

166164
$command = new LintCommand($environment);
167165

Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,9 +1039,7 @@ public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
10391039
{
10401040
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
10411041
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
1042-
'choice_label' => function () {
1043-
return false;
1044-
},
1042+
'choice_label' => fn () => false,
10451043
'multiple' => false,
10461044
'expanded' => true,
10471045
]);
@@ -1404,9 +1402,7 @@ public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
14041402
{
14051403
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', ['&a'], [
14061404
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
1407-
'choice_label' => function () {
1408-
return false;
1409-
},
1405+
'choice_label' => fn () => false,
14101406
'multiple' => true,
14111407
'expanded' => true,
14121408
]);

Tests/Extension/AbstractBootstrap4LayoutTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,7 @@ public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
580580
{
581581
$form = $this->factory->createNamed('name', ChoiceType::class, '&a', [
582582
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
583-
'choice_label' => function () {
584-
return false;
585-
},
583+
'choice_label' => fn () => false,
586584
'multiple' => false,
587585
'expanded' => true,
588586
]);
@@ -901,9 +899,7 @@ public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
901899
{
902900
$form = $this->factory->createNamed('name', ChoiceType::class, ['&a'], [
903901
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
904-
'choice_label' => function () {
905-
return false;
906-
},
902+
'choice_label' => fn () => false,
907903
'multiple' => true,
908904
'expanded' => true,
909905
]);

Tests/Mime/BodyRendererTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ public function testRenderedOnceUnserializableContext()
124124
;
125125
$email->textTemplate('text');
126126
$email->context([
127-
'foo' => static function () {
128-
return 'bar';
129-
},
127+
'foo' => static fn () => 'bar',
130128
]);
131129

132130
$renderer->render($email);

UndefinedCallableHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static function onUndefinedFunction(string $name): TwigFunction|false
8787
}
8888

8989
if ('webpack-encore-bundle' === self::FUNCTION_COMPONENTS[$name]) {
90-
return new TwigFunction($name, static function () { return ''; });
90+
return new TwigFunction($name, static fn () => '');
9191
}
9292

9393
throw new SyntaxError(self::onUndefined($name, 'function', self::FUNCTION_COMPONENTS[$name]));

0 commit comments

Comments
 (0)