From b92fd7674c7ed419296d23c5ec2774edb7f75fb4 Mon Sep 17 00:00:00 2001 From: Valentin Date: Tue, 16 Jul 2019 01:59:30 +0300 Subject: [PATCH 1/2] Deprecate passing integers --- src/Symfony/Component/Form/ButtonBuilder.php | 6 ++-- src/Symfony/Component/Form/Form.php | 9 ++++-- src/Symfony/Component/Form/FormBuilder.php | 28 +++++++++++++++++-- .../Component/Form/FormBuilderInterface.php | 6 ++-- .../Component/Form/FormConfigBuilder.php | 5 ++++ src/Symfony/Component/Form/FormFactory.php | 10 +++++++ .../Component/Form/FormFactoryInterface.php | 16 +++++------ src/Symfony/Component/Form/FormInterface.php | 6 ++-- .../Component/Form/ResolvedFormType.php | 10 +++++++ 9 files changed, 74 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index 19196729f1cb0..91a7fe92d763e 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -79,9 +79,9 @@ public function __construct(?string $name, array $options = []) * * This method should not be invoked. * - * @param string|int|FormBuilderInterface $child - * @param string|FormTypeInterface $type - * @param array $options + * @param string|FormBuilderInterface $child + * @param string|FormTypeInterface $type + * @param array $options * * @throws BadMethodCallException */ diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 347cf5e78fadb..8de637ba4327a 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -843,8 +843,13 @@ public function add($child, $type = null, array $options = []) } if (!$child instanceof FormInterface) { - if (!\is_string($child) && !\is_int($child)) { - throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface'); + if (\is_int($child)) { + @trigger_error(sprintf('Passing an integer child name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $child = (string) $child; + } + + if (!\is_string($child)) { + throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormInterface'); } $child = (string) $child; diff --git a/src/Symfony/Component/Form/FormBuilder.php b/src/Symfony/Component/Form/FormBuilder.php index 56cdbb6f14298..8741754f2ea4d 100644 --- a/src/Symfony/Component/Form/FormBuilder.php +++ b/src/Symfony/Component/Form/FormBuilder.php @@ -62,8 +62,13 @@ public function add($child, $type = null, array $options = []) return $this; } - if (!\is_string($child) && !\is_int($child)) { - throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormBuilderInterface'); + if (\is_int($child)) { + @trigger_error(sprintf('Passing an integer child name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $child = (string) $child; + } + + if (!\is_string($child)) { + throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilderInterface'); } if (null !== $type && !\is_string($type) && !$type instanceof FormTypeInterface) { @@ -86,6 +91,11 @@ public function create($name, $type = null, array $options = []) throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'); } + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $name = (string) $name; + } + if (null === $type && null === $this->getDataClass()) { $type = 'Symfony\Component\Form\Extension\Core\Type\TextType'; } @@ -106,6 +116,10 @@ public function get($name) throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'); } + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + } + if (isset($this->unresolvedChildren[$name])) { return $this->resolveChild($name); } @@ -126,6 +140,10 @@ public function remove($name) throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'); } + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + } + unset($this->unresolvedChildren[$name], $this->children[$name]); return $this; @@ -140,6 +158,10 @@ public function has($name) throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'); } + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + } + return isset($this->unresolvedChildren[$name]) || isset($this->children[$name]); } @@ -241,7 +263,7 @@ private function resolveChild(string $name): FormBuilderInterface private function resolveChildren() { foreach ($this->unresolvedChildren as $name => $info) { - $this->children[$name] = $this->create($name, $info[0], $info[1]); + $this->children[$name] = $this->create((string) $name, $info[0], $info[1]); } $this->unresolvedChildren = []; diff --git a/src/Symfony/Component/Form/FormBuilderInterface.php b/src/Symfony/Component/Form/FormBuilderInterface.php index 1ed695ed044d8..f066116c32f48 100644 --- a/src/Symfony/Component/Form/FormBuilderInterface.php +++ b/src/Symfony/Component/Form/FormBuilderInterface.php @@ -23,9 +23,9 @@ interface FormBuilderInterface extends \Traversable, \Countable, FormConfigBuild * If you add a nested group, this group should also be represented in the * object hierarchy. * - * @param string|int|FormBuilderInterface $child - * @param string|null $type - * @param array $options + * @param string|FormBuilderInterface $child + * @param string|null $type + * @param array $options * * @return self */ diff --git a/src/Symfony/Component/Form/FormConfigBuilder.php b/src/Symfony/Component/Form/FormConfigBuilder.php index 09d4dd549c345..ce547227ba087 100644 --- a/src/Symfony/Component/Form/FormConfigBuilder.php +++ b/src/Symfony/Component/Form/FormConfigBuilder.php @@ -776,6 +776,11 @@ public function getFormConfig() */ public static function validateName($name) { + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $name = (string) $name; + } + if (null !== $name && !\is_string($name)) { throw new UnexpectedTypeException($name, 'string or null'); } diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index ac38b0a39e647..4c13dd3bf9d6a 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -35,6 +35,11 @@ public function create($type = 'Symfony\Component\Form\Extension\Core\Type\FormT */ public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []) { + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $name = (string) $name; + } + return $this->createNamedBuilder($name, $type, $data, $options)->getForm(); } @@ -63,6 +68,11 @@ public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Typ */ public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []) { + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $name = (string) $name; + } + if (null !== $data && !\array_key_exists('data', $options)) { $options['data'] = $data; } diff --git a/src/Symfony/Component/Form/FormFactoryInterface.php b/src/Symfony/Component/Form/FormFactoryInterface.php index 333652ddf8d73..1a935093768ad 100644 --- a/src/Symfony/Component/Form/FormFactoryInterface.php +++ b/src/Symfony/Component/Form/FormFactoryInterface.php @@ -38,10 +38,10 @@ public function create($type = 'Symfony\Component\Form\Extension\Core\Type\FormT * * @see createNamedBuilder() * - * @param string|int $name The name of the form - * @param string $type The type of the form - * @param mixed $data The initial data - * @param array $options The options + * @param string $name The name of the form + * @param string $type The type of the form + * @param mixed $data The initial data + * @param array $options The options * * @return FormInterface The form * @@ -81,10 +81,10 @@ public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Typ /** * Returns a form builder. * - * @param string|int $name The name of the form - * @param string $type The type of the form - * @param mixed $data The initial data - * @param array $options The options + * @param string $name The name of the form + * @param string $type The type of the form + * @param mixed $data The initial data + * @param array $options The options * * @return FormBuilderInterface The form builder * diff --git a/src/Symfony/Component/Form/FormInterface.php b/src/Symfony/Component/Form/FormInterface.php index b73927f3bcaaf..da3cba51b9628 100644 --- a/src/Symfony/Component/Form/FormInterface.php +++ b/src/Symfony/Component/Form/FormInterface.php @@ -43,9 +43,9 @@ public function getParent(); /** * Adds or replaces a child to the form. * - * @param FormInterface|string|int $child The FormInterface instance or the name of the child - * @param string|null $type The child's type, if a name was passed - * @param array $options The child's options, if a name was passed + * @param FormInterface|string $child The FormInterface instance or the name of the child + * @param string|null $type The child's type, if a name was passed + * @param array $options The child's options, if a name was passed * * @return $this * diff --git a/src/Symfony/Component/Form/ResolvedFormType.php b/src/Symfony/Component/Form/ResolvedFormType.php index d7ab90ff18308..41f06ae4f4120 100644 --- a/src/Symfony/Component/Form/ResolvedFormType.php +++ b/src/Symfony/Component/Form/ResolvedFormType.php @@ -93,6 +93,11 @@ public function getTypeExtensions() */ public function createBuilder(FormFactoryInterface $factory, $name, array $options = []) { + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $name = (string) $name; + } + try { $options = $this->getOptionsResolver()->resolve($options); } catch (ExceptionInterface $e) { @@ -218,6 +223,11 @@ public function getOptionsResolver() */ protected function newBuilder($name, $dataClass, FormFactoryInterface $factory, array $options) { + if (\is_int($name)) { + @trigger_error(sprintf('Passing an integer name to "%s" is deprecated since Symfony 4.4. Pass a string instead.', __METHOD__), \E_USER_DEPRECATED); + $name = (string) $name; + } + if ($this->innerType instanceof ButtonTypeInterface) { return new ButtonBuilder($name, $options); } From 10ded0ace0faf3167722cffc7f72ebe6258eb769 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 29 Jul 2019 08:26:22 +0200 Subject: [PATCH 2/2] fix tests --- .../Form/Extension/Core/EventListener/ResizeFormListener.php | 4 ++-- src/Symfony/Component/Form/Tests/CompoundFormTest.php | 3 +++ src/Symfony/Component/Form/Tests/FormBuilderTest.php | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php index 695336e9548ae..38d43bc9d151d 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php @@ -77,7 +77,7 @@ public function preSetData(FormEvent $event) // Then add all rows again in the correct order foreach ($data as $name => $value) { - $form->add($name, $this->type, array_replace([ + $form->add((string) $name, $this->type, array_replace([ 'property_path' => '['.$name.']', ], $this->options)); } @@ -105,7 +105,7 @@ public function preSubmit(FormEvent $event) if ($this->allowAdd) { foreach ($data as $name => $value) { if (!$form->has($name)) { - $form->add($name, $this->type, array_replace([ + $form->add((string) $name, $this->type, array_replace([ 'property_path' => '['.$name.']', ], $this->options)); } diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 2fb535ae7ef1e..7c604c137e42b 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -188,6 +188,9 @@ public function testAddUsingNameAndType() $this->assertSame(['foo' => $child], $this->form->all()); } + /** + * @group legacy + */ public function testAddUsingIntegerNameAndType() { $child = $this->getBuilder(0)->getForm(); diff --git a/src/Symfony/Component/Form/Tests/FormBuilderTest.php b/src/Symfony/Component/Form/Tests/FormBuilderTest.php index b5fb8a3572e5c..676ab88ece0f4 100644 --- a/src/Symfony/Component/Form/Tests/FormBuilderTest.php +++ b/src/Symfony/Component/Form/Tests/FormBuilderTest.php @@ -81,6 +81,9 @@ public function testAdd() $this->assertTrue($this->builder->has('foo')); } + /** + * @group legacy + */ public function testAddIntegerName() { $this->assertFalse($this->builder->has(0));