8000 minor #41998 [Form] add union types (nicolas-grekas) · symfony/symfony@fa99245 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa99245

Browse files
minor #41998 [Form] add union types (nicolas-grekas)
This PR was squashed before being merged into the 6.0 branch. Discussion ---------- [Form] add union types | Q | A | ------------- | --- | Branch? | 6.0 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Part of #41424 | License | MIT | Doc PR | - Commits ------- 0480be6 [Form] add union types
2 parents 5bd3024 + 0480be6 commit fa99245

File tree

80 files changed

+271
-360
lines changed

Some content is hidden

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

80 files changed

+271
-360
lines changed

src/Symfony/Component/Form/AbstractRendererEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(array $defaultThemes = [])
4444
/**
4545
* {@inheritdoc}
4646
*/
47-
public function setTheme(FormView $view, $themes, bool $useDefaultThemes = true)
47+
public function setTheme(FormView $view, mixed $themes, bool $useDefaultThemes = true)
4848
{
4949
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
5050

src/Symfony/Component/Form/Button.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ public function __construct(FormConfigInterface $config)
4747
/**
4848
* Unsupported method.
4949
*
50-
* @param mixed $offset
51-
*
5250
* @return bool Always returns false
5351
*/
54-
public function offsetExists($offset)
52+
public function offsetExists(mixed $offset)
5553
{
5654
return false;
5755
}
@@ -61,11 +59,9 @@ public function offsetExists($offset)
6159
*
6260
* This method should not be invoked.
6361
*
64-
* @param mixed $offset
65-
*
6662
* @throws BadMethodCallException
6763
*/
68-
public function offsetGet($offset)
64+
public function offsetGet(mixed $offset)
6965
{
7066
throw new BadMethodCallException('Buttons cannot have children.');
7167
}
@@ -75,12 +71,9 @@ public function offsetGet($offset)
7571
*
7672
* This method should not be invoked.
7773
*
78-
* @param mixed $offset
79-
* @param mixed $value
80-
*
8174
* @throws BadMethodCallException
8275
*/
83-
public function offsetSet($offset, $value)
76+
public function offsetSet(mixed $offset, mixed $value)
8477
{
8578
throw new BadMethodCallException('Buttons cannot have children.');
8679
}
@@ -90,11 +83,9 @@ public function offsetSet($offset, $value)
9083
*
9184
* This method should not be invoked.
9285
*
93-
* @param mixed $offset
94-
*
9586
* @throws BadMethodCallException
9687
*/
97-
public function offsetUnset($offset)
88+
public function offsetUnset(mixed $offset)
9889
{
9990
throw new BadMethodCallException('Buttons cannot have children.');
10091
}
@@ -128,7 +119,7 @@ public function getParent()
128119
*
129120
* @throws BadMethodCallException
130121
*/
131-
public function add($child, string $type = null, array $options = [])
122+
public function add(string|FormInterface $child, string $type = null, array $options = [])
132123
{
133124
throw new BadMethodCallException('Buttons cannot have children.');
134125
}
@@ -188,11 +179,9 @@ public function getErrors(bool $deep = false, bool $flatten = true)
188179
*
189180
* This method should not be invoked.
190181
*
191-
* @param mixed $modelData
192-
*
193182
* @return $this
194183
*/
195-
public function setData($modelData)
184+
public function setData(mixed $modelData)
196185
{
197186
// no-op, called during initialization of the form tree
198187
return $this;
@@ -353,26 +342,21 @@ public function initialize()
353342
/**
354343
* Unsupported method.
355344
*
356-
* @param mixed $request
357-
*
358345
* @throws BadMethodCallException
359346
*/
360-
public function handleRequest($request = null)
347+
public function handleRequest(mixed $request = null)
361348
{
362349
throw new BadMethodCallException('Buttons cannot handle requests. Call handleRequest() on the root form instead.');
363350
}
364351

365352
/**
366353
* Submits data to the button.
367354
*
368-
* @param string|null $submittedData Not used
369-
* @param bool $clearMissing Not used
370-
*
371355
* @return $this
372356
*
373357
* @throws Exception\AlreadySubmittedException if the button has already been submitted
374358
*/
375-
public function submit($submittedData, bool $clearMissing = true)
359+
public function submit(array|string|null $submittedData, bool $clearMissing = true)
376360
{
377361
if ($this->submitted) {
378362
throw new AlreadySubmittedException('A form can only be submitted once.');

src/Symfony/Component/Form/ButtonBuilder.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1515
use Symfony\Component\Form\Exception\BadMethodCallException;
1616
use Symfony\Component\Form\Exception\InvalidArgumentException;
17+
use Symfony\Component\PropertyAccess\PropertyPathInterface;
1718

1819
/**
1920
* A builder for {@link Button} instances.
@@ -71,7 +72,7 @@ public function __construct(?string $name, array $options = [])
7172
*
7273
* @throws BadMethodCallException
7374
*/
74-
public function add($child, string $type = null, array $options = [])
75+
public function add(string|FormBuilderInterface $child, string $type = null, array $options = [])
7576
{
7677
throw new BadMethodCallException('Buttons cannot have children.');
7778
}
@@ -217,7 +218,7 @@ public function resetModelTransformers()
217218
/**
218219
* {@inheritdoc}
219220
*/
220-
public function setAttribute(string $name, $value)
221+
public function setAttribute(string $name, mixed $value)
221222
{
222223
$this->attributes[$name] = $value;
223224

@@ -263,11 +264,9 @@ public function setDisabled(bool $disabled)
263264
*
264265
* This method should not be invoked.
265266
*
266-
* @param mixed $emptyData
267-
*
268267
* @throws BadMethodCallException
269268
*/
270-
public function setEmptyData($emptyData)
269+
public function setEmptyData(mixed $emptyData)
271270
{
272271
throw new BadMethodCallException('Buttons do not support empty data.');
273272
}
@@ -301,11 +300,9 @@ public function setRequired(bool $required)
301300
*
302301
* This method should not be invoked.
303302
*
304-
* @param null $propertyPath
305-
*
306303
* @throws BadMethodCallException
307304
*/
308-
public function setPropertyPath($propertyPath)
305+
public function setPropertyPath(string|PropertyPathInterface|null $propertyPath)
309306
{
310307
throw new BadMethodCallException('Buttons do not support property paths.');
311308
}
@@ -363,11 +360,9 @@ public function setType(ResolvedFormTypeInterface $type)
363360
*
364361
* This method should not be invoked.
365362
*
366-
* @param mixed $data
367-
*
368363
* @throws BadMethodCallException
369364
*/
370-
public function setData($data)
365+
public function setData(mixed $data)
371366
{
372367
throw new BadMethodCallException('Buttons do not support data.');
373368
}
@@ -629,11 +624,9 @@ public function hasAttribute(string $name)
629624
/**
630625
* Returns the value of the given attribute.
631626
*
632-
* @param mixed $default The value returned if the attribute does not exist
633-
*
634627
* @return mixed The attribute value
635628
*/
636-
public function getAttribute(string $name, $default = null)
629+
public function getAttribute(string $name, mixed $default = null)
637630
{
638631
return \array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
639632
}
@@ -739,11 +732,9 @@ public function hasOption(string $name)
739732
/**
740733
* Returns the value of a specific option.
741734
*
742-
* @param mixed $default The value returned if the option does not exist
743-
*
744735
* @return mixed The option value
745736
*/
746-
public function getOption(string $name, $default = null)
737+
public function getOption(string $name, mixed $default = null)
747738
{
748739
return \array_key_exists($name, $this->options) ? $this->options[$name] : $default;
749740
}

src/Symfony/Component/Form/CallbackTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ public function __construct(callable $transform, callable $reverseTransform)
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function transform($data)
32+
public function transform(mixed $data)
3333
{
3434
return ($this->transform)($data);
3535
}
3636

3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function reverseTransform($data)
40+
public function reverseTransform(mixed $data)
4141
{
4242
return ($this->reverseTransform)($data);
4343
}

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

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,117 +35,107 @@ final class ChoiceList
3535
/**
3636
* Creates a cacheable loader from any callable providing iterable choices.
3737
*
38-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
39-
* @param callable $choices A callable that must return iterable choices or grouped choices
40-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
38+
* @param callable $choices A callable that must return iterable choices or grouped choices
39+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the loader
4140
*/
42-
public static function lazy($formType, callable $choices, $vary = null): ChoiceLoader
41+
public static function lazy(FormTypeInterface|FormTypeExtensionInterface $formType, callable $choices, mixed $vary = null): ChoiceLoader
4342
{
4443
return self::loader($formType, new CallbackChoiceLoader($choices), $vary);
4544
}
4645

4746
/**
4847
* Decorates a loader to make it cacheable.
4948
*
50-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
51-
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
52-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
49+
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
50+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the loader
5351
*/
54-
public static function loader($formType, ChoiceLoaderInterface $loader, $vary = null): ChoiceLoader
52+
public static function loader(FormTypeInterface|FormTypeExtensionInterface $formType, ChoiceLoaderInterface $loader, mixed $vary = null): ChoiceLoader
5553
{
5654
return new ChoiceLoader($formType, $loader, $vary);
5755
}
5856

5957
/**
6058
* Decorates a "choice_value" callback to make it cacheable.
6159
*
62-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
63-
* @param callable $value Any pseudo callable to create a unique string value from a choice
64-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
60+
* @param callable|array $value Any pseudo callable to create a unique string value from a choice
61+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
6562
*/
66-
public static function value($formType, $value, $vary = null): ChoiceValue
63+
public static function value(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $value, mixed $vary = null): ChoiceValue
6764
{
6865
return new ChoiceValue($formType, $value, $vary);
6966
}
7067

7168
/**
72-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
73-
* @param callable $filter Any pseudo callable to filter a choice list
74-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
69+
* @param callable|array $filter Any pseudo callable to filter a choice list
70+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
7571
*/
76-
public static function filter($formType, $filter, $vary = null): ChoiceFilter
72+
public static function filter(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $filter, mixed $vary = null): ChoiceFilter
7773
{
7874
return new ChoiceFilter($formType, $filter, $vary);
7975
}
8076

8177
/**
8278
* Decorates a "choice_label" option to make it cacheable.
8379
*
84-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
85-
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
86-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
80+
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
81+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
8782
*/
88-
public static function label($formType, $label, $vary = null): ChoiceLabel
83+
public static function label(FormTypeInterface|FormTypeExtensionInterface $formType, callable|false $label, mixed $vary = null): ChoiceLabel
8984
{
9085
return new ChoiceLabel($formType, $label, $vary);
9186
}
9287

9388
/**
9489
* Decorates a "choice_name" callback to make it cacheable.
9590
*
96-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
97-
* @param callable $fieldName Any pseudo callable to create a field name from a choice
98-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
91+
* @param callable|array $fieldName Any pseudo callable to create a field name from a choice
92+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
9993
*/
100-
public static function fieldName($formType, $fieldName, $vary = null): ChoiceFieldName
94+
public static function fieldName(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $fieldName, mixed $vary = null): ChoiceFieldName
10195
{
10296
return new ChoiceFieldName($formType, $fieldName, $vary);
10397
}
10498

10599
/**
106100
* Decorates a "choice_attr" option to make it cacheable.
107101
*
108-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
109-
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
110-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
102+
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
103+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
111104
*/
112-
public static function attr($formType, $attr, $vary = null): ChoiceAttr
105+
public static function attr(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $attr, mixed $vary = null): ChoiceAttr
113106
{
114107
return new ChoiceAttr($formType, $attr, $vary);
115108
}
116109

117110
/**
118111
* Decorates a "choice_translation_parameters" option to make it cacheable.
119112
*
120-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
121-
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
122-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
113+
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
114+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
123115
*/
124-
public static function translationParameters($formType, $translationParameters, $vary = null): ChoiceTranslationParameters
116+
public static function translationParameters(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $translationParameters, mixed $vary = null): ChoiceTranslationParameters
125117
{
126118
return new ChoiceTranslationParameters($formType, $translationParameters, $vary);
127119
}
128120

129121
/**
130122
* Decorates a "group_by" callback to make it cacheable.
131123
*
132-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
133-
* @param callable $groupBy Any pseudo callable to return a group name from a choice
134-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
124+
* @param callable|array $groupBy Any pseudo callable to return a group name from a choice
125+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
135126
*/
136-
public static function groupBy($formType, $groupBy, $vary = null): GroupBy
127+
public static function groupBy(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $groupBy, mixed $vary = null): GroupBy
137128
{
138129
return new GroupBy($formType, $groupBy, $vary);
139130
}
140131

141132
/**
142133
* Decorates a "preferred_choices" option to make it cacheable.
143134
*
144-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
145-
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
146-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
135+
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
136+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
147137
*/
148-
public static function preferred($formType, $preferred, $vary = null): PreferredChoice
138+
public static function preferred(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $preferred, mixed $vary = null): PreferredChoice
149139
{
150140
return new PreferredChoice($formType, $preferred, $vary);
151141
}

src/Symfony/Component/Form/ChoiceList/Factory/Cache/AbstractStaticOption.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,11 @@ abstract class AbstractStaticOption
3434
private $option;
3535

3636
/**
37-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
38-
* @param mixed $option Any pseudo callable, array, string or bool to define a choice list option
39-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
37+
* @param mixed $option Any pseudo callable, array, string or bool to define a choice list option
38+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
4039
*/
41-
final public function __construct($formType, $option, $vary = null)
40+
final public function __construct(FormTypeInterface|FormTypeExtensionInterface $formType, mixed $option, mixed $vary = null)
4241
{
43-
if (!$formType instanceof FormTypeInterface && !$formType instanceof FormTypeExtensionInterface) {
44-
throw new \TypeError(sprintf('Expected an instance of "%s" or "%s", but got "%s".', FormTypeInterface::class, FormTypeExtensionInterface::class, get_debug_type($formType)));
45-
}
46-
4742
$hash = CachingFactoryDecorator::generateHash([static::class, $formType, $vary]);
4843

4944
$this->option = self::$options[$hash] ?? self::$options[$hash] = $option;

0 commit comments

Comments
 (0)
0