Skip to content

Add OptionsResolver::configureOptions() #33735

@steevanb

Description

@steevanb

Description
When we want to configure an option in OptionsResolver, we have a code like this one:

$optionsResolver
    ->setDefined('foo')
    ->setAllowedTypes('foo', ['string'])
    ->setAllowedValues('foo', ['bar', 'baz'])
    ->setRequired('foo');

We have to duplicate option name and call 4 or 5 methods for each option to configure.

With only one option, it's readable, but when we have more options it's difficult to read.

Can we add a method with most used configurations?

$optionsResolver = new OptionsResolver();

# Configure a required option 
$optionsResolver->configureRequiredOption('foo', ['string'], ['bar', 'baz']);

# Equivalent to call original OptionsResolver methods:
$optionsResolver
    ->setDefined('foo')
    ->setAllowedTypes('foo', ['string'])
    ->setAllowedValues('foo', ['bar', 'baz'])
    ->setRequired('foo');

Example
I don't know if I can add a link here, my bad if it's not allowed.

I've done it in a public repository:
https://github.com/steevanb/symfony-options-resolver

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureGood first issueIdeal for your first contribution! (some Symfony experience may be required)OptionsResolverRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions