Skip to content

[OptionsResolver] Passing Options argument to deprecation closure #28738

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
Oct 10, 2018
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
20 changes: 16 additions & 4 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ public function getDefinedOptions()
* Instead of passing the message, you may also pass a closure with the
* following signature:
*
* function ($value) {
* function (Options $options, $value): string {
* // ...
* }
*
* The closure receives the value as argument and should return a string.
* Returns an empty string to ignore the option deprecation.
* Return an empty string to ignore the option deprecation.
*
* The closure is invoked when {@link resolve()} is called. The parameter
* passed to the closure is the value of the option after validating it
Expand Down Expand Up @@ -860,8 +860,20 @@ public function offsetGet($option)
if (isset($this->deprecated[$option])) {
$deprecationMessage = $this->deprecated[$option];

if ($deprecationMessage instanceof \Closure && !\is_string($deprecationMessage = $deprecationMessage($value))) {
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", returns an empty string to ignore.', \gettype($deprecationMessage)));
if ($deprecationMessage instanceof \Closure) {
// If the closure is already being called, we have a cyclic dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
}

$this->calling[$option] = true;
try {
if (!\is_string($deprecationMessage = $deprecationMessage($this, $value))) {
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", return an empty string to ignore.', \gettype($deprecationMessage)));
}
} finally {
unset($this->calling[$option]);
}
}

if ('' !== $deprecationMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function testGetClosureDeprecationMessage()
{
$resolver = new OptionsResolver();
$resolver->setDefined('foo');
$resolver->setDeprecated('foo', $closure = function ($value) {});
$resolver->setDeprecated('foo', $closure = function (Options $options, $value) {});

$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame($closure, $debug->getDeprecationMessage('foo'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,19 +486,38 @@ public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid type for deprecation message, expected string but got "boolean", returns an empty string to ignore.
* @expectedExceptionMessage Invalid type for deprecation message, expected string but got "boolean", return an empty string to ignore.
*/
public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
{
$this->resolver
->setDefault('foo', true)
->setDeprecated('foo', function ($value) {
->setDeprecated('foo', function (Options $options, $value) {
return false;
})
;
$this->resolver->resolve();
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
* @expectedExceptionMessage The options "foo", "bar" have a cyclic dependency.
*/
public function testFailsIfCyclicDependencyBetweenDeprecation()
{
$this->resolver
->setDefault('foo', null)
->setDefault('bar', null)
->setDeprecated('foo', function (Options $options, $value) {
$options['bar'];
})
->setDeprecated('bar', function (Options $options, $value) {
$options['foo'];
})
;
$this->resolver->resolve();
}

public function testIsDeprecated()
{
$this->resolver
Expand Down Expand Up @@ -590,7 +609,7 @@ function (OptionsResolver $resolver) {
$resolver
->setDefault('foo', null)
->setAllowedTypes('foo', array('null', 'string', \stdClass::class))
->setDeprecated('foo', function ($value) {
->setDeprecated('foo', function (Options $options, $value) {
if ($value instanceof \stdClass) {
return sprintf('Passing an instance of "%s" to option "foo" is deprecated, pass its FQCN instead.', \stdClass::class);
}
Expand Down Expand Up @@ -621,14 +640,35 @@ function (OptionsResolver $resolver) {
function (OptionsResolver $resolver) {
$resolver
->setDefault('foo', null)
->setDeprecated('foo', function ($value) {
->setDeprecated('foo', function (Options $options, $value) {
return '';
})
;
},
array('foo' => Bar::class),
null,
);

yield 'It deprecates value depending on other option value' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefault('widget', null)
->setDefault('date_format', null)
->setDeprecated('date_format', function (Options $options, $dateFormat) {
if (null !== $dateFormat && 'single_text' === $options['widget']) {
return 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated.';
}

return '';
})
;
},
array('widget' => 'single_text', 'date_format' => 2),
array(
'type' => E_USER_DEPRECATED,
'message' => 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated.',
),
);
}

/**
Expand Down