-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[OptionsResolver] Introduce ability to deprecate options, allowed types and values #27277
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
[OptionsResolver] Introduce ability to deprecate options, allowed types and values #27277
Conversation
381c953
to
3b7280e
Compare
|
3b7280e
to
981c2bf
Compare
The "Deprecating allowed values" is kinda the same example as "Deprecating allowed types".. no? |
Yes, same approach: a closure function that returns a string or not depending on the given value. However in "Deprecating allowed values" example you aren't deprecating the value type at all but just a range. |
0e6ff64
to
b47b6b7
Compare
b47b6b7
to
e112140
Compare
src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php
Outdated
Show resolved
Hide resolved
e112140
to
bbed8b3
Compare
@fabpot comments addressed, very appreciated your review, thanks. |
bbed8b3
to
9beb4ab
Compare
I just changed the exception class here |
9beb4ab
to
f8746ce
Compare
Thank you @yceruto. |
…ns, allowed types and values (yceruto) This PR was merged into the 4.2-dev branch. Discussion ---------- [OptionsResolver] Introduce ability to deprecate options, allowed types and values | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27216 | License | MIT | Doc PR | symfony/symfony-docs#9859 **Deprecating an option** ```php $resolver = (new OptionsResolver()) ->setDefined(['foo', 'bar']) ->setDeprecated('foo') ; $resolver->resolve(['foo' => 'baz']); // PHP Deprecated: The option "foo" is deprecated. ``` With custom message: ```php $resolver = (new OptionsResolver()) ->setDefined('foo') ->setDefault('bar', function (Options $options) { return $options['foo']; }) ->setDeprecated('foo', 'The option "foo" is deprecated, use "bar" option instead.') ; $resolver->resolve(['foo' => 'baz']); // PHP Deprecated: The option "foo" is deprecated, use "bar" option instead. $resolver->resolve(['bar' => 'baz']); // OK. ``` **Deprecating allowed types** ```php $resolver = (new OptionsResolver()) ->setDefault('type', null) ->setAllowedTypes('type', ['null', 'string', FormTypeInterface::class]) ->setDeprecated('type', function ($value) { if ($value instanceof FormTypeInterface) { return sprintf('Passing an instance of "%s" to option "type" is deprecated, pass its FQCN instead.', FormTypeInterface::class); } }) ; $resolver->resolve(['type' => new ChoiceType()]); // PHP Deprecated: Passing an instance of "Symfony\Component\Form\FormTypeInterface" to option "type" is deprecated, pass its FQCN instead. $resolver->resolve(['type' => ChoiceType::class]); // OK. ``` The closure is invoked when `resolve()` is called. The closure must return a string (the deprecation message) or an empty string to ignore the option deprecation. Multiple types and normalizer: ```php $resolver = (new OptionsResolver()) ->setDefault('percent', 0.0) ->setAllowedTypes('percent', ['null', 'int', 'float']) ->setDeprecated('percent', function ($value) { if (null === $value) { return 'Passing "null" to option "percent" is deprecated, pass a float number instead.'; } if (is_int($value)) { return sprintf('Passing an integer "%d" to option "percent" is deprecated, pass a float number instead.', $value); } }) ->setNormalizer('percent', function (Options $options, $value) { return (float) $value; }) ; $resolver->resolve(['percent' => null]); // PHP Deprecated: Passing "null" to option "percent" is deprecated, pass a float number instead. $resolver->resolve(['percent' => 20]); // PHP Deprecated: Passing an integer "20" to option "percent" is deprecated, pass a float number instead. $resolver->resolve(['percent' => 20.0]); // OK. ``` The parameter passed to the closure is the value of the option after validating it and before normalizing it. **Deprecating allowed values** ```php $resolver = (new OptionsResolver()) ->setDefault('percent', 0.0) ->setAllowedTypes('percent', 'float') ->setDeprecated('percent', function ($value) { if ($value < 0) { return 'Passing a number less than 0 to option "percent" is deprecated.'; } }) ; $resolver->resolve(['percent' => -50.0]); // PHP Deprecated: Passing a number less than 0 to option "percent" is deprecated. ``` Commits ------- f8746ce Add ability to deprecate options
This PR was merged into the master branch. Discussion ---------- [OptionsResolver] Document setDeprecated method Documenting feature symfony/symfony#27277. Commits ------- afa5e54 Document setDeprecated method from OptionsResolver component
…tion on debug:form command (yceruto) This PR was merged into the 4.2-dev branch. Discussion ---------- [Form][OptionsResolver] Show deprecated options definition on debug:form command | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Next move after symfony/symfony#27277. It will look like this: Use `--show-deprecated` option to show form types with deprecated options (example):  Use `--show-deprecated` option to show deprecated options of the given form type (example):  Deprecated option (example):   Commits ------- 87c209d741 Show deprecated options definition on debug:form command
…tion on debug:form command (yceruto) This PR was merged into the 4.2-dev branch. Discussion ---------- [Form][OptionsResolver] Show deprecated options definition on debug:form command | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Next move after symfony/symfony#27277. It will look like this: Use `--show-deprecated` option to show form types with deprecated options (example):  Use `--show-deprecated` option to show deprecated options of the given form type (example):  Deprecated option (example):   Commits ------- 87c209d741 Show deprecated options definition on debug:form command
…tion on debug:form command (yceruto) This PR was merged into the 4.2-dev branch. Discussion ---------- [Form][OptionsResolver] Show deprecated options definition on debug:form command | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Next move after #27277. It will look like this: Use `--show-deprecated` option to show form types with deprecated options (example):  Use `--show-deprecated` option to show deprecated options of the given form type (example):  Deprecated option (example):   Commits ------- 87c209d Show deprecated options definition on debug:form command
Deprecating an option
With custom message:
Deprecating allowed types
The closure is invoked when
resolve()
is called. The closure must return a string (the deprecation message) or an empty string to ignore the option deprecation.Multiple types and normalizer:
The parameter passed to the closure is the value of the option after validating it and before normalizing it.
Deprecating allowed values