8000 bug #25763 [OptionsResolver] Fix options resolver with array allowed … · symfony/symfony@6df7d83 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6df7d83

Browse files
committed
bug #25763 [OptionsResolver] Fix options resolver with array allowed types (mcg-web)
This PR was merged into the 3.4 branch. Discussion ---------- [OptionsResolver] Fix options resolver with array allowed types | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | none When playing a simple test: ```php use Symfony\Component\OptionsResolver\Options; $resolver = new OptionsResolver(); $resolver->setDefined('foo'); $resolver->setAllowedTypes('foo', 'string[]'); $options = $resolver->resolve(['foo' => ['bar', 'baz']]); ``` I get this error: ``` Symfony\Component\OptionsResolver\Exception\InvalidOptionsException: The option "foo" with value array is expected to be of type "string[]", but is of type "string[]" ``` This PR should fix this. Commits ------- cc215f7 Fix options resolver with array allowed types
2 parents 372bc1a + cc215f7 commit 6df7d83

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ private function verifyTypes($type, $value, array &$invalidTypes)
883883
$invalidValues = array_filter( // Filter out valid values, keeping invalid values in the resulting array
884884
$value,
885885
function ($value) use ($type) {
886-
return (function_exists($isFunction = 'is_'.$type) && !$isFunction($value)) || !$value instanceof $type;
886+
return !self::isValueValidType($type, $value);
887887
}
888888
);
889889

@@ -896,7 +896,7 @@ function ($value) use ($type) {
896896
return false;
897897
}
898898

899-
if ((function_exists($isFunction = 'is_'.$type) && $isFunction($value)) || $value instanceof $type) {
899+
if (self::isValueValidType($type, $value)) {
900900
return true;
901901
}
902902

@@ -1073,4 +1073,9 @@ private function formatValues(array $values)
10731073

10741074
return implode(', ', $values);
10751075
}
1076+
1077+
private static function isValueValidType($type, $value)
1078+
{
1079+
return (function_exists($isFunction = 'is_'.$type) && $isFunction($value)) || $value instanceof $type;
1080+
}
10761081
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,15 @@ public function testSetAllowedTypesFailsIfUnknownOption()
486486
$this->resolver->setAllowedTypes('foo', 'string');
487487
}
488488

489+
public function testResolveTypedArray()
490+
{
491+
$this->resolver->setDefined('foo');
492+
$this->resolver->setAllowedTypes('foo', 'string[]');
493+
$options = $this->resolver->resolve(array('foo' => array('bar', 'baz')));
494+
495+
$this->assertSame(array('foo' => array('bar', 'baz')), $options);
496+
}
497+
489498
/**
490499
* @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
491500
*/

0 commit comments

Comments
 (0)
0