8000 [Form] add union types by nicolas-grekas · Pull Request #41998 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] add union types #41998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/AbstractRendererEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(array $defaultThemes = [])
/**
* {@inheritdoc}
*/
public function setTheme(FormView $view, $themes, bool $useDefaultThemes = true)
public function setTheme(FormView $view, mixed $themes, bool $useDefaultThemes = true)
{
$cacheKey = $view->vars[self::CACHE_KEY_VAR];

Expand Down
32 changes: 8 additions & 24 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ public function __construct(FormConfigInterface $config)
/**
* Unsupported method.
*
* @param mixed $offset
*
* @return bool Always returns false
*/
public function offsetExists($offset)
public function offsetExists(mixed $offset)
{
return false;
}
Expand All @@ -61,11 +59,9 @@ public function offsetExists($offset)
*
* This method should not be invoked.
*
* @param mixed $offset
*
* @throws BadMethodCallException
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset)
{
throw new BadMethodCallException('Buttons cannot have children.');
}
Expand All @@ -75,12 +71,9 @@ public function offsetGet($offset)
*
* This method should not be invoked.
*
* @param mixed $offset
* @param mixed $value
*
* @throws BadMethodCallException
*/
public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value)
{
throw new BadMethodCallException('Buttons cannot have children.');
}
Expand All @@ -90,11 +83,9 @@ public function offsetSet($offset, $value)
*
* This method should not be invoked.
*
* @param mixed $offset
*
* @throws BadMethodCallException
*/
public function offsetUnset($offset)
public function offsetUnset(mixed $offset)
{
throw new BadMethodCallException('Buttons cannot have children.');
}
Expand Down Expand Up @@ -128,7 +119,7 @@ public function getParent()
*
* @throws BadMethodCallException
*/
public function add($child, string $type = null, array $options = [])
public function add(string|FormInterface $child, string $type = null, array $options = [])
{
throw new BadMethodCallException('Buttons cannot have children.');
}
Expand Down Expand Up @@ -188,11 +179,9 @@ public function getErrors(bool $deep = false, bool $flatten = true)
*
* This method should not be invoked.
*
* @param mixed $modelData
*
* @return $this
*/
public function setData($modelData)
public function setData(mixed $modelData)
{
// no-op, called during initialization of the form tree
return $this;
Expand Down Expand Up @@ -353,26 +342,21 @@ public function initialize()
/**
* Unsupported method.
*
* @param mixed $request
*
* @throws BadMethodCallException
*/
public function handleRequest($request = null)
public function handleRequest(mixed $request = null)
{
throw new BadMethodCallException('Buttons cannot handle requests. Call handleRequest() on the root form instead.');
}

/**
* Submits data to the button.
*
* @param string|null $submittedData Not used
* @param bool $clearMissing Not used
*
* @return $this
*
* @throws Exception\AlreadySubmittedException if the button has already been submitted
*/
public function submit($submittedData, bool $clearMissing = true)
public function submit(array|string|null $submittedData, bool $clearMissing = true)
{
if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once.');
Expand Down
25 changes: 8 additions & 17 deletions src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\PropertyAccess\PropertyPathInterface;

/**
* A builder for {@link Button} instances.
Expand Down Expand Up @@ -71,7 +72,7 @@ public function __construct(?string $name, array $options = [])
*
* @throws BadMethodCallException
*/
public function add($child, string $type = null, array $options = [])
public function add(string|FormBuilderInterface $child, string $type = null, array $options = [])
{
throw new BadMethodCallException('Buttons cannot have children.');
}
Expand Down Expand Up @@ -217,7 +218,7 @@ public function resetModelTransformers()
/**
* {@inheritdoc}
*/
public function setAttribute(string $name, $value)
public function setAttribute(string $name, mixed $value)
{
$this->attributes[$name] = $value;

Expand Down Expand Up @@ -263,11 +264,9 @@ public function setDisabled(bool $disabled)
*
* This method should not be invoked.
*
* @param mixed $emptyData
*
* @throws BadMethodCallException
*/
public function setEmptyData($emptyData)
public function setEmptyData(mixed $emptyData)
{
throw new BadMethodC F42D allException('Buttons do not support empty data.');
}
Expand Down Expand Up @@ -301,11 +300,9 @@ public function setRequired(bool $required)
*
* This method should not be invoked.
*
* @param null $propertyPath
*
* @throws BadMethodCallException
*/
public function setPropertyPath($propertyPath)
public function setPropertyPath(string|PropertyPathInterface|null $propertyPath)
{
throw new BadMethodCallException('Buttons do not support property paths.');
}
Expand Down Expand Up @@ -363,11 +360,9 @@ public function setType(ResolvedFormTypeInterface $type)
*
* This method should not be invoked.
*
* @param mixed $data
*
* @throws BadMethodCallException
*/
public function setData($data)
public function setData(mixed $data)
{
throw new BadMethodCallException('Buttons do not support data.');
}
Expand Down Expand Up @@ -629,11 +624,9 @@ public function hasAttribute(string $name)
/**
* Returns the value of the given attribute.
*
* @param mixed $default The value returned if the attribute does not exist
*
* @return mixed The attribute value
*/
public function getAttribute(string $name, $default = null)
public function getAttribute(string $name, mixed $default = null)
{
return \array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
}
Expand Down Expand Up @@ -739,11 +732,9 @@ public function hasOption(string $name)
/**
* Returns the value of a specific option.
*
* @param mixed $default The value returned if the option does not exist
*
* @return mixed The option value
*/
public function getOption(string $name, $default = null)
public function getOption(string $name, mixed $default = null)
{
return \array_key_exists($name, $this->options) ? $this->options[$name] : $default;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/CallbackTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public function __construct(callable $transform, callable $reverseTransform)
/**
* {@inheritdoc}
*/
public function transform($data)
public function transform(mixed $data)
{
return ($this->transform)($data);
}

/**
* {@inheritdoc}
*/
public function reverseTransform($data)
public function reverseTransform(mixed $data)
{
return ($this->reverseTransform)($data);
}
Expand Down
70 changes: 30 additions & 40 deletions src/Symfony/Component/Form/ChoiceList/ChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,117 +35,107 @@ final class ChoiceList
/**
* Creates a cacheable loader from any callable providing iterable choices.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable $choices A callable that must return iterable choices or grouped choices
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
* @param callable $choices A callable that must return iterable choices or grouped choices
* @param mixed $vary Dynamic data used to compute a unique hash when caching the loader
*/
public static function lazy($formType, callable $choices, $vary = null): ChoiceLoader
public static function lazy(FormTypeInterface|FormTypeExtensionInterface $formType, callable $choices, mixed $vary = null): ChoiceLoader
{
return self::loader($formType, new CallbackChoiceLoader($choices), $vary);
}

/**
* Decorates a loader to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
* @param mixed $vary Dynamic data used to compute a unique hash when caching the loader
*/
public static function loader($formType, ChoiceLoaderInterface $loader, $vary = null): ChoiceLoader
public static function loader(FormTypeInterface|FormTypeExtensionInterface $formType, ChoiceLoaderInterface $loader, mixed $vary = null): ChoiceLoader
{
return new ChoiceLoader($formType, $loader, $vary);
}

/**
* Decorates a "choice_value" callback to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable $value Any pseudo callable to create a unique string value from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
* @param callable|array $value Any pseudo callable to create a unique string value from a choice
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
*/
public static function value($formType, $value, $vary = null): ChoiceValue
public static function value(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $value, mixed $vary = null): ChoiceValue
{
return new ChoiceValue($formType, $value, $vary);
}

/**
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable $filter Any pseudo callable to filter a choice list
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
* @param callable|array $filter Any pseudo callable to filter a choice list
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
*/
public static function filter($formType, $filter, $vary = null): ChoiceFilter
public static function filter(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $filter, mixed $vary = null): ChoiceFilter
{
return new ChoiceFilter($formType, $filter, $vary);
}

/**
* Decorates a "choice_label" option to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
*/
public static function label($formType, $label, $vary = null): ChoiceLabel
public static function label(FormTypeInterface|FormTypeExtensionInterface $formType, callable|false $label, mixed $vary = null): ChoiceLabel
{
return new ChoiceLabel($formType, $label, $vary);
}

/**
* Decorates a "choice_name" callback to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable $fieldName Any pseudo callable to create a field name from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
* @param callable|array $fieldName Any pseudo callable to create a field name from a choice
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
*/
public static function fieldName($formType, $fieldName, $vary = null): ChoiceFieldName
public static function fieldName(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $fieldName, mixed $vary = null): ChoiceFieldName
{
return new ChoiceFieldName($formType, $fieldName, $vary);
}

/**
* Decorates a "choice_attr" option to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
*/
public static function attr($formType, $attr, $vary = null): ChoiceAttr
public static function attr(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $attr, mixed $vary = null): ChoiceAttr
{
return new ChoiceAttr($formType, $attr, $vary);
}

/**
* Decorates a "choice_translation_parameters" option to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
*/
public static function translationParameters($formType, $translationParameters, $vary = null): ChoiceTranslationParameters
public static function translationParameters(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $translationParameters, mixed $vary = null): ChoiceTranslationParameters
{
return new ChoiceTranslationParameters($formType, $translationParameters, $vary);
}

/**
* Decorates a "group_by" callback to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable $groupBy Any pseudo callable to return a group name from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
* @param callable|array $groupBy Any pseudo callable to return a group name from a choice
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
*/
public static function groupBy($formType, $groupBy, $vary = null): GroupBy
public static function groupBy(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $groupBy, mixed $vary = null): GroupBy
{
return new GroupBy($formType, $groupBy, $vary);
}

/**
* Decorates a "preferred_choices" option to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
*/
public static function preferred($formType, $preferred, $vary = null): PreferredChoice
public static function preferred(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $preferred, mixed $vary = null): PreferredChoice
{
return new PreferredChoice($formType, $preferred, $vary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ abstract class AbstractStaticOption
private $option;

/**
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param mixed $option Any pseudo callable, array, string or bool to define a choice list option
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
* @param mixed $option Any pseudo callable, array, string or bool to define a choice list option
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
*/
final public function __construct($formType, $option, $vary = null)
final public function __construct(FormTypeInterface|FormTypeExtensionInterface $formType, mixed $option, mixed $vary = null)
{
if (!$formType instanceof FormTypeInterface && !$formType instanceof FormTypeExtensionInterface) {
throw new \TypeError(sprintf('Expected an instance of "%s" or "%s", but got "%s".', FormTypeInterface::class, FormTypeExtensionInterface::class, get_debug_type($formType)));
}

$hash = CachingFactoryDecorator::generateHash([static::class, $formType, $vary]);

$this->option = self::$options[$hash] ?? self::$options[$hash] = $option;
Expand Down
Loading
0