Skip to content

Commit f5802d3

Browse files
tigitznicolas-grekas
authored andcommitted
Leverage arrow function syntax for closure
1 parent e9870a4 commit f5802d3

File tree

413 files changed

+1100
-2337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

413 files changed

+1100
-2337
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,12 @@ public function getEntitiesByIds(string $identifier, array $values): array
7474

7575
// Filter out non-integer values (e.g. ""). If we don't, some
7676
// databases such as PostgreSQL fail.
77-
$values = array_values(array_filter($values, function ($v) {
78-
return (string) $v === (string) (int) $v || ctype_digit($v);
79-
}));
77+
$values = array_values(array_filter($values, fn ($v) => (string) $v === (string) (int) $v || ctype_digit($v)));
8078
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) {
8179
$parameterType = Connection::PARAM_STR_ARRAY;
8280

8381
// Like above, but we just filter out empty strings.
84-
$values = array_values(array_filter($values, function ($v) {
85-
return '' !== (string) $v;
86-
}));
82+
$values = array_values(array_filter($values, fn ($v) => '' !== (string) $v));
8783

8884
// Convert values into right type
8985
if (Type::hasType($type)) {

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,13 @@ public function configureOptions(OptionsResolver $resolver)
193193

194194
// Set the "id_reader" option via the normalizer. This option is not
195195
// supposed to be set by the user.
196-
$idReaderNormalizer = function (Options $options) {
197-
// The ID reader is a utility that is needed to read the object IDs
198-
// when generating the field values. The callback generating the
199-
// field values has no access to the object manager or the class
200-
// of the field, so we store that information in the reader.
201-
// The reader is cached so that two choice lists for the same class
202-
// (and hence with the same reader) can successfully be cached.
203-
return $this->getCachedIdReader($options['em'], $options['class']);
204-
};
196+
// The ID reader is a utility that is needed to read the object IDs
197+
// when generating the field values. The callback generating the
198+
// field values has no access to the object manager or the class
199+
// of the field, so we store that information in the reader.
200+
// The reader is cached so that two choice lists for the same class
201+
// (and hence with the same reader) can successfully be cached.
202+
$idReaderNormalizer = fn (Options $options) => $this->getCachedIdReader($options['em'], $options['class']);
205203

206204
$resolver->setDefaults([
207205
'em' => null,

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function getProperties(string $class, array $context = []): ?array
4747
$properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
4848

4949
if ($metadata instanceof ClassMetadataInfo && class_exists(Embedded::class) && $metadata->embeddedClasses) {
50-
$properties = array_filter($properties, function ($property) {
51-
return !str_contains($property, '.');
52-
});
50+
$properties = array_filter($properties, fn ($property) => !str_contains($property, '.'));
5351

5452
$properties = array_merge($properties, array_keys($metadata->embeddedClasses));
5553
}

src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/DoctrineExtensionTest.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ protected function setUp(): void
4747

4848
$this->extension->expects($this->any())
4949
->method('getObjectManagerElementName')
50-
->willReturnCallback(function ($name) {
51-
return 'doctrine.orm.'.$name;
52-
});
50+
->willReturnCallback(fn ($name) => 'doctrine.orm.'.$name);
5351

5452
$this->extension
5553
->method('getMappingObjectDefaultName')

src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/DoctrineChoiceLoaderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntIdAndValueGiven()
222222
);
223223

224224
$choices = [$this->obj1, $this->obj2, $this->obj3];
225-
$value = function (\stdClass $object) { return $object->name; };
225+
$value = fn (\stdClass $object) => $object->name;
226226

227227
$this->repository->expects($this->never())
228228
->method('findAll')
@@ -367,7 +367,7 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
367367
);
368368

369369
$choices = [$this->obj1, $this->obj2, $this->obj3];
370-
$value = function (\stdClass $object) { return $object->name; };
370+
$value = fn (\stdClass $object) => $object->name;
371371

372372
$this->repository->expects($this->once())
373373
->method('findAll')

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

+10-26
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
236236
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
237237
'em' => 'default',
238238
'class' => self::SINGLE_IDENT_CLASS,
239-
'query_builder' => function () {
240-
return new \stdClass();
241-
},
239+
'query_builder' => fn () => new \stdClass(),
242240
]);
243241

244242
$field->submit('2');
@@ -1078,10 +1076,8 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingle
10781076
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
10791077
'em' => 'default',
10801078
'class' => self::SINGLE_IDENT_CLASS,
1081-
'query_builder' => function (EntityRepository $repository) {
1082-
return $repository->createQueryBuilder('e')
1083-
->where('e.id IN (1, 2)');
1084-
},
1079+
'query_builder' => fn (EntityRepository $repository) => $repository->createQueryBuilder('e')
1080+
->where('e.id IN (1, 2)'),
10851081
'choice_label' => 'name',
10861082
]);
10871083

@@ -1102,10 +1098,8 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompos
11021098
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
11031099
'em' => 'default',
11041100
'class' => self::COMPOSITE_IDENT_CLASS,
1105-
'query_builder' => function (EntityRepository $repository) {
1106-
return $repository->createQueryBuilder('e')
1107-
->where('e.id1 IN (10, 50)');
1108-
},
1101+
'query_builder' => fn (EntityRepository $repository) => $repository->createQueryBuilder('e')
1102+
->where('e.id1 IN (10, 50)'),
11091103
'choice_label' => 'name',
11101104
]);
11111105

@@ -1220,17 +1214,13 @@ public function testLoaderCaching()
12201214
$formBuilder->add('property2', static::TESTED_TYPE, [
12211215
'em' => 'default',
12221216
'class' => self::SINGLE_IDENT_CLASS,
1223-
'query_builder' => function (EntityRepository $repo) {
1224-
return $repo->createQueryBuilder('e')->where('e.id IN (1, 2)');
1225-
},
1217+
'query_builder' => fn (EntityRepository $repo) => $repo->createQueryBuilder('e')->where('e.id IN (1, 2)'),
12261218
]);
12271219

12281220
$formBuilder->add('property3', static::TESTED_TYPE, [
12291221
'em' => 'default',
12301222
'class' => self::SINGLE_IDENT_CLASS,
1231-
'query_builder' => function (EntityRepository $repo) {
1232-
return $repo->createQueryBuilder('e')->where('e.id IN (1, 2)');
1233-
},
1223+
'query_builder' => fn (EntityRepository $repo) => $repo->createQueryBuilder('e')->where('e.id IN (1, 2)'),
12341224
]);
12351225

12361226
$form = $formBuilder->getForm();
@@ -1280,17 +1270,13 @@ public function testLoaderCachingWithParameters()
12801270
$formBuilder->add('property2', static::TESTED_TYPE, [
12811271
'em' => 'default',
12821272
'class' => self::SINGLE_IDENT_CLASS,
1283-
'query_builder' => function (EntityRepository $repo) {
1284-
return $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1);
1285-
},
1273+
'query_builder' => fn (EntityRepository $repo) => $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1),
12861274
]);
12871275

12881276
$formBuilder->add('property3', static::TESTED_TYPE, [
12891277
'em' => 'default',
12901278
'class' => self::SINGLE_IDENT_CLASS,
1291-
'query_builder' => function (EntityRepository $repo) {
1292-
return $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1);
1293-
},
1279+
'query_builder' => fn (EntityRepository $repo) => $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1),
12941280
]);
12951281

12961282
$form = $formBuilder->getForm();
@@ -1791,9 +1777,7 @@ public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
17911777
->add('entity_two', self::TESTED_TYPE, [
17921778
'em' => 'default',
17931779
'class' => self::SINGLE_IDENT_CLASS,
1794-
'choice_value' => function ($choice) {
1795-
return $choice ? $choice->name : '';
1796-
},
1780+
'choice_value' => fn ($choice) => $choice ? $choice->name : '',
17971781
])
17981782
->createView()
17991783
;

src/Symfony/Bridge/Doctrine/Tests/SchemaListener/PdoSessionHandlerSchemaSubscriberTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testPostGenerateSchemaPdo()
3434
$pdoSessionHandler = $this->createMock(PdoSessionHandler::class);
3535
$pdoSessionHandler->expects($this->once())
3636
->method('configureSchema')
37-
->with($schema, fn() => true);
37+
->with($schema, fn () => true);
3838

3939
$subscriber = new PdoSessionHandlerSchemaSubscriber([$pdoSessionHandler]);
4040
$subscriber->postGenerateSchema($event);

src/Symfony/Bridge/Monolog/Tests/Handler/MailerHandlerTest.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public function testHandle()
3939
$this->mailer
4040
->expects($this->once())
4141
->method('send')
42-
->with($this->callback(function (Email $email) {
43-
return 'Alert: WARNING message' === $email->getSubject() && null === $email->getHtmlBody();
44-
}))
42+
->with($this->callback(fn (Email $email) => 'Alert: WARNING message' === $email->getSubject() && null === $email->getHtmlBody()))
4543
;
4644
$handler->handle($this->getRecord(Logger::WARNING, 'message'));
4745
}
@@ -53,9 +51,7 @@ public function testHandleBatch()
5351
$this->mailer
5452
->expects($this->once())
5553
->method('send')
56-
->with($this->callback(function (Email $email) {
57-
return 'Alert: ERROR error' === $email->getSubject() && null === $email->getHtmlBody();
58-
}))
54+
->with($this->callback(fn (Email $email) => 'Alert: ERROR error' === $email->getSubject() && null === $email->getHtmlBody()))
5955
;
6056
$handler->handleBatch($this->getMultipleRecords());
6157
}
@@ -86,9 +82,7 @@ public function testHtmlContent()
8682
$this->mailer
8783
->expects($this->once())
8884
->method('send')
89-
->with($this->callback(function (Email $email) {
90-
return 'Alert: WARNING message' === $email->getSubject() && null === $email->getTextBody();
91-
}))
85+
->with($this->callback(fn (Email $email) => 'Alert: WARNING message' === $email->getSubject() && null === $email->getTextBody()))
9286
;
9387
$handler->handle($this->getRecord(Logger::WARNING, 'message'));
9488
}

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Instantiator/RuntimeInstantiatorTest.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public function testInstantiateProxy()
4242
$instance = new \stdClass();
4343
$container = $this->createMock(ContainerInterface::class);
4444
$definition = new Definition('stdClass');
45-
$instantiator = function () use ($instance) {
46-
return $instance;
47-
};
45+
$instantiator = fn () => $instance;
4846

4947
/* @var $proxy LazyLoadingInterface|ValueHolderInterface */
5048
$proxy = $this->instantiator->instantiateProxy($container, $definition, 'foo', $instantiator);

src/Symfony/Bridge/Twig/Command/DebugCommand.php

+2-4
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);

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

+2-6
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
/**

src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php

+1-3
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

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

+2-6
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
]);

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php

+2-6
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
]);

src/Symfony/Bridge/Twig/Tests/Mime/BodyRendererTest.php

+1-3
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);

src/Symfony/Bridge/Twig/UndefinedCallableHandler.php

+1-1
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]));

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function warmUp(string $cacheDir): array
5353
// the ArrayAdapter stores the values serialized
5454
// to avoid mutation of the data after it was written to the cache
5555
// so here we un-serialize the values first
56-
$values = array_map(function ($val) { return null !== $val ? unserialize($val) : null; }, $arrayAdapter->getValues());
56+
$values = array_map(fn ($val) => null !== $val ? unserialize($val) : null, $arrayAdapter->getValues());
5757

5858
return $this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
5959
}

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
7171
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values): array
7272
{
7373
// make sure we don't cache null values
74-
$values = array_filter($values, function ($val) { return null !== $val; });
74+
$values = array_filter($values, fn ($val) => null !== $val);
7575

7676
return parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values);
7777
}

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
7171
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values): array
7272
{
7373
// make sure we don't cache null values
74-
$values = array_filter($values, function ($val) { return null !== $val; });
74+
$values = array_filter($values, fn ($val) => null !== $val);
7575

7676
return parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values);
7777
}

src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ protected function listBundles(OutputInterface|StyleInterface $output)
3535
$rows = [];
3636

3737
$bundles = $this->getApplication()->getKernel()->getBundles();
38-
usort($bundles, function ($bundleA, $bundleB) {
39-
return strcmp($bundleA->getName(), $bundleB->getName());
40-
});
38+
usort($bundles, fn ($bundleA, $bundleB) => strcmp($bundleA->getName(), $bundleB->getName()));
4139

4240
foreach ($bundles as $bundle) {
4341
$extension = $bundle->getContainerExtension();

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolListCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151
{
5252
$io = new SymfonyStyle($input, $output);
5353

54-
$io->table(['Pool name'], array_map(function ($pool) {
55-
return [$pool];
56-
}, $this->poolNames));
54+
$io->table(['Pool name'], array_map(fn ($pool) => [$pool], $this->poolNames));
5755

5856
return 0;
5957
}

0 commit comments

Comments
 (0)