Skip to content

[Form] remove remaining deprecation layers #41743

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
Jun 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ public function getDecoratedFactory()
* @param callable|Cache\ChoiceFilter|null $filter The callable or static option for
* filtering the choices
*/
public function createListFromChoices(iterable $choices, $value = null/*, $filter = null*/)
public function createListFromChoices(iterable $choices, $value = null, $filter = null)
{
$filter = \func_num_args() > 2 ? func_get_arg(2) : null;

if ($choices instanceof \Traversable) {
$choices = iterator_to_array($choices);
}
Expand Down Expand Up @@ -134,10 +132,8 @@ public function createListFromChoices(iterable $choices, $value = null/*, $filte
* @param callable|Cache\ChoiceFilter|null $filter The callable or static option for
* filtering the choices
*/
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null/*, $filter = null*/)
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null, $filter = null)
{
$filter = \func_num_args() > 2 ? func_get_arg(2) : null;

$cache = true;

if ($loader instanceof Cache\ChoiceLoader) {
Expand Down Expand Up @@ -181,9 +177,8 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
* @param array|callable|Cache\ChoiceAttr|null $attr The option or static option generating the HTML attributes
* @param array|callable|Cache\ChoiceTranslationParameters $labelTranslationParameters The parameters used to translate the choice labels
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/)
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelTranslationParameters = [])
{
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : [];
$cache = true;

if ($preferredChoices instanceof Cache\PreferredChoice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface ChoiceListFactoryInterface
*
* @return ChoiceListInterface The choice list
*/
public function createListFromChoices(iterable $choices, callable $value = null/*, callable $filter = null*/);
public function createListFromChoices(iterable $choices, callable $value = null, callable $filter = null);

/**
* Creates a choice list that is loaded with the given loader.
Expand All @@ -48,7 +48,7 @@ public function createListFromChoices(iterable $choices, callable $value = null/
*
* @return ChoiceListInterface The choice list
*/
public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null/*, callable $filter = null*/);
public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null, callable $filter = null);

/**
* Creates a view for the given choice list.
Expand Down Expand Up @@ -84,5 +84,5 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
*
* @return ChoiceListView The choice list view
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/);
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null, $labelTranslationParameters = []);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
{
/**
* {@inheritdoc}
*
* @param callable|null $filter
*/
public function createListFromChoices(iterable $choices, callable $value = null/*, callable $filter = null*/)
public function createListFromChoices(iterable $choices, callable $value = null, callable $filter = null)
{
$filter = \func_num_args() > 2 ? func_get_arg(2) : null;

if ($filter) {
// filter the choice list lazily
return $this->createListFromLoader(new FilterChoiceLoaderDecorator(
Expand All @@ -53,13 +49,9 @@ public function createListFromChoices(iterable $choices, callable $value = null/

/**
* {@inheritdoc}
*
* @param callable|null $filter
*/
public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null/*, callable $filter = null*/)
public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null, callable $filter = null)
{
$filter = \func_num_args() > 2 ? func_get_arg(2) : null;

if ($filter) {
$loader = new FilterChoiceLoaderDecorator($loader, $filter);
}
Expand All @@ -72,9 +64,8 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
*
* @param array|callable $labelTranslationParameters The parameters used to translate the choice labels
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove as many annotations as possible, and add the union types meanwhile!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but I prefer to do that in a different PR.

*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/)
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null, $labelTranslationParameters = [])
{
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : [];
$preferredViews = [];
$preferredViewsOrder = [];
$otherViews = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ public function getDecoratedFactory()
*
* @return ChoiceListInterface The choice list
*/
public function createListFromChoices(iterable $choices, $value = null/*, $filter = null*/)
public function createListFromChoices(iterable $choices, $value = null, $filter = null)
{
$filter = \func_num_args() > 2 ? func_get_arg(2) : null;

if (\is_string($value)) {
$value = new PropertyPath($value);
}
Expand Down Expand Up @@ -109,10 +107,8 @@ public function createListFromChoices(iterable $choices, $value = null/*, $filte
*
* @return ChoiceListInterface The choice list
*/
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null/*, $filter = null*/)
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null, $filter = null)
{
$filter = \func_num_args() > 2 ? func_get_arg(2) : null;

if (\is_string($value)) {
$value = new PropertyPath($value);
}
Expand Down Expand Up @@ -154,9 +150,8 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
*
* @return ChoiceListView The choice list view
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/)
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelTranslationParameters = [])
{
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : [];
$accessor = $this->propertyAccessor;

if (\is_string($label)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class RadioListMapper implements DataMapperInterface
*/
public function mapDataToForms($choice, \Traversable $radios)
{
if (\is_array($radios)) {
trigger_deprecation('symfony/form', '5.3', 'Passing an array as the second argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__);
}

if (!\is_string($choice)) {
throw new UnexpectedTypeException($choice, 'string');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ 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
* @param int|null $roundingMode A value from \NumberFormatter, such as \NumberFormatter::ROUND_HALFUP
* @param bool $html5Format Use an HTML5 specific format, see https://www.w3.org/TR/html51/sec-forms.html#date-time-and-number-formats
* @param int $scale The scale
* @param string $type One of the supported types
* @param int $roundingMode A value from \NumberFormatter, such as \NumberFormatter::ROUND_HALFUP
* @param bool $html5Format Use an HTML5 specific format, see https://www.w3.org/TR/html51/sec-forms.html#date-time-and-number-formats
*
* @throws UnexpectedTypeException if the given value of type is unknown
*/
public function __construct(int $scale = null, string $type = null, ?int $roundingMode = null, bool $html5Format = false)
public function __construct(int $scale = null, string $type = null, int $roundingMode = \NumberFormatter::ROUND_HALFUP, bool $html5Format = false)
{
if (null === $scale) {
$scale = 0;
Expand All @@ -56,10 +56,6 @@ public function __construct(int $scale = null, string $type = null, ?int $roundi
$type = self::FRACTIONAL;
}

if (null === $roundingMode && (\func_num_args() < 4 || func_get_arg(3))) {
trigger_deprecation('symfony/form', '5.1', 'Not passing a rounding mode to "%s()" is deprecated. Starting with Symfony 6.0 it will default to "\NumberFormatter::ROUND_HALFUP".', __METHOD__);
}

if (!\in_array($type, self::$types, true)) {
throw new UnexpectedTypeException($type, implode('", "', self::$types));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BirthdayType extends AbstractType
Expand All @@ -24,11 +23,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'years' => range((int) date('Y') - 120, date('Y')),
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid birthdate.';
},
'invalid_message' => 'Please enter a valid birthdate.',
]);

$resolver->setAllowedTypes('years', 'array');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CheckboxType extends AbstractType
Expand Down Expand Up @@ -61,11 +60,7 @@ public function configureOptions(OptionsResolver $resolver)
'empty_data' => $emptyData,
'compound' => false,
'false_values' => [null],
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'The checkbox has an invalid value.';
},
'invalid_message' => 'The checkbox has an invalid value.',
'is_empty_callback' => static function ($modelData): bool {
return false === $modelData;
},
Expand Down
26 changes: 2 additions & 24 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,13 @@ class ChoiceType extends AbstractType
private $choiceListFactory;
private $translator;

/**
* @param TranslatorInterface $translator
*/
public function __construct(ChoiceListFactoryInterface $choiceListFactory = null, $translator = null)
public function __construct(ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null)
{
$this->choiceListFactory = $choiceListFactory ?? new CachingFactoryDecorator(
new PropertyAccessDecorator(
new DefaultChoiceListFactory()
)
);

// BC, to be removed in 6.0
if ($this->choiceListFactory instanceof CachingFactoryDecorator) {
return;
}

$ref = new \ReflectionMethod($this->choiceListFactory, 'createListFromChoices');

if ($ref->getNumberOfParameters() < 3) {
trigger_deprecation('symfony/form', '5.1', 'Not defining a third parameter "callable|null $filter" in "%s::%s()" is deprecated.', $ref->class, $ref->name);
}

if (null !== $translator && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be han instance of "%s", "%s" given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->translator = $translator;
}

Expand Down Expand Up @@ -390,11 +372,7 @@ public function configureOptions(OptionsResolver $resolver)
'data_class' => null,
'choice_translation_domain' => true,
'trim' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'The selected choice is invalid.';
},
'invalid_message' => 'The selected choice is invalid.',
]);

$resolver->setNormalizer('placeholder', $placeholderNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ public function configureOptions(OptionsResolver $resolver)
'entry_type' => TextType::class,
'entry_options' => [],
'delete_empty' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'The collection is invalid.';
},
'invalid_message' => 'The collection is invalid.',
]);

$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -70,11 +69,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'html5' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid color.';
},
'invalid_message' => 'Please select a valid color.',
]);

$resolver->setAllowedTypes('html5', 'bool');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public function configureOptions(OptionsResolver $resolver)
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'alpha3' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid country.';
},
'invalid_message' => 'Please select a valid country.',
]);

$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public function configureOptions(OptionsResolver $resolver)
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid currency.';
},
'invalid_message' => 'Please select a valid currency.',
]);

$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,7 @@ public function configureOptions(OptionsResolver $resolver)
'compound' => $compound,
'empty_data' => $emptyData,
'labels' => [],
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please choose a valid date interval.';
},
'invalid_message' => 'Please choose a valid date interval.',
]);
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
$resolver->setNormalizer('labels', $labelsNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,7 @@ public function configureOptions(OptionsResolver $resolver)
return $options['compound'] ? [] : '';
},
'input_format' => 'Y-m-d H:i:s',
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid date and time.';
},
'invalid_message' => 'Please enter a valid date and time.',
]);

// Don't add some defaults in order to preserve the defaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,7 @@ public function configureOptions(OptionsResolver $resolver)
},
'choice_translation_domain' => false,
'input_format' => 'Y-m-d',
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid date.';
},
'invalid_message' => 'Please enter a valid date.',
]);

$resolver->setNormalizer('placeholder', $placeholderNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EmailType extends AbstractType
Expand All @@ -23,11 +22,7 @@ class EmailType extends AbstractType
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid email address.';
},
'invalid_message' => 'Please enter a valid email address.',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ public function configureOptions(OptionsResolver $resolver)
'empty_data' => $emptyData,
'multiple' => false,
'allow_file_upload' => true,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid file.';
},
'invalid_message' => 'Please select a valid file.',
]);
}

Expand Down
Loading