Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@yceruto
Copy link
Member

@ycerutoyceruto commentedMay 15, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#27216
LicenseMIT
Doc PRsymfony/symfony-docs#9859

Deprecating an option

$resolver = (newOptionsResolver())    ->setDefined(['foo','bar'])    ->setDeprecated('foo');$resolver->resolve(['foo' =>'baz']);// PHP Deprecated: The option "foo" is deprecated.

With custom message:

$resolver = (newOptionsResolver())    ->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

$resolver = (newOptionsResolver())    ->setDefault('type',null)    ->setAllowedTypes('type', ['null','string', FormTypeInterface::class])    ->setDeprecated('type',function ($value) {if ($valueinstanceof FormTypeInterface) {returnsprintf('Passing an instance of "%s" to option "type" is deprecated, pass its FQCN instead.', FormTypeInterface::class);        }    });$resolver->resolve(['type' =>newChoiceType()]);// 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 whenresolve() is called. The closure must return a string (the deprecation message) or an empty string to ignore the option deprecation.

Multiple types and normalizer:

$resolver = (newOptionsResolver())    ->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)) {returnsprintf('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

$resolver = (newOptionsResolver())    ->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.

Koc, ro0NL, yceruto, and evelync23 reacted with thumbs up emoji
@yceruto
Copy link
MemberAuthor

yceruto commentedMay 15, 2018
edited
Loading

fabbot.io failure is unrelated, should I fix it here? already fixed after rebased.

@ycerutoycerutoforce-pushed theoptions_resolver_deprecated_option branch from3b7280e to981c2bfCompareMay 15, 2018 15:57
@ro0NL
Copy link
Contributor

The "Deprecating allowed values" is kinda the same example as "Deprecating allowed types".. no?

@yceruto
Copy link
MemberAuthor

yceruto commentedMay 16, 2018
edited
Loading

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.

@ycerutoycerutoforce-pushed theoptions_resolver_deprecated_option branch 2 times, most recently from0e6ff64 tob47b6b7CompareMay 18, 2018 12:15
@nicolas-grekasnicolas-grekas added this to thenext milestoneMay 18, 2018
@ycerutoycerutoforce-pushed theoptions_resolver_deprecated_option branch fromb47b6b7 toe112140CompareMay 23, 2018 19:04
@yceruto
Copy link
MemberAuthor

@fabpot comments addressed, very appreciated your review, thanks.

@ycerutoycerutoforce-pushed theoptions_resolver_deprecated_option branch frombbed8b3 to9beb4abCompareMay 30, 2018 13:10
@yceruto
Copy link
MemberAuthor

I just changed the exception classhereInvalidOptionsException instead ofInvalidArgumentException.

@ycerutoycerutoforce-pushed theoptions_resolver_deprecated_option branch from9beb4ab tof8746ceCompareMay 31, 2018 11:28
@nicolas-grekas
Copy link
Member

Thank you@yceruto.

yceruto and ogizanagi reacted with hooray emoji

@nicolas-grekasnicolas-grekas merged commitf8746ce intosymfony:masterJun 19, 2018
nicolas-grekas added a commit that referenced this pull requestJun 19, 2018
…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
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestJun 20, 2018
This PR was merged into the master branch.Discussion----------[OptionsResolver] Document setDeprecated methodDocumenting featuresymfony/symfony#27277.Commits-------afa5e54 Document setDeprecated method from OptionsResolver component
@ycerutoyceruto deleted the options_resolver_deprecated_option branchJune 20, 2018 12:56
symfony-splitter pushed a commit to symfony/form that referenced this pull requestSep 4, 2018
…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 aftersymfony/symfony#27277.It will look like this:Use `--show-deprecated` option to show form types with deprecated options (example):![debug_types_with_deprecated_options](https://user-images.githubusercontent.com/2028198/41823977-970c7c98-77d6-11e8-9e97-30dcedc316ac.png)Use `--show-deprecated` option to show deprecated options of the given form type (example):![debug_deprecated_options_from_type](https://user-images.githubusercontent.com/2028198/41823980-a4d6bd8e-77d6-11e8-95ed-39926ffd6235.png)Deprecated option (example):![debug_deprecated_option_text](https://user-images.githubusercontent.com/2028198/41689091-04e6b7dc-74bd-11e8-87d0-90729eac4bb3.png)![debug_deprecated_option_json](https://user-images.githubusercontent.com/2028198/41689105-142b5c5c-74bd-11e8-9232-1f30237bcf69.png)Commits-------87c209d741 Show deprecated options definition on debug:form command
symfony-splitter pushed a commit to symfony/options-resolver that referenced this pull requestSep 4, 2018
…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 aftersymfony/symfony#27277.It will look like this:Use `--show-deprecated` option to show form types with deprecated options (example):![debug_types_with_deprecated_options](https://user-images.githubusercontent.com/2028198/41823977-970c7c98-77d6-11e8-9e97-30dcedc316ac.png)Use `--show-deprecated` option to show deprecated options of the given form type (example):![debug_deprecated_options_from_type](https://user-images.githubusercontent.com/2028198/41823980-a4d6bd8e-77d6-11e8-95ed-39926ffd6235.png)Deprecated option (example):![debug_deprecated_option_text](https://user-images.githubusercontent.com/2028198/41689091-04e6b7dc-74bd-11e8-87d0-90729eac4bb3.png)![debug_deprecated_option_json](https://user-images.githubusercontent.com/2028198/41689105-142b5c5c-74bd-11e8-9232-1f30237bcf69.png)Commits-------87c209d741 Show deprecated options definition on debug:form command
fabpot added a commit that referenced this pull requestSep 4, 2018
…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):![debug_types_with_deprecated_options](https://user-images.githubusercontent.com/2028198/41823977-970c7c98-77d6-11e8-9e97-30dcedc316ac.png)Use `--show-deprecated` option to show deprecated options of the given form type (example):![debug_deprecated_options_from_type](https://user-images.githubusercontent.com/2028198/41823980-a4d6bd8e-77d6-11e8-95ed-39926ffd6235.png)Deprecated option (example):![debug_deprecated_option_text](https://user-images.githubusercontent.com/2028198/41689091-04e6b7dc-74bd-11e8-87d0-90729eac4bb3.png)![debug_deprecated_option_json](https://user-images.githubusercontent.com/2028198/41689105-142b5c5c-74bd-11e8-9232-1f30237bcf69.png)Commits-------87c209d Show deprecated options definition on debug:form command
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
This was referencedNov 3, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

5 participants

@yceruto@ro0NL@nicolas-grekas@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp