Skip to content

Commit f82bfb8

Browse files
tigitznicolas-grekas
authored andcommitted
Leverage arrow function syntax for closure
1 parent 3a676e1 commit f82bfb8

File tree

58 files changed

+206
-494
lines changed

Some content is hidden

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

58 files changed

+206
-494
lines changed

ChoiceList/ArrayChoiceList.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public function __construct(iterable $choices, callable $value = null)
6464
}
6565

6666
if (null === $value && $this->castableToString($choices)) {
67-
$value = function ($choice) {
68-
return false === $choice ? '0' : (string) $choice;
69-
};
67+
$value = fn ($choice) => false === $choice ? '0' : (string) $choice;
7068
}
7169

7270
if (null !== $value) {

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public function createListFromChoices(iterable $choices, callable $value = null,
3535
if ($filter) {
3636
// filter the choice list lazily
3737
return $this->createListFromLoader(new FilterChoiceLoaderDecorator(
38-
new CallbackChoiceLoader(static function () use ($choices) {
39-
return $choices;
40-
}
38+
new CallbackChoiceLoader(static fn () => $choices
4139
), $filter), $value);
4240
}
4341

@@ -67,9 +65,7 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
6765
} else {
6866
// make sure we have keys that reflect order
6967
$preferredChoices = array_values($preferredChoices);
70-
$preferredChoices = static function ($choice) use ($preferredChoices) {
71-
return array_search($choice, $preferredChoices, true);
72-
};
68+
$preferredChoices = static fn ($choice) => array_search($choice, $preferredChoices, true);
7369
}
7470
}
7571

@@ -137,11 +133,9 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
137133
);
138134
}
139135

140-
uksort($preferredViews, static function ($a, $b) use ($preferredViewsOrder): int {
141-
return isset($preferredViewsOrder[$a], $preferredViewsOrder[$b])
142-
? $preferredViewsOrder[$a] <=> $preferredViewsOrder[$b]
143-
: 0;
144-
});
136+
uksort($preferredViews, static fn ($a, $b): int => isset($preferredViewsOrder[$a], $preferredViewsOrder[$b])
137+
? $preferredViewsOrder[$a] <=> $preferredViewsOrder[$b]
138+
: 0);
145139

146140
return new ChoiceListView($otherViews, $preferredViews);
147141
}

ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ public function createListFromChoices(iterable $choices, mixed $value = null, mi
6363

6464
if ($value instanceof PropertyPathInterface) {
6565
$accessor = $this->propertyAccessor;
66-
$value = function ($choice) use ($accessor, $value) {
67-
// The callable may be invoked with a non-object/array value
68-
// when such values are passed to
69-
// ChoiceListInterface::getValuesForChoices(). Handle this case
70-
// so that the call to getValue() doesn't break.
71-
return \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
72-
};
66+
// The callable may be invoked with a non-object/array value
67+
// when such values are passed to
68+
// ChoiceListInterface::getValuesForChoices(). Handle this case
69+
// so that the call to getValue() doesn't break.
70+
$value = fn ($choice) => \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
7371
}
7472

7573
if (\is_string($filter)) {
@@ -78,9 +76,7 @@ public function createListFromChoices(iterable $choices, mixed $value = null, mi
7876

7977
if ($filter instanceof PropertyPath) {
8078
$accessor = $this->propertyAccessor;
81-
$filter = static function ($choice) use ($accessor, $filter) {
82-
return (\is_object($choice) || \is_array($choice)) && $accessor->getValue($choice, $filter);
83-
};
79+
$filter = static fn ($choice) => (\is_object($choice) || \is_array($choice)) && $accessor->getValue($choice, $filter);
8480
}
8581

8682
return $this->decoratedFactory->createListFromChoices($choices, $value, $filter);
@@ -94,13 +90,11 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, mixed $value
9490

9591
if ($value instanceof PropertyPathInterface) {
9692
$accessor = $this->propertyAccessor;
97-
$value = function ($choice) use ($accessor, $value) {
98-
// The callable may be invoked with a non-object/array value
99-
// when such values are passed to
100-
// ChoiceListInterface::getValuesForChoices(). Handle this case
101-
// so that the call to getValue() doesn't break.
102-
return \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
103-
};
93+
// The callable may be invoked with a non-object/array value
94+
// when such values are passed to
95+
// ChoiceListInterface::getValuesForChoices(). Handle this case
96+
// so that the call to getValue() doesn't break.
97+
$value = fn ($choice) => \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
10498
}
10599

106100
if (\is_string($filter)) {
@@ -109,9 +103,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, mixed $value
109103

110104
if ($filter instanceof PropertyPath) {
111105
$accessor = $this->propertyAccessor;
112-
$filter = static function ($choice) use ($accessor, $filter) {
113-
return (\is_object($choice) || \is_array($choice)) && $accessor->getValue($choice, $filter);
114-
};
106+
$filter = static fn ($choice) => (\is_object($choice) || \is_array($choice)) && $accessor->getValue($choice, $filter);
115107
}
116108

117109
return $this->decoratedFactory->createListFromLoader($loader, $value, $filter);
@@ -126,9 +118,7 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
126118
}
127119

128120
if ($label instanceof PropertyPathInterface) {
129-
$label = function ($choice) use ($accessor, $label) {
130-
return $accessor->getValue($choice, $label);
131-
};
121+
$label = fn ($choice) => $accessor->getValue($choice, $label);
132122
}
133123

134124
if (\is_string($preferredChoices)) {
@@ -151,9 +141,7 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
151141
}
152142

153143
if ($index instanceof PropertyPathInterface) {
154-
$index = function ($choice) use ($accessor, $index) {
155-
return $accessor->getValue($choice, $index);
156-
};
144+
$index = fn ($choice) => $accessor->getValue($choice, $index);
157145
}
158146

159147
if (\is_string($groupBy)) {
@@ -176,19 +164,15 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
176164
}
177165

178166
if ($attr instanceof PropertyPathInterface) {
179-
$attr = function ($choice) use ($accessor, $attr) {
180-
return $accessor->getValue($choice, $attr);
181-
};
167+
$attr = fn ($choice) => $accessor->getValue($choice, $attr);
182168
}
183169

184170
if (\is_string($labelTranslationParameters)) {
185171
$labelTranslationParameters = new PropertyPath($labelTranslationParameters);
186172
}
187173

188174
if ($labelTranslationParameters instanceof PropertyPath) {
189-
$labelTranslationParameters = static function ($choice) use ($accessor, $labelTranslationParameters) {
190-
return $accessor->getValue($choice, $labelTranslationParameters);
191-
};
175+
$labelTranslationParameters = static fn ($choice) => $accessor->getValue($choice, $labelTranslationParameters);
192176
}
193177

194178
return $this->decoratedFactory->createView(

Command/DebugCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private function getCoreTypes(): array
204204
$coreExtension = new CoreExtension();
205205
$loadTypesRefMethod = (new \ReflectionObject($coreExtension))->getMethod('loadTypes');
206206
$coreTypes = $loadTypesRefMethod->invoke($coreExtension);
207-
$coreTypes = array_map(function (FormTypeInterface $type) { return $type::class; }, $coreTypes);
207+
$coreTypes = array_map(fn (FormTypeInterface $type) => $type::class, $coreTypes);
208208
sort($coreTypes);
209209

210210
return $coreTypes;
@@ -237,7 +237,7 @@ private function findAlternatives(string $name, array $collection): array
237237
}
238238

239239
$threshold = 1e3;
240-
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
240+
$alternatives = array_filter($alternatives, fn ($lev) => $lev < 2 * $threshold);
241241
ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE);
242242

243243
return array_keys($alternatives);

Console/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ protected function describeDefaults(array $options)
3535
{
3636
if ($options['core_types']) {
3737
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
38-
$shortClassNames = array_map(function ($fqcn) {
39-
return $this->formatClassLink($fqcn, \array_slice(explode('\\', $fqcn), -1)[0]);
40-
}, $options['core_types']);
38+
$shortClassNames = array_map(fn ($fqcn) => $this->formatClassLink($fqcn, \array_slice(explode('\\', $fqcn), -1)[0]), $options['core_types']);
4139
for ($i = 0, $loopsMax = \count($shortClassNames); $i * 5 < $loopsMax; ++$i) {
4240
$this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5)));
4341
}

Extension/Core/Type/CheckboxType.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,15 @@ public function buildView(FormView $view, FormInterface $form, array $options)
4242

4343
public function configureOptions(OptionsResolver $resolver)
4444
{
45-
$emptyData = function (FormInterface $form, $viewData) {
46-
return $viewData;
47-
};
45+
$emptyData = fn (FormInterface $form, $viewData) => $viewData;
4846

4947
$resolver->setDefaults([
5048
'value' => '1',
5149
'empty_data' => $emptyData,
5250
'compound' => false,
5351
'false_values' => [null],
5452
'invalid_message' => 'The checkbox has an invalid value.',
55-
'is_empty_callback' => static function ($modelData): bool {
56-
return false === $modelData;
57-
},
53+
'is_empty_callback' => static fn ($modelData): bool => false === $modelData,
5854
]);
5955

6056
$resolver->setAllowedTypes('false_values', 'array');

Extension/Core/Type/ChoiceType.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
245245
// closure here that is optimized for the value of the form, to
246246
// avoid making the type check inside the closure.
247247
if ($options['multiple']) {
248-
$view->vars['is_selected'] = function ($choice, array $values) {
249-
return \in_array($choice, $values, true);
250-
};
248+
$view->vars['is_selected'] = fn ($choice, array $values) => \in_array($choice, $values, true);
251249
} else {
252-
$view->vars['is_selected'] = function ($choice, $value) {
253-
return $choice === $value;
254-
};
250+
$view->vars['is_selected'] = fn ($choice, $value) => $choice === $value;
255251
}
256252

257253
// Check if the choices already contain the empty value
@@ -301,9 +297,7 @@ public function configureOptions(OptionsResolver $resolver)
301297
return '';
302298
};
303299

304-
$placeholderDefault = function (Options $options) {
305-
return $options['required'] ? null : '';
306-
};
300+
$placeholderDefault = fn (Options $options) => $options['required'] ? null : '';
307301

308302
$placeholderNormalizer = function (Options $options, $placeholder) {
309303
if ($options['multiple']) {
@@ -324,9 +318,7 @@ public function configureOptions(OptionsResolver $resolver)
324318
return $placeholder;
325319
};
326320

327-
$compound = function (Options $options) {
328-
return $options['expanded'];
329-
};
321+
$compound = fn (Options $options) => $options['expanded'];
330322

331323
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
332324
if (true === $choiceTranslationDomain) {

Extension/Core/Type/CountryType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public function configureOptions(OptionsResolver $resolver)
3333
$choiceTranslationLocale = $options['choice_translation_locale'];
3434
$alpha3 = $options['alpha3'];
3535

36-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) {
37-
return array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale));
38-
}), [$choiceTranslationLocale, $alpha3]);
36+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(fn () => array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale))), [$choiceTranslationLocale, $alpha3]);
3937
},
4038
'choice_translation_domain' => false,
4139
'choice_translation_locale' => null,

Extension/Core/Type/CurrencyType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public function configureOptions(OptionsResolver $resolver)
3232

3333
$choiceTranslationLocale = $options['choice_translation_locale'];
3434

35-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
36-
return array_flip(Currencies::getNames($choiceTranslationLocale));
37-
}), $choiceTranslationLocale);
35+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(fn () => array_flip(Currencies::getNames($choiceTranslationLocale))), $choiceTranslationLocale);
3836
},
3937
'choice_translation_domain' => false,
4038
'choice_translation_locale' => null,

Extension/Core/Type/DateIntervalType.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
159159

160160
public function configureOptions(OptionsResolver $resolver)
161161
{
162-
$compound = function (Options $options) {
163-
return 'single_text' !== $options['widget'];
164-
};
165-
$emptyData = function (Options $options) {
166-
return 'single_text' === $options['widget'] ? '' : [];
167-
};
162+
$compound = fn (Options $options) => 'single_text' !== $options['widget'];
163+
$emptyData = fn (Options $options) => 'single_text' === $options['widget'] ? '' : [];
168164

169-
$placeholderDefault = function (Options $options) {
170-
return $options['required'] ? null : '';
171-
};
165+
$placeholderDefault = fn (Options $options) => $options['required'] ? null : '';
172166

173167
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
174168
if (\is_array($placeholder)) {
@@ -180,20 +174,16 @@ public function configureOptions(OptionsResolver $resolver)
180174
return array_fill_keys(self::TIME_PARTS, $placeholder);
181175
};
182176

183-
$labelsNormalizer = function (Options $options, array $labels) {
184-
return array_replace([
185-
'years' => null,
186-
'months' => null,
187-
'days' => null,
188-
'weeks' => null,
189-
'hours' => null,
190-
'minutes' => null,
191-
'seconds' => null,
192-
'invert' => 'Negative interval',
193-
], array_filter($labels, function ($label) {
194-
return null !== $label;
195-
}));
196-
};
177+
$labelsNormalizer = fn (Options $options, array $labels) => array_replace([
178+
'years' => null,
179+
'months' => null,
180+
'days' => null,
181+
'weeks' => null,
182+
'hours' => null,
183+
'minutes' => null,
184+
'seconds' => null,
185+
'invert' => 'Negative interval',
186+
], array_filter($labels, fn ($label) => null !== $label));
197187

198188
$resolver->setDefaults([
199189
'with_years' => true,

0 commit comments

Comments
 (0)