Skip to content

Commit 3963819

Browse files
committedMar 28, 2023
[Form] Use static closures when possible
1 parent ce99d4a commit 3963819

31 files changed

+104
-106
lines changed
 

‎ChoiceList/Factory/CachingFactoryDecorator.php

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

‎ChoiceList/Factory/DefaultChoiceListFactory.php

+4-5
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
}

‎ChoiceList/Factory/PropertyAccessDecorator.php

+7-7
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)) {

‎Command/DebugCommand.php

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

‎Console/Descriptor/Descriptor.php

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

‎Extension/Core/Type/CheckboxType.php

+1-1
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',

‎Extension/Core/Type/ChoiceType.php

+13-12
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
}

‎Extension/Core/Type/CollectionType.php

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

‎Extension/Core/Type/ColorType.php

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

‎Extension/Core/Type/CountryType.php

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

‎Extension/Core/Type/CurrencyType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function configureOptions(OptionsResolver $resolver)
3535

3636
$choiceTranslationLocale = $options['choice_translation_locale'];
3737

38-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(fn () => array_flip(Currencies::getNames($choiceTranslationLocale))), $choiceTranslationLocale);
38+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(static fn () => array_flip(Currencies::getNames($choiceTranslationLocale))), $choiceTranslationLocale);
3939
},
4040
'choice_translation_domain' => false,
4141
'choice_translation_locale' => null,

‎Extension/Core/Type/DateIntervalType.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)
168168
*/
169169
public function configureOptions(OptionsResolver $resolver)
170170
{
171-
$compound = fn (Options $options) => 'single_text' !== $options['widget'];
172-
$emptyData = fn (Options $options) => 'single_text' === $options['widget'] ? '' : [];
171+
$compound = static fn (Options $options) => 'single_text' !== $options['widget'];
172+
$emptyData = static fn (Options $options) => 'single_text' === $options['widget'] ? '' : [];
173173

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

176-
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
176+
$placeholderNormalizer = static function (Options $options, $placeholder) use ($placeholderDefault) {
177177
if (\is_array($placeholder)) {
178178
$default = $placeholderDefault($options);
179179

@@ -183,7 +183,7 @@ public function configureOptions(OptionsResolver $resolver)
183183
return array_fill_keys(self::TIME_PARTS, $placeholder);
184184
};
185185

186-
$labelsNormalizer = fn (Options $options, array $labels) => array_replace([
186+
$labelsNormalizer = static fn (Options $options, array $labels) => array_replace([
187187
'years' => null,
188188
'months' => null,
189189
'days' => null,
@@ -192,7 +192,7 @@ public function configureOptions(OptionsResolver $resolver)
192192
'minutes' => null,
193193
'seconds' => null,
194194
'invert' => 'Negative interval',
195-
], array_filter($labels, fn ($label) => null !== $label));
195+
], array_filter($labels, static fn ($label) => null !== $label));
196196

197197
$resolver->setDefaults([
198198
'with_years' => true,

‎Extension/Core/Type/DateTimeType.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
229229
*/
230230
public function configureOptions(OptionsResolver $resolver)
231231
{
232-
$compound = fn (Options $options) => 'single_text' !== $options['widget'];
232+
$compound = static fn (Options $options) => 'single_text' !== $options['widget'];
233233

234234
// Defaults to the value of "widget"
235-
$dateWidget = fn (Options $options) => 'single_text' === $options['widget'] ? null : $options['widget'];
235+
$dateWidget = static fn (Options $options) => 'single_text' === $options['widget'] ? null : $options['widget'];
236236

237237
// Defaults to the value of "widget"
238-
$timeWidget = fn (Options $options) => 'single_text' === $options['widget'] ? null : $options['widget'];
238+
$timeWidget = static fn (Options $options) => 'single_text' === $options['widget'] ? null : $options['widget'];
239239

240240
$resolver->setDefaults([
241241
'input' => 'datetime',
@@ -261,7 +261,7 @@ public function configureOptions(OptionsResolver $resolver)
261261
'compound' => $compound,
262262
'date_label' => null,
263263
'time_label' => null,
264-
'empty_data' => fn (Options $options) => $options['compound'] ? [] : '',
264+
'empty_data' => static fn (Options $options) => $options['compound'] ? [] : '',
265265
'input_format' => 'Y-m-d H:i:s',
266266
'invalid_message' => 'Please enter a valid date and time.',
267267
]);
@@ -308,28 +308,28 @@ public function configureOptions(OptionsResolver $resolver)
308308

309309
$resolver->setAllowedTypes('input_format', 'string');
310310

311-
$resolver->setNormalizer('date_format', function (Options $options, $dateFormat) {
311+
$resolver->setNormalizer('date_format', static function (Options $options, $dateFormat) {
312312
if (null !== $dateFormat && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
313313
throw new LogicException(sprintf('Cannot use the "date_format" option of the "%s" with an HTML5 date.', self::class));
314314
}
315315

316316
return $dateFormat;
317317
});
318-
$resolver->setNormalizer('date_widget', function (Options $options, $dateWidget) {
318+
$resolver->setNormalizer('date_widget', static function (Options $options, $dateWidget) {
319319
if (null !== $dateWidget && 'single_text' === $options['widget']) {
320320
throw new LogicException(sprintf('Cannot use the "date_widget" option of the "%s" when the "widget" option is set to "single_text".', self::class));
321321
}
322322

323323
return $dateWidget;
324324
});
325-
$resolver->setNormalizer('time_widget', function (Options $options, $timeWidget) {
325+
$resolver->setNormalizer('time_widget', static function (Options $options, $timeWidget) {
326326
if (null !== $timeWidget && 'single_text' === $options['widget']) {
327327
throw new LogicException(sprintf('Cannot use the "time_widget" option of the "%s" when the "widget" option is set to "single_text".', self::class));
328328
}
329329

330330
return $timeWidget;
331331
});
332-
$resolver->setNormalizer('html5', function (Options $options, $html5) {
332+
$resolver->setNormalizer('html5', static function (Options $options, $html5) {
333333
if ($html5 && self::HTML5_FORMAT !== $options['format']) {
334334
throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is enabled.', self::class));
335335
}

‎Extension/Core/Type/DateType.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ public function finishView(FormView $view, FormInterface $form, array $options)
216216

217217
public function configureOptions(OptionsResolver $resolver)
218218
{
219-
$compound = fn (Options $options) => 'single_text' !== $options['widget'];
219+
$compound = static fn (Options $options) => 'single_text' !== $options['widget'];
220220

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

223-
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
223+
$placeholderNormalizer = static function (Options $options, $placeholder) use ($placeholderDefault) {
224224
if (\is_array($placeholder)) {
225225
$default = $placeholderDefault($options);
226226

@@ -237,7 +237,7 @@ public function configureOptions(OptionsResolver $resolver)
237237
];
238238
};
239239

240-
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
240+
$choiceTranslationDomainNormalizer = static function (Options $options, $choiceTranslationDomain) {
241241
if (\is_array($choiceTranslationDomain)) {
242242
$default = false;
243243

@@ -254,7 +254,7 @@ public function configureOptions(OptionsResolver $resolver)
254254
];
255255
};
256256

257-
$format = fn (Options $options) => 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;
257+
$format = static fn (Options $options) => 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;
258258

259259
$resolver->setDefaults([
260260
'years' => range((int) date('Y') - 5, (int) date('Y') + 5),
@@ -277,7 +277,7 @@ public function configureOptions(OptionsResolver $resolver)
277277
// this option.
278278
'data_class' => null,
279279
'compound' => $compound,
280-
'empty_data' => fn (Options $options) => $options['compound'] ? [] : '',
280+
'empty_data' => static fn (Options $options) => $options['compound'] ? [] : '',
281281
'choice_translation_domain' => false,
282282
'input_format' => 'Y-m-d',
283283
'invalid_message' => 'Please enter a valid date.',
@@ -305,7 +305,7 @@ public function configureOptions(OptionsResolver $resolver)
305305
$resolver->setAllowedTypes('days', 'array');
306306
$resolver->setAllowedTypes('input_format', 'string');
307307

308-
$resolver->setNormalizer('html5', function (Options $options, $html5) {
308+
$resolver->setNormalizer('html5', static function (Options $options, $html5) {
309309
if ($html5 && 'single_text' === $options['widget'] && self::HTML5_FORMAT !== $options['format']) {
310310
throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is enabled.', self::class));
311311
}

‎Extension/Core/Type/FileType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public function configureOptions(OptionsResolver $resolver)
104104
{
105105
$dataClass = null;
106106
if (class_exists(File::class)) {
107-
$dataClass = fn (Options $options) => $options['multiple'] ? null : File::class;
107+
$dataClass = static fn (Options $options) => $options['multiple'] ? null : File::class;
108108
}
109109

110-
$emptyData = fn (Options $options) => $options['multiple'] ? [] : null;
110+
$emptyData = static fn (Options $options) => $options['multiple'] ? [] : null;
111111

112112
$resolver->setDefaults([
113113
'compound' => false,

‎Extension/Core/Type/FormType.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,25 @@ public function configureOptions(OptionsResolver $resolver)
136136
parent::configureOptions($resolver);
137137

138138
// Derive "data_class" option from passed "data" object
139-
$dataClass = fn (Options $options) => isset($options['data']) && \is_object($options['data']) ? $options['data']::class : null;
139+
$dataClass = static fn (Options $options) => isset($options['data']) && \is_object($options['data']) ? $options['data']::class : null;
140140

141141
// Derive "empty_data" closure from "data_class" option
142-
$emptyData = function (Options $options) {
142+
$emptyData = static function (Options $options) {
143143
$class = $options['data_class'];
144144

145145
if (null !== $class) {
146-
return fn (FormInterface $form) => $form->isEmpty() && !$form->isRequired() ? null : new $class();
146+
return static fn (FormInterface $form) => $form->isEmpty() && !$form->isRequired() ? null : new $class();
147147
}
148148

149-
return fn (FormInterface $form) => $form->getConfig()->getCompound() ? [] : '';
149+
return static fn (FormInterface $form) => $form->getConfig()->getCompound() ? [] : '';
150150
};
151151

152152
// Wrap "post_max_size_message" in a closure to translate it lazily
153-
$uploadMaxSizeMessage = fn (Options $options) => fn () => $options['post_max_size_message'];
153+
$uploadMaxSizeMessage = static fn (Options $options) => static fn () => $options['post_max_size_message'];
154154

155155
// For any form that is not represented by a single HTML control,
156156
// errors should bubble up by default
157-
$errorBubbling = fn (Options $options) => $options['compound'] && !$options['inherit_data'];
157+
$errorBubbling = static fn (Options $options) => $options['compound'] && !$options['inherit_data'];
158158

159159
// If data is given, the form is locked to that data
160160
// (independent of its value)

‎Extension/Core/Type/LanguageType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function configureOptions(OptionsResolver $resolver)
3737
$useAlpha3Codes = $options['alpha3'];
3838
$choiceSelfTranslation = $options['choice_self_translation'];
3939

40-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $useAlpha3Codes, $choiceSelfTranslation) {
40+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(static function () use ($choiceTranslationLocale, $useAlpha3Codes, $choiceSelfTranslation) {
4141
if (true === $choiceSelfTranslation) {
4242
foreach (Languages::getLanguageCodes() as $alpha2Code) {
4343
try {
@@ -65,7 +65,7 @@ public function configureOptions(OptionsResolver $resolver)
6565
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
6666
$resolver->setAllowedTypes('alpha3', 'bool');
6767

68-
$resolver->setNormalizer('choice_self_translation', function (Options $options, $value) {
68+
$resolver->setNormalizer('choice_self_translation', static function (Options $options, $value) {
6969
if (true === $value && $options['choice_translation_locale']) {
7070
throw new LogicException('Cannot use the "choice_self_translation" and "choice_translation_locale" options at the same time. Remove one of them.');
7171
}

‎Extension/Core/Type/LocaleType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function configureOptions(OptionsResolver $resolver)
3535

3636
$choiceTranslationLocale = $options['choice_translation_locale'];
3737

38-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(fn () => array_flip(Locales::getNames($choiceTranslationLocale))), $choiceTranslationLocale);
38+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(static fn () => array_flip(Locales::getNames($choiceTranslationLocale))), $choiceTranslationLocale);
3939
},
4040
'choice_translation_domain' => false,
4141
'choice_translation_locale' => null,

‎Extension/Core/Type/MoneyType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function configureOptions(OptionsResolver $resolver)
8484

8585
$resolver->setAllowedTypes('html5', 'bool');
8686

87-
$resolver->setNormalizer('grouping', function (Options $options, $value) {
87+
$resolver->setNormalizer('grouping', static function (Options $options, $value) {
8888
if ($value && $options['html5']) {
8989
throw new LogicException('Cannot use the "grouping" option when the "html5" option is enabled.');
9090
}

‎Extension/Core/Type/NumberType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function configureOptions(OptionsResolver $resolver)
8585
$resolver->setAllowedTypes('scale', ['null', 'int']);
8686
$resolver->setAllowedTypes('html5', 'bool');
8787

88-
$resolver->setNormalizer('grouping', function (Options $options, $value) {
88+
$resolver->setNormalizer('grouping', static function (Options $options, $value) {
8989
if (true === $value && $options['html5']) {
9090
throw new LogicException('Cannot use the "grouping" option when the "html5" option is enabled.');
9191
}

‎Extension/Core/Type/TimeType.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6161
}
6262

6363
if ('single_text' === $options['widget']) {
64-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) use ($options) {
64+
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $e) use ($options) {
6565
$data = $e->getData();
6666
if ($data && preg_match('/^(?P<hours>\d{2}):(?P<minutes>\d{2})(?::(?P<seconds>\d{2})(?:\.\d+)?)?$/', $data, $matches)) {
6767
if ($options['with_seconds']) {
@@ -79,7 +79,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7979
if (null !== $options['reference_date']) {
8080
$parseFormat = 'Y-m-d '.$format;
8181

82-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
82+
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($options) {
8383
$data = $event->getData();
8484

8585
if (preg_match('/^\d{2}:\d{2}(:\d{2})?$/', $data)) {
@@ -242,11 +242,11 @@ public function buildView(FormView $view, FormInterface $form, array $options)
242242
*/
243243
public function configureOptions(OptionsResolver $resolver)
244244
{
245-
$compound = fn (Options $options) => 'single_text' !== $options['widget'];
245+
$compound = static fn (Options $options) => 'single_text' !== $options['widget'];
246246

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

249-
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
249+
$placeholderNormalizer = static function (Options $options, $placeholder) use ($placeholderDefault) {
250250
if (\is_array($placeholder)) {
251251
$default = $placeholderDefault($options);
252252

@@ -263,7 +263,7 @@ public function configureOptions(OptionsResolver $resolver)
263263
];
264264
};
265265

266-
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
266+
$choiceTranslationDomainNormalizer = static function (Options $options, $choiceTranslationDomain) {
267267
if (\is_array($choiceTranslationDomain)) {
268268
$default = false;
269269

@@ -327,13 +327,13 @@ public function configureOptions(OptionsResolver $resolver)
327327
// representation is not \DateTime, but an array, we need to unset
328328
// this option.
329329
'data_class' => null,
330-
'empty_data' => fn (Options $options) => $options['compound'] ? [] : '',
330+
'empty_data' => static fn (Options $options) => $options['compound'] ? [] : '',
331331
'compound' => $compound,
332332
'choice_translation_domain' => false,
333333
'invalid_message' => 'Please enter a valid time.',
334334
]);
335335

336-
$resolver->setNormalizer('view_timezone', function (Options $options, $viewTimezone): ?string {
336+
$resolver->setNormalizer('view_timezone', static function (Options $options, $viewTimezone): ?string {
337337
if (null !== $options['model_timezone'] && $viewTimezone !== $options['model_timezone'] && null === $options['reference_date']) {
338338
throw new LogicException('Using different values for the "model_timezone" and "view_timezone" options without configuring a reference date is not supported.');
339339
}

‎Extension/Core/Type/TimezoneType.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public function configureOptions(OptionsResolver $resolver)
5454

5555
$choiceTranslationLocale = $options['choice_translation_locale'];
5656

57-
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(fn () => self::getIntlTimezones($input, $choiceTranslationLocale)), [$input, $choiceTranslationLocale]);
57+
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(static fn () => self::getIntlTimezones($input, $choiceTranslationLocale)), [$input, $choiceTranslationLocale]);
5858
}
5959

60-
return ChoiceList::lazy($this, fn () => self::getPhpTimezones($input), $input);
60+
return ChoiceList::lazy($this, static fn () => self::getPhpTimezones($input), $input);
6161
},
6262
'choice_translation_domain' => false,
6363
'choice_translation_locale' => null,
@@ -69,7 +69,7 @@ public function configureOptions(OptionsResolver $resolver)
6969
$resolver->setAllowedTypes('intl', ['bool']);
7070

7171
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
72-
$resolver->setNormalizer('choice_translation_locale', function (Options $options, $value) {
72+
$resolver->setNormalizer('choice_translation_locale', static function (Options $options, $value) {
7373
if (null !== $value && !$options['intl']) {
7474
throw new LogicException('The "choice_translation_locale" option can only be used if the "intl" option is set to true.');
7575
}
@@ -78,7 +78,7 @@ public function configureOptions(OptionsResolver $resolver)
7878
});
7979

8080
$resolver->setAllowedValues('input', ['string', 'datetimezone', 'intltimezone']);
81-
$resolver->setNormalizer('input', function (Options $options, $value) {
81+
$resolver->setNormalizer('input', static function (Options $options, $value) {
8282
if ('intltimezone' === $value && !class_exists(\IntlTimeZone::class)) {
8383
throw new LogicException('Cannot use "intltimezone" input because the PHP intl extension is not available.');
8484
}

‎Extension/Core/Type/WeekType.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public function buildView(FormView $view, FormInterface $form, array $options)
100100
*/
101101
public function configureOptions(OptionsResolver $resolver)
102102
{
103-
$compound = fn (Options $options) => 'single_text' !== $options['widget'];
103+
$compound = static fn (Options $options) => 'single_text' !== $options['widget'];
104104

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

107-
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
107+
$placeholderNormalizer = static function (Options $options, $placeholder) use ($placeholderDefault) {
108108
if (\is_array($placeholder)) {
109109
$default = $placeholderDefault($options);
110110

@@ -120,7 +120,7 @@ public function configureOptions(OptionsResolver $resolver)
120120
];
121121
};
122122

123-
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
123+
$choiceTranslationDomainNormalizer = static function (Options $options, $choiceTranslationDomain) {
124124
if (\is_array($choiceTranslationDomain)) {
125125
$default = false;
126126

@@ -144,15 +144,15 @@ public function configureOptions(OptionsResolver $resolver)
144144
'placeholder' => $placeholderDefault,
145145
'html5' => static fn (Options $options) => 'single_text' === $options['widget'],
146146
'error_bubbling' => false,
147-
'empty_data' => fn (Options $options) => $options['compound'] ? [] : '',
147+
'empty_data' => static fn (Options $options) => $options['compound'] ? [] : '',
148148
'compound' => $compound,
149149
'choice_translation_domain' => false,
150150
'invalid_message' => 'Please enter a valid week.',
151151
]);
152152

153153
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
154154
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
155-
$resolver->setNormalizer('html5', function (Options $options, $html5) {
155+
$resolver->setNormalizer('html5', static function (Options $options, $html5) {
156156
if ($html5 && 'single_text' !== $options['widget']) {
157157
throw new LogicException(sprintf('The "widget" option of "%s" must be set to "single_text" when the "html5" option is enabled.', self::class));
158158
}

‎Extension/DataCollector/FormDataCollector.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function __sleep(): array
219219
protected function getCasters(): array
220220
{
221221
return parent::getCasters() + [
222-
\Exception::class => function (\Exception $e, array $a, Stub $s) {
222+
\Exception::class => static function (\Exception $e, array $a, Stub $s) {
223223
foreach (["\0Exception\0previous", "\0Exception\0trace"] as $k) {
224224
if (isset($a[$k])) {
225225
unset($a[$k]);
@@ -229,12 +229,12 @@ protected function getCasters(): array
229229

230230
return $a;
231231
},
232-
FormInterface::class => fn (FormInterface $f, array $a) => [
232+
FormInterface::class => static fn (FormInterface $f, array $a) => [
233233
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
234234
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub($f->getConfig()->getType()->getInnerType()::class),
235235
],
236236
FormView::class => StubCaster::cutInternals(...),
237-
ConstraintViolationInterface::class => fn (ConstraintViolationInterface $v, array $a) => [
237+
ConstraintViolationInterface::class => static fn (ConstraintViolationInterface $v, array $a) => [
238238
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
239239
Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
240240
Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),

‎Extension/Validator/Type/BaseValidatorExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class BaseValidatorExtension extends AbstractTypeExtension
3030
public function configureOptions(OptionsResolver $resolver)
3131
{
3232
// Make sure that validation groups end up as null, closure or array
33-
$validationGroupsNormalizer = function (Options $options, $groups) {
33+
$validationGroupsNormalizer = static function (Options $options, $groups) {
3434
if (false === $groups) {
3535
return [];
3636
}

‎Extension/Validator/Type/FormTypeValidatorExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function configureOptions(OptionsResolver $resolver)
5353
parent::configureOptions($resolver);
5454

5555
// Constraint should always be converted to an array
56-
$constraintsNormalizer = fn (Options $options, $constraints) => \is_object($constraints) ? [$constraints] : (array) $constraints;
56+
$constraintsNormalizer = static fn (Options $options, $constraints) => \is_object($constraints) ? [$constraints] : (array) $constraints;
5757

5858
$resolver->setDefaults([
5959
'error_mapping' => [],

‎Extension/Validator/Type/RepeatedTypeValidatorExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RepeatedTypeValidatorExtension extends AbstractTypeExtension
2727
public function configureOptions(OptionsResolver $resolver)
2828
{
2929
// Map errors to the first field
30-
$errorMapping = fn (Options $options) => ['.' => $options['first_name']];
30+
$errorMapping = static fn (Options $options) => ['.' => $options['first_name']];
3131

3232
$resolver->setDefaults([
3333
'error_mapping' => $errorMapping,

‎Extension/Validator/Type/UploadValidatorExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function configureOptions(OptionsResolver $resolver)
3939
{
4040
$translator = $this->translator;
4141
$translationDomain = $this->translationDomain;
42-
$resolver->setNormalizer('upload_max_size_message', fn (Options $options, $message) => fn () => $translator->trans($message(), [], $translationDomain));
42+
$resolver->setNormalizer('upload_max_size_message', static fn (Options $options, $message) => static fn () => $translator->trans($message(), [], $translationDomain));
4343
}
4444

4545
public static function getExtendedTypes(): iterable

‎Extension/Validator/ValidatorTypeGuesser.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,24 @@ public function __construct(MetadataFactoryInterface $metadataFactory)
6666

6767
public function guessType(string $class, string $property): ?TypeGuess
6868
{
69-
return $this->guess($class, $property, fn (Constraint $constraint) => $this->guessTypeForConstraint($constraint));
69+
return $this->guess($class, $property, $this->guessTypeForConstraint(...));
7070
}
7171

7272
public function guessRequired(string $class, string $property): ?ValueGuess
7373
{
74-
return $this->guess($class, $property, function (Constraint $constraint) {
75-
return $this->guessRequiredForConstraint($constraint);
76-
// If we don't find any constraint telling otherwise, we can assume
77-
// that a field is not required (with LOW_CONFIDENCE)
78-
}, false);
74+
// If we don't find any constraint telling otherwise, we can assume
75+
// that a field is not required (with LOW_CONFIDENCE)
76+
return $this->guess($class, $property, $this->guessRequiredForConstraint(...), false);
7977
}
8078

8179
public function guessMaxLength(string $class, string $property): ?ValueGuess
8280
{
83-
return $this->guess($class, $property, fn (Constraint $constraint) => $this->guessMaxLengthForConstraint($constraint));
81+
return $this->guess($class, $property, $this->guessMaxLengthForConstraint(...));
8482
}
8583

8684
public function guessPattern(string $class, string $property): ?ValueGuess
8785
{
88-
return $this->guess($class, $property, fn (Constraint $constraint) => $this->guessPatternForConstraint($constraint));
86+
return $this->guess($class, $property, $this->guessPatternForConstraint(...));
8987
}
9088

9189
/**

‎FormTypeGuesserChain.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ public function __construct(iterable $guessers)
4545

4646
public function guessType(string $class, string $property): ?TypeGuess
4747
{
48-
return $this->guess(fn ($guesser) => $guesser->guessType($class, $property));
48+
return $this->guess(static fn ($guesser) => $guesser->guessType($class, $property));
4949
}
5050

5151
public function guessRequired(string $class, string $property): ?ValueGuess
5252
{
53-
return $this->guess(fn ($guesser) => $guesser->guessRequired($class, $property));
53+
return $this->guess(static fn ($guesser) => $guesser->guessRequired($class, $property));
5454
}
5555

5656
public function guessMaxLength(string $class, string $property): ?ValueGuess
5757
{
58-
return $this->guess(fn ($guesser) => $guesser->guessMaxLength($class, $property));
58+
return $this->guess(static fn ($guesser) => $guesser->guessMaxLength($class, $property));
5959
}
6060

6161
public function guessPattern(string $class, string $property): ?ValueGuess
6262
{
63-
return $this->guess(fn ($guesser) => $guesser->guessPattern($class, $property));
63+
return $this->guess(static fn ($guesser) => $guesser->guessPattern($class, $property));
6464
}
6565

6666
/**

‎Tests/Fixtures/Descriptor/default_option_with_normalizer.txt

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (choice_translation_domain
2020
Normalizers [ %s
2121
Closure(%s
2222
class:%s
23-
this: %s
2423
file: %s
2524
line: %s
2625
} %s

0 commit comments

Comments
 (0)
Please sign in to comment.