8000 [OptionsResolver] Fix an error message to be more accurate by dimabory · Pull Request #30442 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[OptionsResolver] Fix an error message to be more accurate #30442

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 26, 2019
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
40 changes: 22 additions & 18 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public function offsetGet($option)

// Validate the type of the resolved option
if (isset($this->allowedTypes[$option])) {
$valid = false;
$valid = true;
$invalidTypes = [];

foreach ($this->allowedTypes[$option] as $type) {
Expand All @@ -746,13 +746,22 @@ public function offsetGet($option)
}

if (!$valid) {
$keys = array_keys($invalidTypes);
$fmtActualValue = $this->formatValue($value);
$fmtAllowedTypes = implode('" or "', $this->allowedTypes[$option]);
$fmtProvidedTypes = implode('|', array_keys($invalidTypes));

$allowedContainsArrayType = \count(array_filter(
Copy link
Member
8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this logic needed? Wouldn't something like $allowedContainsArrayType = '[]' === substr($type, -2); be enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's not enough, see test testResolveFailsIfInvalidType and data set in 575 and 576 lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh ping

$this->allowedTypes[$option],
function ($item) {
return '[]' === substr(isset(self::$typeAliases[$item]) ? self::$typeAliases[$item] : $item, -2);
}
)) > 0;

if (1 === \count($keys) && '[]' === substr($keys[0], -2)) {
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), $keys[0]));
if (\is_array($value) && $allowedContainsArrayType) {
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".', $option, $fmtActualValue, $fmtAllowedTypes, $fmtProvidedTypes));
}

throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), implode('|', array_keys($invalidTypes))));
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but is of type "%s".', $option, $fmtActualValue, $fmtAllowedTypes, $fmtProvidedTypes));
}
}

Expand Down Expand Up @@ -858,37 +867,32 @@ private function verifyArrayType($type, array $value, array &$invalidTypes, $lev
{
$type = substr($type, 0, -2);

$suffix = '[]';
while (\strlen($suffix) <= $level * 2) {
$suffix .= '[]';
}

if ('[]' === substr($type, -2)) {
$success = true;
foreach ($value as $item) {
if (!\is_array($item)) {
$invalidTypes[$this->formatTypeOf($item, null).$suffix] = true;
$invalidTypes[$this->formatTypeOf($item, null)] = true;

return false;
}

if (!$this->verifyArrayType($type, $item, $invalidTypes, $level + 1)) {
$success = false;
} elseif (!$this->verifyArrayType($type, $item, $invalidTypes, $level + 1)) {
$success = false;
}
}

return $success;
}

$valid = true;

foreach ($value as $item) {
if (!self::isValueValidType($type, $item)) {
$invalidTypes[$this->formatTypeOf($item, $type).$suffix] = $value;
$invalidTypes[$this->formatTypeOf($item, $type)] = $value;

return false;
$valid = false;
}
}

return true;
return $valid;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public function testFailIfSetAllowedTypesFromLazyOption()
public function testResolveFailsIfInvalidTypedArray()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "DateTime[]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "DateTime".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');

Expand All @@ -486,9 +486,10 @@ public function testResolveFailsWithNonArray()
public function testResolveFailsIfTypedArrayContainsInvalidTypes()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "stdClass[]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "stdClass|array|DateTime".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');

$values = range(1, 5);
$values[] = new \stdClass();
$values[] = [];
Expand All @@ -501,7 +502,7 @@ public function testResolveFailsIfTypedArrayContainsInvalidTypes()
public function testResolveFailsWithCorrectLevelsButWrongScalar()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double[][]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');

Expand Down Expand Up @@ -537,6 +538,11 @@ public function provideInvalidTypes()
[42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "integer".'],
[null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "NULL".'],
8000 ['bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'],
[['foo', 12], 'string[]', 'The option "option" with value array is expected to be of type "string[]", but one of the elements is of type "integer".'],
[123, ['string[]', 'string'], 'The option "option" with value 123 is expected to be of type "string[]" or "string", but is of type "integer".'],
[[null], ['string[]', 'string'], 'The option "option" with value array is expected to be of type "string[]" or "string", but one of the elements is of type "NULL".'],
[['string', null], ['string[]', 'string'], 'The option "option" with value array is expected to be of type "string[]" or "string", but one of the elements is of type "NULL".'],
[[\stdClass::class], ['string'], 'The option "option" with value array is expected to be of type "string", but is of type "array".'],
];
}

Expand Down Expand Up @@ -585,6 +591,7 @@ public function testResolveSucceedsIfTypedArray()
new \DateTime(),
],
];

$result = $this->resolver->resolve($data);
$this->assertEquals($data, $result);
}
Expand Down Expand Up @@ -1535,7 +1542,7 @@ public function testNested2Arrays()
public function testNestedArraysException()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer[][][][]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'float[][][][]');

Expand All @@ -1553,7 +1560,7 @@ public function testNestedArraysException()
public function testNestedArrayException1()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean|string|array".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');
$this->resolver->resolve([
Expand All @@ -1566,7 +1573,7 @@ public function testNestedArrayException1()
public function testNestedArrayException2()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean|string|array".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');
$this->resolver->resolve([
Expand All @@ -1579,7 +1586,7 @@ public function testNestedArrayException2()
public function testNestedArrayException3()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string[][]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string|integer".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[][][]');
$this->resolver->resolve([
Expand All @@ -1592,7 +1599,7 @@ public function testNestedArrayException3()
public function testNestedArrayException4()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer[][][]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[][][]');
$this->resolver->resolve([
Expand All @@ -1606,7 +1613,7 @@ public function testNestedArrayException4()
public function testNestedArrayException5()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[]", but one of the elements is of type "array[]".');
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[]", but one of the elements is of type "array".');
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[]');
$this->resolver->resolve([
Expand Down
0