From 289a7680ffd44b4747aafc2e9c43085c5d8c285a Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 15 Jul 2021 00:16:54 +0200 Subject: [PATCH] [Form] Backport type fixes Signed-off-by: Alexander M. Turek --- .../Component/Form/AbstractExtension.php | 10 ++--- .../Component/Form/AbstractRendererEngine.php | 20 +++++++++- src/Symfony/Component/Form/Button.php | 2 +- .../BaseDateTimeTransformer.php | 4 +- .../DateIntervalToArrayTransformer.php | 9 ++--- .../DateTimeToArrayTransformer.php | 15 +++---- .../DateTimeToLocalizedStringTransformer.php | 14 +++---- .../DateTimeToStringTransformer.php | 6 +-- .../MoneyToLocalizedStringTransformer.php | 16 +------- .../NumberToLocalizedStringTransformer.php | 12 +----- .../PercentToLocalizedStringTransformer.php | 9 +---- .../Extension/Core/Type/DateIntervalType.php | 13 +++--- .../Form/Extension/Core/Type/DateType.php | 4 +- .../Form/Extension/Core/Type/TimeType.php | 4 +- .../Csrf/Type/FormTypeCsrfExtension.php | 3 +- src/Symfony/Component/Form/Form.php | 25 ++++++++---- src/Symfony/Component/Form/FormBuilder.php | 3 +- src/Symfony/Component/Form/FormFactory.php | 14 ++++--- .../Component/Form/FormFactoryBuilder.php | 2 +- .../Component/Form/FormFactoryInterface.php | 23 ++++++----- src/Symfony/Component/Form/FormRegistry.php | 8 ++-- .../Component/Form/FormTypeGuesserChain.php | 2 +- src/Symfony/Component/Form/Guess/Guess.php | 2 - .../Component/Form/ResolvedFormType.php | 19 ++++----- .../Form/Tests/ResolvedFormTypeTest.php | 40 +++++++------------ 25 files changed, 131 insertions(+), 148 deletions(-) diff --git a/src/Symfony/Component/Form/AbstractExtension.php b/src/Symfony/Component/Form/AbstractExtension.php index 3e82f7a451..411e86db21 100644 --- a/src/Symfony/Component/Form/AbstractExtension.php +++ b/src/Symfony/Component/Form/AbstractExtension.php @@ -22,14 +22,14 @@ abstract class AbstractExtension implements FormExtensionInterface /** * The types provided by this extension. * - * @var FormTypeInterface[] An array of FormTypeInterface + * @var FormTypeInterface[] */ private $types; /** * The type extensions provided by this extension. * - * @var FormTypeExtensionInterface[] An array of FormTypeExtensionInterface + * @var FormTypeExtensionInterface[][] */ private $typeExtensions; @@ -153,7 +153,7 @@ abstract class AbstractExtension implements FormExtensionInterface foreach ($this->loadTypes() as $type) { if (!$type instanceof FormTypeInterface) { - throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface'); + throw new UnexpectedTypeException($type, FormTypeInterface::class); } $this->types[\get_class($type)] = $type; @@ -172,7 +172,7 @@ abstract class AbstractExtension implements FormExtensionInterface foreach ($this->loadTypeExtensions() as $extension) { if (!$extension instanceof FormTypeExtensionInterface) { - throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface'); + throw new UnexpectedTypeException($extension, FormTypeExtensionInterface::class); } if (method_exists($extension, 'getExtendedTypes')) { @@ -204,7 +204,7 @@ abstract class AbstractExtension implements FormExtensionInterface $this->typeGuesser = $this->loadTypeGuesser(); if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) { - throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface'); + throw new UnexpectedTypeException($this->typeGuesser, FormTypeGuesserInterface::class); } } } diff --git a/src/Symfony/Component/Form/AbstractRendererEngine.php b/src/Symfony/Component/Form/AbstractRendererEngine.php index 7d2e4c8a1f..116892d415 100644 --- a/src/Symfony/Component/Form/AbstractRendererEngine.php +++ b/src/Symfony/Component/Form/AbstractRendererEngine.php @@ -23,11 +23,29 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface */ public const CACHE_KEY_VAR = 'cache_key'; + /** + * @var array + */ protected $defaultThemes; + + /** + * @var array[] + */ protected $themes = []; + + /** + * @var bool[] + */ protected $useDefaultThemes = []; + + /** + * @var array[] + */ protected $resources = []; + /** + * @var array> + */ private $resourceHierarchyLevels = []; /** @@ -127,7 +145,7 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface * * @see getResourceForBlockHierarchy() */ - private function loadResourceForBlockNameHierarchy(string $cacheKey, FormView $view, array $blockNameHierarchy, $hierarchyLevel): bool + private function loadResourceForBlockNameHierarchy(string $cacheKey, FormView $view, array $blockNameHierarchy, int $hierarchyLevel): bool { $blockName = $blockNameHierarchy[$hierarchyLevel]; diff --git a/src/Symfony/Component/Form/Button.php b/src/Symfony/Component/Form/Button.php index aa981a0125..16b999d4bf 100644 --- a/src/Symfony/Component/Form/Button.php +++ b/src/Symfony/Component/Form/Button.php @@ -22,7 +22,7 @@ use Symfony\Component\Form\Exception\BadMethodCallException; class Button implements \IteratorAggregate, FormInterface { /** - * @var FormInterface + * @var FormInterface|null */ private $parent; diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php index 4727c42e54..142f4894e1 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php @@ -29,8 +29,8 @@ abstract class BaseDateTimeTransformer implements DataTransformerInterface protected $outputTimezone; /** - * @param string $inputTimezone The name of the input timezone - * @param string $outputTimezone The name of the output timezone + * @param string|null $inputTimezone The name of the input timezone + * @param string|null $outputTimezone The name of the output timezone * * @throws InvalidArgumentException if a timezone is not valid */ diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateIntervalToArrayTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateIntervalToArrayTransformer.php index bb461bb3b0..64c37cd375 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateIntervalToArrayTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateIntervalToArrayTransformer.php @@ -43,15 +43,12 @@ class DateIntervalToArrayTransformer implements DataTransformerInterface private $pad; /** - * @param string[] $fields The date fields - * @param bool $pad Whether to use padding + * @param string[]|null $fields The date fields + * @param bool $pad Whether to use padding */ public function __construct(array $fields = null, bool $pad = false) { - if (null === $fields) { - $fields = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'invert']; - } - $this->fields = $fields; + $this->fields = $fields ?? ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'invert']; $this->pad = $pad; } diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php index 2a4d10c0e3..2d91a2207f 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php @@ -22,25 +22,20 @@ use Symfony\Component\Form\Exception\TransformationFailedException; class DateTimeToArrayTransformer extends BaseDateTimeTransformer { private $pad; - private $fields; private $referenceDate; /** - * @param string $inputTimezone The input timezone - * @param string $outputTimezone The output timezone - * @param array $fields The date fields - * @param bool $pad Whether to use padding + * @param string|null $inputTimezone The input timezone + * @param string|null $outputTimezone The output timezone + * @param string[]|null $fields The date fields + * @param bool $pad Whether to use padding */ public function __construct(string $inputTimezone = null, string $outputTimezone = null, array $fields = null, bool $pad = false, \DateTimeInterface $referenceDate = null) { parent::__construct($inputTimezone, $outputTimezone); - if (null === $fields) { - $fields = ['year', 'month', 'day', 'hour', 'minute', 'second']; - } - - $this->fields = $fields; + $this->fields = $fields ?? ['year', 'month', 'day', 'hour', 'minute', 'second']; $this->pad = $pad; $this->referenceDate = $referenceDate ?? new \DateTimeImmutable('1970-01-01 00:00:00'); } diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php index a5ef4f6f06..b28cbfcc1a 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php @@ -30,12 +30,12 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer /** * @see BaseDateTimeTransformer::formats for available format options * - * @param string $inputTimezone The name of the input timezone - * @param string $outputTimezone The name of the output timezone - * @param int $dateFormat The date format - * @param int $timeFormat The time format - * @param int $calendar One of the \IntlDateFormatter calendar constants - * @param string $pattern A pattern to pass to \IntlDateFormatter + * @param string|null $inputTimezone The name of the input timezone + * @param string|null $outputTimezone The name of the output timezone + * @param int|null $dateFormat The date format + * @param int|null $timeFormat The time format + * @param int $calendar One of the \IntlDateFormatter calendar constants + * @param string|null $pattern A pattern to pass to \IntlDateFormatter * * @throws UnexpectedTypeException If a format is not supported or if a timezone is not a string */ @@ -70,7 +70,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer * * @param \DateTimeInterface $dateTime A DateTimeInterface object * - * @return string|array Localized date string/array + * @return string Localized date string * * @throws TransformationFailedException if the given value is not a \DateTimeInterface * or if the date could not be transformed diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php index d27a8ff6c4..a8f81bb8a8 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php @@ -44,9 +44,9 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer * * @see \DateTime::format() for supported formats * - * @param string $inputTimezone The name of the input timezone - * @param string $outputTimezone The name of the output timezone - * @param string $format The date format + * @param string|null $inputTimezone The name of the input timezone + * @param string|null $outputTimezone The name of the output timezone + * @param string $format The date format */ public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s') { diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php index 18341b0677..f01ea7ca5e 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php @@ -25,21 +25,9 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = self::ROUND_HALF_UP, ?int $divisor = 1) { - if (null === $grouping) { - $grouping = true; - } + parent::__construct($scale ?? 2, $grouping ?? true, $roundingMode); - if (null === $scale) { - $scale = 2; - } - - parent::__construct($scale, $grouping, $roundingMode); - - if (null === $divisor) { - $divisor = 1; - } - - $this->divisor = $divisor; + $this->divisor = $divisor ?? 1; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php index 7cf46d17c3..9ad2dbad62 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php @@ -81,17 +81,9 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP, string $locale = null) { - if (null === $grouping) { - $grouping = false; - } - - if (null === $roundingMode) { - $roundingMode = self::ROUND_HALF_UP; - } - $this->scale = $scale; - $this->grouping = $grouping; - $this->roundingMode = $roundingMode; + $this->grouping = $grouping ?? false; + $this->roundingMode = $roundingMode ?? self::ROUND_HALF_UP; $this->locale = $locale; } diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php index d231af8c75..a6e433f1e0 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php @@ -37,17 +37,10 @@ class PercentToLocalizedStringTransformer implements DataTransformerInterface /** * @see self::$types for a list of supported types * - * @param int $scale The scale - * @param string $type One of the supported types - * * @throws UnexpectedTypeException if the given value of type is unknown */ public function __construct(int $scale = null, string $type = null) { - if (null === $scale) { - $scale = 0; - } - if (null === $type) { $type = self::FRACTIONAL; } @@ -57,7 +50,7 @@ class PercentToLocalizedStringTransformer implements DataTransformerInterface } $this->type = $type; - $this->scale = $scale; + $this->scale = $scale ?? 0; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php index af24b00644..c5109fdfb3 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php @@ -28,7 +28,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; */ class DateIntervalType extends AbstractType { - private $timeParts = [ + private const TIME_PARTS = [ 'years', 'months', 'weeks', @@ -96,7 +96,7 @@ class DateIntervalType extends AbstractType if ('single_text' === $options['widget']) { $builder->addViewTransformer(new DateIntervalToStringTransformer($format)); } else { - foreach ($this->timeParts as $part) { + foreach (self::TIME_PARTS as $part) { if ($options['with_'.$part]) { $childOptions = [ 'error_bubbling' => true, @@ -157,7 +157,7 @@ class DateIntervalType extends AbstractType 'widget' => $options['widget'], 'with_invert' => $options['with_invert'], ]; - foreach ($this->timeParts as $part) { + foreach (self::TIME_PARTS as $part) { $vars['with_'.$part] = $options['with_'.$part]; } $view->vars = array_replace($view->vars, $vars); @@ -168,7 +168,6 @@ class DateIntervalType extends AbstractType */ public function configureOptions(OptionsResolver $resolver) { - $timeParts = $this->timeParts; $compound = function (Options $options) { return 'single_text' !== $options['widget']; }; @@ -180,14 +179,14 @@ class DateIntervalType extends AbstractType return $options['required'] ? null : ''; }; - $placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault, $timeParts) { + $placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) { if (\is_array($placeholder)) { $default = $placeholderDefault($options); - return array_merge(array_fill_keys($timeParts, $default), $placeholder); + return array_merge(array_fill_keys(self::TIME_PARTS, $default), $placeholder); } - return array_fill_keys($timeParts, $placeholder); + return array_fill_keys(self::TIME_PARTS, $placeholder); }; $labelsNormalizer = function (Options $options, array $labels) { diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php index 88f5457625..c43a9f0b24 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -39,8 +39,8 @@ class DateType extends AbstractType ]; private const WIDGETS = [ - 'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType', - 'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', + 'text' => TextType::class, + 'choice' => ChoiceType::class, ]; /** diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php index 44acf3efb4..cd337376e2 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php @@ -29,8 +29,8 @@ use Symfony\Component\OptionsResolver\OptionsResolver; class TimeType extends AbstractType { private const WIDGETS = [ - 'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType', - 'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', + 'text' => TextType::class, + 'choice' => ChoiceType::class, ]; /** diff --git a/src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php b/src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php index bcb8792a09..d6d6eea211 100644 --- a/src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php +++ b/src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Extension\Csrf\Type; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\FormType; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Csrf\EventListener\CsrfValidationListener; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; @@ -83,7 +84,7 @@ class FormTypeCsrfExtension extends AbstractTypeExtension $tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: \get_class($form->getConfig()->getType()->getInnerType())); $data = (string) $options['csrf_token_manager']->getToken($tokenId); - $csrfForm = $factory->createNamed($options['csrf_field_name'], 'Symfony\Component\Form\Extension\Core\Type\HiddenType', $data, [ + $csrfForm = $factory->createNamed($options['csrf_field_name'], HiddenType::class, $data, [ 'block_prefix' => 'csrf_token', 'mapped' => false, ]); diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 17955425aa..b91d757491 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -23,6 +23,7 @@ use Symfony\Component\Form\Exception\OutOfBoundsException; use Symfony\Component\Form\Exception\RuntimeException; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Util\FormUtil; use Symfony\Component\Form\Util\InheritDataAwareIterator; use Symfony\Component\Form\Util\OrderedHashMap; @@ -80,12 +81,14 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac private $parent; /** - * @var FormInterface[]|OrderedHashMap A map of FormInterface instances + * A map of FormInterface instances. + * + * @var FormInterface[]|OrderedHashMap */ private $children; /** - * @var FormError[] An array of FormError instances + * @var FormError[] */ private $errors = []; @@ -95,7 +98,9 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac private $submitted = false; /** - * @var FormInterface|ClickableInterface|null The button that was used to submit the form + * The button that was used to submit the form. + * + * @var FormInterface|ClickableInterface|null */ private $clickedButton; @@ -115,12 +120,16 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac private $viewData; /** - * @var array The submitted values that don't belong to any children + * The submitted values that don't belong to any children. + * + * @var array */ private $extraData = []; /** - * @var TransformationFailedException|null The transformation failure generated during submission, if any + * The transformation failure generated during submission, if any. + * + * @var TransformationFailedException|null */ private $transformationFailure; @@ -151,7 +160,9 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac private $name = ''; /** - * @var bool Whether the form inherits its underlying data from its parent + * Whether the form inherits its underlying data from its parent. + * + * @var bool */ private $inheritData; @@ -857,7 +868,7 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac $options['auto_initialize'] = false; if (null === $type && null === $this->config->getDataClass()) { - $type = 'Symfony\Component\Form\Extension\Core\Type\TextType'; + $type = TextType::class; } if (null === $type) { diff --git a/src/Symfony/Component/Form/FormBuilder.php b/src/Symfony/Component/Form/FormBuilder.php index efbd6f86a1..2fa65e095c 100644 --- a/src/Symfony/Component/Form/FormBuilder.php +++ b/src/Symfony/Component/Form/FormBuilder.php @@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Exception\BadMethodCallException; use Symfony\Component\Form\Exception\InvalidArgumentException; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Extension\Core\Type\TextType; /** * A builder for creating {@link Form} instances. @@ -87,7 +88,7 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB } if (null === $type && null === $this->getDataClass()) { - $type = 'Symfony\Component\Form\Extension\Core\Type\TextType'; + $type = TextType::class; } if (null !== $type) { diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index ac38b0a39e..7e6b13933d 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Form; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Extension\Core\Type\FormType; +use Symfony\Component\Form\Extension\Core\Type\TextType; class FormFactory implements FormFactoryInterface { @@ -25,7 +27,7 @@ class FormFactory implements FormFactoryInterface /** * {@inheritdoc} */ - public function create($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []) + public function create($type = FormType::class, $data = null, array $options = []) { return $this->createBuilder($type, $data, $options)->getForm(); } @@ -33,7 +35,7 @@ class FormFactory implements FormFactoryInterface /** * {@inheritdoc} */ - public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []) + public function createNamed($name, $type = FormType::class, $data = null, array $options = []) { return $this->createNamedBuilder($name, $type, $data, $options)->getForm(); } @@ -49,7 +51,7 @@ class FormFactory implements FormFactoryInterface /** * {@inheritdoc} */ - public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []) + public function createBuilder($type = FormType::class, $data = null, array $options = []) { if (!\is_string($type)) { throw new UnexpectedTypeException($type, 'string'); @@ -61,7 +63,7 @@ class FormFactory implements FormFactoryInterface /** * {@inheritdoc} */ - public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []) + public function createNamedBuilder($name, $type = FormType::class, $data = null, array $options = []) { if (null !== $data && !\array_key_exists('data', $options)) { $options['data'] = $data; @@ -88,7 +90,7 @@ class FormFactory implements FormFactoryInterface public function createBuilderForProperty($class, $property, $data = null, array $options = []) { if (null === $guesser = $this->registry->getTypeGuesser()) { - return $this->createNamedBuilder($property, 'Symfony\Component\Form\Extension\Core\Type\TextType', $data, $options); + return $this->createNamedBuilder($property, TextType::class, $data, $options); } $typeGuess = $guesser->guessType($class, $property); @@ -96,7 +98,7 @@ class FormFactory implements FormFactoryInterface $requiredGuess = $guesser->guessRequired($class, $property); $patternGuess = $guesser->guessPattern($class, $property); - $type = $typeGuess ? $typeGuess->getType() : 'Symfony\Component\Form\Extension\Core\Type\TextType'; + $type = $typeGuess ? $typeGuess->getType() : TextType::class; $maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null; $pattern = $patternGuess ? $patternGuess->getValue() : null; diff --git a/src/Symfony/Component/Form/FormFactoryBuilder.php b/src/Symfony/Component/Form/FormFactoryBuilder.php index 31f38518b7..11f455fb39 100644 --- a/src/Symfony/Component/Form/FormFactoryBuilder.php +++ b/src/Symfony/Component/Form/FormFactoryBuilder.php @@ -38,7 +38,7 @@ class FormFactoryBuilder implements FormFactoryBuilderInterface private $types = []; /** - * @var FormTypeExtensionInterface[] + * @var FormTypeExtensionInterface[][] */ private $typeExtensions = []; diff --git a/src/Symfony/Component/Form/FormFactoryInterface.php b/src/Symfony/Component/Form/FormFactoryInterface.php index 5719962724..c78481c47a 100644 --- a/src/Symfony/Component/Form/FormFactoryInterface.php +++ b/src/Symfony/Component/Form/FormFactoryInterface.php @@ -11,6 +11,9 @@ namespace Symfony\Component\Form; +use Symfony\Component\Form\Extension\Core\Type\FormType; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; + /** * Allows creating a form based on a name, a class or a property. * @@ -28,9 +31,9 @@ interface FormFactoryInterface * * @return FormInterface The form named after the type * - * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException if any given option is not applicable to the given type + * @throws InvalidOptionsException if any given option is not applicable to the given type */ - public function create($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []); + public function create($type = FormType::class, $data = null, array $options = []); /** * Returns a form. @@ -43,9 +46,9 @@ interface FormFactoryInterface * * @return FormInterface The form * - * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException if any given option is not applicable to the given type + * @throws InvalidOptionsException if any given option is not applicable to the given type */ - public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []); + public function createNamed($name, $type = FormType::class, $data = null, array $options = []); /** * Returns a form for a property of a class. @@ -58,7 +61,7 @@ interface FormFactoryInterface * * @return FormInterface The form named after the property * - * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException if any given option is not applicable to the form type + * @throws InvalidOptionsException if any given option is not applicable to the form type */ public function createForProperty($class, $property, $data = null, array $options = []); @@ -70,9 +73,9 @@ interface FormFactoryInterface * * @return FormBuilderInterface The form builder * - * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException if any given option is not applicable to the given type + * @throws InvalidOptionsException if any given option is not applicable to the given type */ - public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []); + public function createBuilder($type = FormType::class, $data = null, array $options = []); /** * Returns a form builder. @@ -83,9 +86,9 @@ interface FormFactoryInterface * * @return FormBuilderInterface The form builder * - * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException if any given option is not applicable to the given type + * @throws InvalidOptionsException if any given option is not applicable to the given type */ - public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []); + public function createNamedBuilder($name, $type = FormType::class, $data = null, array $options = []); /** * Returns a form builder for a property of a class. @@ -99,7 +102,7 @@ interface FormFactoryInterface * * @return FormBuilderInterface The form builder named after the property * - * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException if any given option is not applicable to the form type + * @throws InvalidOptionsException if any given option is not applicable to the form type */ public function createBuilderForProperty($class, $property, $data = null, array $options = []); } diff --git a/src/Symfony/Component/Form/FormRegistry.php b/src/Symfony/Component/Form/FormRegistry.php index 8583054c20..a93e0f7baa 100644 --- a/src/Symfony/Component/Form/FormRegistry.php +++ b/src/Symfony/Component/Form/FormRegistry.php @@ -24,8 +24,6 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException; class FormRegistry implements FormRegistryInterface { /** - * Extensions. - * * @var FormExtensionInterface[] */ private $extensions = []; @@ -48,7 +46,7 @@ class FormRegistry implements FormRegistryInterface private $checkedTypes = []; /** - * @param FormExtensionInterface[] $extensions An array of FormExtensionInterface + * @param FormExtensionInterface[] $extensions * * @throws UnexpectedTypeException if any extension does not implement FormExtensionInterface */ @@ -56,7 +54,7 @@ class FormRegistry implements FormRegistryInterface { foreach ($extensions as $extension) { if (!$extension instanceof FormExtensionInterface) { - throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormExtensionInterface'); + throw new UnexpectedTypeException($extension, FormExtensionInterface::class); } } @@ -84,7 +82,7 @@ class FormRegistry implements FormRegistryInterface if (!class_exists($name)) { throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not exist.', $name)); } - if (!is_subclass_of($name, 'Symfony\Component\Form\FormTypeInterface')) { + if (!is_subclass_of($name, FormTypeInterface::class)) { throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not implement "Symfony\Component\Form\FormTypeInterface".', $name)); } diff --git a/src/Symfony/Component/Form/FormTypeGuesserChain.php b/src/Symfony/Component/Form/FormTypeGuesserChain.php index 0a3450fc33..707cd57b42 100644 --- a/src/Symfony/Component/Form/FormTypeGuesserChain.php +++ b/src/Symfony/Component/Form/FormTypeGuesserChain.php @@ -27,7 +27,7 @@ class FormTypeGuesserChain implements FormTypeGuesserInterface { foreach ($guessers as $guesser) { if (!$guesser instanceof FormTypeGuesserInterface) { - throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\FormTypeGuesserInterface'); + throw new UnexpectedTypeException($guesser, FormTypeGuesserInterface::class); } if ($guesser instanceof self) { diff --git a/src/Symfony/Component/Form/Guess/Guess.php b/src/Symfony/Component/Form/Guess/Guess.php index 81ed8e1998..935bbfea1c 100644 --- a/src/Symfony/Component/Form/Guess/Guess.php +++ b/src/Symfony/Component/Form/Guess/Guess.php @@ -80,8 +80,6 @@ abstract class Guess } /** - * @param int $confidence The confidence - * * @throws InvalidArgumentException if the given value of confidence is unknown */ public function __construct(int $confidence) diff --git a/src/Symfony/Component/Form/ResolvedFormType.php b/src/Symfony/Component/Form/ResolvedFormType.php index d913d34eb3..514dc6dc4f 100644 --- a/src/Symfony/Component/Form/ResolvedFormType.php +++ b/src/Symfony/Component/Form/ResolvedFormType.php @@ -43,11 +43,14 @@ class ResolvedFormType implements ResolvedFormTypeInterface */ private $optionsResolver; + /** + * @param FormTypeExtensionInterface[] $typeExtensions + */ public function __construct(FormTypeInterface $innerType, array $typeExtensions = [], ResolvedFormTypeInterface $parent = null) { foreach ($typeExtensions as $extension) { if (!$extension instanceof FormTypeExtensionInterface) { - throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface'); + throw new UnexpectedTypeException($extension, FormTypeExtensionInterface::class); } } @@ -117,7 +120,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface } /** - * Configures a form builder for the type hierarchy. + * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { @@ -133,9 +136,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface } /** - * Configures a form view for the type hierarchy. - * - * This method is called before the children of the view are built. + * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { @@ -151,9 +152,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface } /** - * Finishes a form view for the type hierarchy. - * - * This method is called after the children of the view have been built. + * {@inheritdoc} */ public function finishView(FormView $view, FormInterface $form, array $options) { @@ -170,9 +169,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface } /** - * Returns the configured options resolver used for this type. - * - * @return \Symfony\Component\OptionsResolver\OptionsResolver The options resolver + * {@inheritdoc} */ public function getOptionsResolver() { diff --git a/src/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php b/src/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php index 178efd9258..9d0fae9725 100644 --- a/src/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php +++ b/src/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php @@ -13,9 +13,8 @@ namespace Symfony\Component\Form\Tests; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractTypeExtension; -use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormConfigInterface; @@ -33,21 +32,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver; */ class ResolvedFormTypeTest extends TestCase { - /** - * @var MockObject - */ - private $dispatcher; - - /** - * @var MockObject - */ - private $factory; - - /** - * @var MockObject - */ - private $dataMapper; - /** * @var MockObject|FormTypeInterface */ @@ -80,9 +64,6 @@ class ResolvedFormTypeTest extends TestCase protected function setUp(): void { - $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->factory = $this->createMock(FormFactoryInterface::class); - $this->dataMapper = $this->createMock(DataMapperInterface::class); $this->parentType = $this->getMockFormType(); $this->type = $this->getMockFormType(); $this->extension1 = $this->getMockFormTypeExtension(); @@ -256,7 +237,7 @@ class ResolvedFormTypeTest extends TestCase public function testCreateView() { - $form = new Form($this->createMock(FormConfigInterface::class)); + $form = $this->bootstrapForm(); $view = $this->resolvedType->createView($form); @@ -266,7 +247,7 @@ class ResolvedFormTypeTest extends TestCase public function testCreateViewWithParent() { - $form = new Form($this->createMock(FormConfigInterface::class)); + $form = $this->bootstrapForm(); $parentView = $this->createMock(FormView::class); $view = $this->resolvedType->createView($form, $parentView); @@ -278,7 +259,7 @@ class ResolvedFormTypeTest extends TestCase public function testBuildView() { $options = ['a' => '1', 'b' => '2']; - $form = new Form($this->createMock(FormConfigInterface::class)); + $form = $this->bootstrapForm(); $view = $this->createMock(FormView::class); $i = 0; @@ -320,7 +301,7 @@ class ResolvedFormTypeTest extends TestCase public function testFinishView() { $options = ['a' => '1', 'b' => '2']; - $form = new Form($this->createMock(FormConfigInterface::class)); + $form = $this->bootstrapForm(); $view = $this->createMock(FormView::class); $i = 0; @@ -392,7 +373,7 @@ class ResolvedFormTypeTest extends TestCase ]; } - private function getMockFormType($typeClass = 'Symfony\Component\Form\AbstractType'): MockObject + private function getMockFormType($typeClass = AbstractType::class): MockObject { return $this->getMockBuilder($typeClass)->setMethods(['getBlockPrefix', 'configureOptions', 'finishView', 'buildView', 'buildForm'])->getMock(); } @@ -406,4 +387,13 @@ class ResolvedFormTypeTest extends TestCase { return $this->createMock(FormFactoryInterface::class); } + + private function bootstrapForm(): Form + { + $config = $this->createMock(FormConfigInterface::class); + $config->method('getInheritData')->willReturn(false); + $config->method('getName')->willReturn(''); + + return new Form($config); + } }