8000 #30432 provide an accurate error message for multiple allowed types · symfony/symfony@ce249ba · GitHub
[go: up one dir, main page]

Skip to content

Commit ce249ba

Browse files
committed
#30432 provide an accurate error message for multiple allowed types
#30442 (comment)
1 parent 4b22c46 commit ce249ba

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,11 @@ public function offsetGet($option)
733733
}
734734

735735
// Validate the type of the resolved option
736-
if (isset($this->allowedTypes[$option])) {
737-
$valid = false;
736+
if (isset($this->allowedTypes[$option]) && $allowedTypes = $this->allowedTypes[$option]) {
737+
$valid = true;
738738
$invalidTypes = [];
739739

740-
foreach ($this->allowedTypes[$option] as $type) {
740+
foreach ($allowedTypes as $type) {
741741
$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;
742742

743743
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
@@ -752,8 +752,8 @@ public function offsetGet($option)
752752
$option,
753753
$this->formatValue($value),
754754
implode('" or "', $this->allowedTypes[$option]),
755-
\is_array($value) && '[]' === substr($type, -2) ? 'one of the elements is' : 'is',
756-
implode('|', $invalidTypes)
755+
\is_array($value) && '[]' === substr($allowedTypes[0], -2) ? 'one of the elements is' : 'is',
756+
\count($allowedTypes) > 1 ? $invalidTypes[0] : implode('|', $invalidTypes)
757757
)
758758
);
759759
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ public function provideInvalidTypes()
572572
['bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'],
573573
[['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".'],
574574
[123, ['string[]', 'string'], 'The option "option" with value 123 is expected to be of type "string[]" or "string", but is of type "integer".'],
575-
[[123], ['string[]', 'string'], 'The option "option" with value array is expected to be of type "string[]" or "string", but is of type "integer|array".'],
575+
[[123], ['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 "integer".'],
576+
[123, ['float', 'string[]'], 'The option "option" with value 123 is expected to be of type "float" or "string[]", but is of type "integer".'],
577+
[[[123]], ['string[][]', 'int'], 'The option "option" with value array is expected to be of type "string[][]" or "int", but one of the elements is of type "integer".'],
576578
];
577579
}
578580

0 commit comments

Comments
 (0)
0