8000 Trigger deprecation only if the option is used · symfony/symfony@aeef730 · GitHub
[go: up one dir, main page]

Skip to content

Commit aeef730

Browse files
committed
Trigger deprecation only if the option is used
1 parent f0bcab5 commit aeef730

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class OptionsResolver implements Options
8888
*/
8989
private $deprecated = array();
9090

91+
/**
92+
* The list of options provided by the user.
93+
*/
94+
private $given = array();
95+
9196
/**
9297
* Whether the instance is locked for reading.
9398
*
@@ -744,6 +749,7 @@ public function resolve(array $options = array())
744749

745750
// Override options set by the user
746751
foreach ($options as $option => $value) {
752+
$clone->given[$option] = true;
747753
$clone->defaults[$option] = $value;
748754
unset($clone->resolved[$option], $clone->lazy[$option]);
749755
}
@@ -760,6 +766,13 @@ public function resolve(array $options = array())
760766
// Lock the container
761767
$clone->locked = true;
762768

769+
if ($clone->defaults && $clone->deprecated) {
770+
// Deprecated options must be evaluated last
771+
uksort($clone->defaults, function ($option) use ($clone): int {
772+
return (int) isset($clone->deprecated[$option]);
773+
});
774+
}
775+
763776
// Now process the individual options. Use offsetGet(), which resolves
764777
// the option itself and any options that the option depends on
765778
foreach ($clone->defaults as $option => $_) {
@@ -920,7 +933,8 @@ public function offsetGet($option)
920933
}
921934

922935
// Check whether the option is deprecated
923-
if (isset($this->deprecated[$option])) {
936+
// and it is provided by the user or is being called from a lazy evaluation
937+
if (isset($this->deprecated[$option]) && (isset($this->given[$option]) || $this->calling)) {
924938
$deprecationMessage = $this->deprecated[$option];
925939

926940
if ($deprecationMessage instanceof \Closure) {

src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,31 +491,30 @@ public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
491491
public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
492492
{
493493
$this->resolver
494-
->setDefault('foo', true)
494+
->setDefined('foo')
495495
->setDeprecated('foo', function (Options $options, $value) {
496496
return false;
497497
})
498498
;
499-
$this->resolver->resolve();
499+
$this->resolver->resolve(array('foo' => null));
500500
}
501501

502502
/**
503503
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
504-
* @expectedExceptionMessage The options "foo", "bar" have a cyclic dependency.
504+
* @expectedExceptionMessage The options "bar", "foo" have a cyclic dependency.
505505
*/
506506
public function testFailsIfCyclicDependencyBetweenDeprecation()
507507
{
508508
$this->resolver
509-
->setDefault('foo', null)
510-
->setDefault('bar', null)
509+
->setDefined(array('foo', 'bar'))
511510
->setDeprecated('foo', function (Options $options, $value) {
512511
$options['bar'];
513512
})
514513
->setDeprecated('bar', function (Options $options, $value) {
515514
$options['foo'];
516515
})
517516
;
518-
$this->resolver->resolve();
517+
$this->resolver->resolve(array('foo' => null, 'bar' => null));
519518
}
520519

521520
public function testIsDeprecated()
@@ -590,14 +589,30 @@ function (OptionsResolver $resolver) {
590589
),
591590
);
592591

593-
yield 'It deprecates a missing option with default value' => array(
592+
yield 'It does not deprecates a missing option with default value' => array(
594593
function (OptionsResolver $resolver) {
595594
$resolver
596-
->setDefaults(array('foo' => null, 'bar' => null))
595+
->setDefault('foo', null)
597596
->setDeprecated('foo')
598597
;
599598
},
600-
array('bar' => 'baz'),
599+
array(),
600+
null,
601+
);
602+
603+
yield 'It deprecates an option evaluated in another definition' => array(
604+
function (OptionsResolver $resolver) {
605+
// defined by superclass
606+
$resolver
607+
->setDefault('foo', null)
608+
->setDeprecated('foo')
609+
;
610+
// defined by subclass
611+
$resolver->setDefault('bar', function (Options $options) {
612+
return $options['foo'];
613+
});
614+
},
615+
array(),
601616
array(
602617
'type' => E_USER_DEPRECATED,
603618
'message' => 'The option "foo" is deprecated.',

0 commit comments

Comments
 (0)
0