Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4bff68

Browse files
author
Cas Leentfaar
committed
clarified exception message to show the actual type passed to the resolver
further improved message of the exception
1 parent a7d52fd commit e4bff68

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

src/Symfony/Component/OptionsResolver/Options.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,64 @@ public static function validateTypes(array $options, array $acceptedTypes)
241241
}
242242
}
243243

244-
$printableValue = is_object($value)
245-
? get_class($value)
246-
: (is_array($value)
247-
? 'Array'
248-
: (string) $value);
249-
250244
throw new InvalidOptionsException(sprintf(
251245
'The option "%s" with value "%s" is expected to be of type "%s"',
252246
$option,
253-
$printableValue,
254-
implode('", "', $optionTypes)
247+
self::createPrintableValue($value),
248+
implode('", "', array_map(function ($optionType) { return self::createPrintableType($optionType); }, $optionTypes))
255249
));
256250
}
257251
}
258252

253+
/**
254+
* Attempts to convert a given value into a (short) string or integer.
255+
*
256+
* @param mixed $value The value to convert.
257+
*
258+
* @return string The printable value.
259+
*/
260+
private static function createPrintableValue($value)
261+
{
262+
if (is_object($value)) {
263+
return get_class($value);
264+
}
265+
266+
if (is_array($value)) {
267+
return 'Array';
268+
}
269+
270+
if (null === $value) {
271+
return 'null';
272+
}
273+
274+
if (is_string($value)) {
275+
return '"'.$value.'"';
276+
}
277+
278+
if (is_bool($value)) {
279+
return (string) var_export($value, true);
280+
}
281+
282+
return (string) $value;
283+
}
284+
285+
/**
286+
* Attempts to convert a given value's type into a printable string.
287+
*
288+
* @param mixed $value The value to convert.
289+
*
290+
* @return string Objects get returned as their classname,
291+
* all other values are put through gettype().
292+
*/
293+
private static function createPrintableType($value)
294+
{
295+
if (is_object($value)) {
296+
return get_class($value);
297+
} else {
298+
return gettype($value);
299+
}
300+
}
301+
259302
/**
260303
* Validates that the given option values match the accepted values and
261304
* throws an exception otherwise.

0 commit comments

Comments
 (0)