Skip to content

Commit 10a951b

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 c92d85a + 2b5a6d9 commit 10a951b

File tree

7 files changed

+24
-50
lines changed

7 files changed

+24
-50
lines changed

Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 2 additions & 6 deletions
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)) {

Form/Type/DoctrineType.php

Lines changed: 7 additions & 9 deletions
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,

PropertyInfo/DoctrineExtractor.php

Lines changed: 1 addition & 3 deletions
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
}

Tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 1 addition & 3 deletions
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')

Tests/Form/ChoiceList/DoctrineChoiceLoaderTest.php

Lines changed: 2 additions & 2 deletions
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')

Tests/Form/Type/EntityTypeTest.php

Lines changed: 10 additions & 26 deletions
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
;

Tests/SchemaListener/PdoSessionHandlerSchemaSubscriberTest.php

Lines changed: 1 addition & 1 deletion
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);

0 commit comments

Comments
 (0)