Skip to content

[OptionsResolver] Fixed unexpected behavior when using setOptional + setNormalizers #6233

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

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 20 additions & 18 deletions src/Symfony/Component/OptionsResolver/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,28 +484,30 @@ private function resolve($option)
*/
private function normalize($option)
{
// The code duplication with resolve() exists for performance
// reasons, in order to save a method call.
// Remember that this method is potentially called a couple of thousand
// times and needs to be as efficient as possible.
if (isset($this->lock[$option])) {
$conflicts = array();

foreach ($this->lock as $option => $locked) {
if ($locked) {
$conflicts[] = $option;
if (array_key_exists($option, $this->options)) {
// The code duplication with resolve() exists for performance
// reasons, in order to save a method call.
// Remember that this method is potentially called a couple of thousand
// times and needs to be as efficient as possible.
if (isset($this->lock[$option])) {
$conflicts = array();

foreach ($this->lock as $option => $locked) {
if ($locked) {
$conflicts[] = $option;
}
}
}

throw new OptionDefinitionException('The options "' . implode('", "', $conflicts) . '" have a cyclic dependency.');
}
throw new OptionDefinitionException('The options "' . implode('", "', $conflicts) . '" have a cyclic dependency.');
}

/** @var \Closure $normalizer */
$normalizer = $this->normalizers[$option];
/** @var \Closure $normalizer */
$normalizer = $this->normalizers[$option];

$this->lock[$option] = true;
$this->options[$option] = $normalizer($this, array_key_exists($option, $this->options) ? $this->options[$option] : null);
unset($this->lock[$option]);
$this->lock[$option] = true;
$this->options[$option] = $normalizer($this, $this->options[$option]);
unset($this->lock[$option]);
}

// The option is now normalized
unset($this->normalizers[$option]);
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/OptionsResolver/Tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,8 @@ public function testNormalizerWithoutCorrespondingOption()
$test = $this;

$this->options->setNormalizer('foo', function (Options $options, $previousValue) use ($test) {
$test->assertNull($previousValue);

return '';
$test->fail('Should not be called');
});
$this->assertEquals(array('foo' => ''), $this->options->all());
$this->assertEquals(array(), $this->options->all());
}
}