8000 [Form] Use static closures when possible · kbond/symfony@b169b43 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit b169b43

Browse files
[Form] Use static closures when possible
1 parent c375406 commit b169b43

31 files changed

+104
-106
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function generateHash(mixed $value, string $namespace = ''): strin
5454
if (\is_object($value)) {
5555
$value = spl_object_hash($value);
5656
} elseif (\is_array($value)) {
57-
array_walk_recursive($value, function (&$v) {
57+
array_walk_recursive($value, static function (&$v) {
5858
if (\is_object($v)) {
5959
$v = spl_object_hash($v);
6060
}

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ 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 fn () => $choices
39-
), $filter), $value);
38+
new CallbackChoiceLoader(static fn () => $choices),
39+
$filter
40+
), $value);
4041
}
4142

4243
return new ArrayChoiceList($choices, $value);
@@ -133,9 +134,7 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
133134
);
134135
}
135136

136-
uksort($preferredViews, static fn ($a, $b): int => isset($preferredViewsOrder[$a], $preferredViewsOrder[$b])
137-
? $preferredViewsOrder[$a] <=> $preferredViewsOrder[$b]
138-
: 0);
137+
uksort($preferredViews, static fn ($a, $b) => isset($preferredViewsOrder[$a], $preferredViewsOrder[$b]) ? $preferredViewsOrder[$a] <=> $preferredViewsOrder[$b] : 0);
139138

140139
return new ChoiceListView($otherViews, $preferredViews);
141140
}

src/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function createListFromChoices(iterable $choices, mixed $value = null, mi
6767
// when such values are passed to
6868
// ChoiceListInterface::getValuesForChoices(). Handle this case
6969
// so that the call to getValue() doesn't break.
70-
$value = fn ($choice) => \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
70+
$value = static fn ($choice) => \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
7171
}
7272

7373
if (\is_string($filter)) {
@@ -94,7 +94,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, mixed $value
9494
// when such values are passed to
9595
// ChoiceListInterface::getValuesForChoices(). Handle this case
9696
// so that the call to getValue() doesn't break.
97-
$value = fn ($choice) => \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
97+
$value = static fn ($choice) => \is_object($choice) || \is_array($choice) ? $accessor->getValue($choice, $value) : null;
9898
}
9999

100100
if (\is_string($filter)) {
@@ -118,15 +118,15 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
118118
}
119119

120120
if ($label instanceof PropertyPathInterface) {
121-
$label = fn ($choice) => $accessor->getValue($choice, $label);
121+
$label = static fn ($choice) => $accessor->getValue($choice, $label);
122122
}
123123

124124
if (\is_string($preferredChoices)) {
125125
$preferredChoices = new PropertyPath($preferredChoices);
126126
}
127127

128128
if ($preferredChoices instanceof PropertyPathInterface) {
129-
$preferredChoices = function ($choice) use ($accessor, $preferredChoices) {
129+
$preferredChoices = static function ($choice) use ($accessor, $preferredChoices) {
130130
try {
131131
return $accessor->getValue($choice, $preferredChoices);
132132
} catch (UnexpectedTypeException) {
@@ -141,15 +141,15 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
141141
}
142142

143143
if ($index instanceof PropertyPathInterface) {
144-
$index = fn ($choice) => $accessor->getValue($choice, $index);
144+
$index = static fn ($choice) => $accessor->getValue($choice, $index);
145145
}
146146

147147
if (\is_string($groupBy)) {
148148
$groupBy = new PropertyPath($groupBy);
149149
}
150150

151151
if ($groupBy instanceof PropertyPathInterface) {
152-
$groupBy = function ($choice) use ($accessor, $groupBy) {
152+
$groupBy = static function ($choice) use ($accessor, $groupBy) {
153153
try {
154154
return $accessor->getValue($choice, $groupBy);
155155
} catch (UnexpectedTypeException) {
@@ -164,7 +164,7 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
164164
}
165165

166166
if ($attr instanceof PropertyPathInterface) {
167-
$attr = fn ($choice) => $accessor->getValue($choice, $attr);
167+
$attr = static fn ($choice) => $accessor->getValue($choice, $attr);
168168
}
169169

170170
if (\is_string($labelTranslationParameters)) {

src/Symfony/Component/Form/Command/DebugCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private function getCoreTypes(): array
207207
$coreExtension = new CoreExtension();
208208
$loadTypesRefMethod = (new \ReflectionObject($coreExtension))->getMethod('loadTypes');
209209
$coreTypes = $loadTypesRefMethod->invoke($coreExtension);
210-
$coreTypes = array_map(fn (FormTypeInterface $type) => $type::class, $coreTypes);
210+
$coreTypes = array_map(static fn (FormTypeInterface $type) => $type::class, $coreTypes);
211211
sort($coreTypes);
212212

213213
return $coreTypes;
@@ -240,7 +240,7 @@ private function findAlternatives(string $name, array $collection): array
240240
}
241241

242242
$threshold = 1e3;
243-
$alternatives = array_filter($alternatives, fn ($lev) => $lev < 2 * $threshold);
243+
$alternatives = array_filter($alternatives, static fn ($lev) => $lev < 2 * $threshold);
244244
ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE);
245245

246246
return array_keys($alternatives);

src/Symfony/Component/Form/Console/Descriptor/Descriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ protected function filterOptionsByDeprecated(ResolvedFormTypeInterface $type): v
149149
}
150150
}
151151

152-
$filterByDeprecated = function (array $options) use ($deprecatedOptions) {
152+
$filterByDeprecated = static function (array $options) use ($deprecatedOptions) {
153153
foreach ($options as $class => $opts) {
154154
if ($deprecated = array_intersect($deprecatedOptions, $opts)) {
155155
$options[$class] = $deprecated;

src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
5151
*/
5252
public function configureOptions(OptionsResolver $resolver)
5353
{
54-
$emptyData = fn (FormInterface $form, $viewData) => $viewData;
54+
$emptyData = static fn (FormInterface $form, $viewData) => $viewData;
5555

5656
$resolver->setDefaults([
5757
'value' => '1',

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9797
if ($options['expanded'] || $options['multiple']) {
9898
// Make sure that scalar, submitted values are converted to arrays
9999
// which can be submitted to the checkboxes/radio buttons
100-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($choiceList, $options, &$unknownValues) {
100+
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($choiceList, $options, &$unknownValues) {
101101
$form = $event->getForm();
102102
$data = $event->getData();
103103

@@ -166,16 +166,17 @@ public function buildForm(FormBuilderInterface $builder, array $options)
166166

167167
if ($options['multiple']) {
168168
$messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';
169+
$translator = $this->translator;
169170

170-
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
171+
$builder->addEventListener(FormEvents::POST_SUBMIT, static function (FormEvent $event) use (&$unknownValues, $messageTemplate, $translator) {
171172
// Throw exception if unknown values were submitted
172173
if (\count($unknownValues) > 0) {
173174
$form = $event->getForm();
174175

175176
$clientDataAsString = \is_scalar($form->getViewData()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
176177

177-
if (null !== $this->translator) {
178-
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
178+
if ($translator) {
179+
$message = $translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
179180
} else {
180181
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
181182
}
@@ -199,7 +200,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
199200

200201
// To avoid issues when the submitted choices are arrays (i.e. array to string conversions),
201202
// we have to ensure that all elements of the submitted choice data are NULL, strings or ints.
202-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
203+
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) {
203204
$data = $event->getData();
204205

205206
if (!\is_array($data)) {
@@ -245,9 +246,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
245246
// closure here that is optimized for the value of the form, to
246247
// avoid making the type check inside the closure.
247248
if ($options['multiple']) {
248-
$view->vars['is_selected'] = fn ($choice, array $values) => \in_array($choice, $values, true);
249+
$view->vars['is_selected'] = static fn ($choice, array $values) => \in_array($choice, $values, true);
249250
} else {
250-
$view->vars['is_selected'] = fn ($choice, $value) => $choice === $value;
251+
$view->vars['is_selected'] = static fn ($choice, $value) => $choice === $value;
251252
}
252253

253254
// Check if the choices already contain the empty value
@@ -285,7 +286,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
285286

286287
public function configureOptions(OptionsResolver $resolver)
287288
{
288-
$emptyData = function (Options $options) {
289+
$emptyData = static function (Options $options) {
289290
if ($options['expanded'] && !$options['multiple']) {
290291
return null;
291292
}
@@ -297,9 +298,9 @@ public function configureOptions(OptionsResolver $resolver)
297298
return '';
298299
};
299300

300-
$placeholderDefault = fn (Options $options) => $options['required'] ? null : '';
301+
$placeholderDefault = static fn (Options $options) => $options['required'] ? null : '';
301302

302-
$placeholderNormalizer = function (Options $options, $placeholder) {
303+
$placeholderNormalizer = static function (Options $options, $placeholder) {
303304
if ($options['multiple']) {
304305
// never use an empty value for this case
305306
return null;
@@ -318,9 +319,9 @@ public function configureOptions(OptionsResolver $resolver)
318319
return $placeholder;
319320
};
320321

321-
$compound = fn (Options $options) => $options['expanded'];
322+
$compound = static fn (Options $options) => $options['expanded'];
322323

323-
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
324+
$choiceTranslationDomainNormalizer = static function (Options $options, $choiceTranslationDomain) {
324325
if (true === $choiceTranslationDomain) {
325326
return $options['translation_domain'];
326327
}

src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
106106
*/
107107
public function configureOptions(OptionsResolver $resolver)
108108
{
109-
$entryOptionsNormalizer = function (Options $options, $value) {
109+
$entryOptionsNormalizer = static function (Options $options, $value) {
110110
$value['block_name'] = 'entry';
111111

112112
return $value;

src/Symfony/Component/Form/Extension/Core/Type/ColorType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4242
return;
4343
}
4444

45-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
45+
$translator = $this->translator;
46+
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($translator): void {
4647
$value = $event->getData();
4748
if (null === $value || '' === $value) {
4849
return;
@@ -56,7 +57,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5657
$messageParameters = [
5758
'{{ value }}' => \is_scalar($value) ? (string) $value : \gettype($value),
5859
];
59-
$message = $this->translator ? $this->translator->trans($messageTemplate, $messageParameters, 'validators') : $messageTemplate;
60+
$message = $translator?->trans($messageTemplate, $messageParameters, 'validators') ?? $messageTemplate;
6061

6162
$event->getForm()->addError(new FormError($message, $messageTemplate, $messageParameters));
6263
});

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function configureOptions(OptionsResolver $resolver)
3636
$choiceTranslationLocale = $options['choice_translation_locale'];
3737
$alpha3 = $options['alpha3'];
3838

39-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(fn () => array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale))), [$choiceTranslationLocale, $alpha3]);
39+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(static fn () => array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale))), [$choiceTranslationLocale, $alpha3]);
4040
},
4141
'choice_translation_domain' => false,
4242
'choice_translation_locale' => null,

0 commit comments

Comments
 (0)
0