Skip to content

Commit bba93ae

Browse files
Leverage PHP8's get_debug_type()
1 parent 1942f69 commit bba93ae

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

OptionsResolver.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public function setDeprecated(string $option, $deprecationMessage = 'The option
434434
}
435435

436436
if (!\is_string($deprecationMessage) && !$deprecationMessage instanceof \Closure) {
437-
throw new InvalidArgumentException(sprintf('Invalid type for deprecation message argument, expected string or \Closure, but got "%s".', \gettype($deprecationMessage)));
437+
throw new InvalidArgumentException(sprintf('Invalid type for deprecation message argument, expected string or \Closure, but got "%s".', get_debug_type($deprecationMessage)));
438438
}
439439

440440
// ignore if empty string
@@ -929,7 +929,7 @@ public function offsetGet($option, bool $triggerDeprecation = true)
929929
}
930930

931931
if (!\is_array($value)) {
932-
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $this->formatOptions([$option]), $this->formatValue($value), $this->formatTypeOf($value)));
932+
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $this->formatOptions([$option]), $this->formatValue($value), get_debug_type($value)));
933933
}
934934

935935
// The following section must be protected from cyclic calls.
@@ -1059,7 +1059,7 @@ public function offsetGet($option, bool $triggerDeprecation = true)
10591059
$this->calling[$option] = true;
10601060
try {
10611061
if (!\is_string($deprecationMessage = $deprecationMessage($this, $value))) {
1062-
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", return an empty string to ignore.', \gettype($deprecationMessage)));
1062+
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", return an empty string to ignore.', get_debug_type($deprecationMessage)));
10631063
}
10641064
} finally {
10651065
unset($this->calling[$option]);
@@ -1120,7 +1120,7 @@ private function verifyTypes(string $type, $value, array &$invalidTypes, int $le
11201120
}
11211121

11221122
if (!$invalidTypes || $level > 0) {
1123-
$invalidTypes[$this->formatTypeOf($value)] = true;
1123+
$invalidTypes[get_debug_type($value)] = true;
11241124
}
11251125

11261126
return false;
@@ -1186,18 +1186,6 @@ public function count()
11861186
return \count($this->defaults);
11871187
}
11881188

1189-
/**
1190-
* Returns a string representation of the type of the value.
1191-
*
1192-
* @param mixed $value The value to return the type of
1193-
*
1194-
* @return string The type of the value
1195-
*/
1196-
private function formatTypeOf($value): string
1197-
{
1198-
return \is_object($value) ? \get_class($value) : \gettype($value);
1199-
}
1200-
12011189
/**
12021190
* Returns a string representation of the value.
12031191
*

Tests/OptionsResolverTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public function testSetDeprecatedFailsIfUnknownOption()
460460
public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
461461
{
462462
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidArgumentException');
463-
$this->expectExceptionMessage('Invalid type for deprecation message argument, expected string or \Closure, but got "boolean".');
463+
$this->expectExceptionMessage('Invalid type for deprecation message argument, expected string or \Closure, but got "bool".');
464464
$this->resolver
465465
->setDefined('foo')
466466
->setDeprecated('foo', true)
@@ -470,7 +470,7 @@ public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
470470
public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
471471
{
472472
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidArgumentException');
473-
$this->expectExceptionMessage('Invalid type for deprecation message, expected string but got "boolean", return an empty string to ignore.');
473+
$this->expectExceptionMessage('Invalid type for deprecation message, expected string but got "bool", return an empty string to ignore.');
474474
$this->resolver
475475
->setDefined('foo')
476476
->setDeprecated('foo', function (Options $options, $value) {
@@ -814,7 +814,7 @@ public function testResolveFailsIfTypedArrayContainsInvalidTypes()
814814
public function testResolveFailsWithCorrectLevelsButWrongScalar()
815815
{
816816
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
817-
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double".');
817+
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "float".');
818818
$this->resolver->setDefined('foo');
819819
$this->resolver->setAllowedTypes('foo', 'int[][]');
820820

@@ -842,18 +842,18 @@ public function testResolveFailsIfInvalidType($actualType, $allowedType, $except
842842
public function provideInvalidTypes()
843843
{
844844
return [
845-
[true, 'string', 'The option "option" with value true is expected to be of type "string", but is of type "boolean".'],
846-
[false, 'string', 'The option "option" with value false is expected to be of type "string", but is of type "boolean".'],
847-
[fopen(__FILE__, 'r'), 'string', 'The option "option" with value resource is expected to be of type "string", but is of type "resource".'],
845+
[true, 'string', 'The option "option" with value true is expected to be of type "string", but is of type "bool".'],
846+
[false, 'string', 'The option "option" with value false is expected to be of type "string", but is of type "bool".'],
847+
[fopen(__FILE__, 'r'), 'string', 'The option "option" with value resource is expected to be of type "string", but is of type "resource (stream)".'],
848848
[[], 'string', 'The option "option" with value array is expected to be of type "string", but is of type "array".'],
849849
[new OptionsResolver(), 'string', 'The option "option" with value Symfony\Component\OptionsResolver\OptionsResolver is expected to be of type "string", but is of type "Symfony\Component\OptionsResolver\OptionsResolver".'],
850-
[42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "integer".'],
851-
[null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "NULL".'],
850+
[42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "int".'],
851+
[null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "null".'],
852852
['bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'],
853-
[['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".'],
854-
[123, ['string[]', 'string'], 'The option "option" with value 123 is expected to be of type "string[]" or "string", but is of type "integer".'],
855-
[[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".'],
856-
[['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".'],
853+
[['foo', 12], 'string[]', 'The option "option" with value array is expected to be of type "string[]", but one of the elements is of type "int".'],
854+
[123, ['string[]', 'string'], 'The option "option" with value 123 is expected to be of type "string[]" or "string", but is of type "int".'],
855+
[[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".'],
856+
[['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".'],
857857
[[\stdClass::class], ['string'], 'The option "option" with value array is expected to be of type "string", but is of type "array".'],
858858
];
859859
}
@@ -869,7 +869,7 @@ public function testResolveSucceedsIfValidType()
869869
public function testResolveFailsIfInvalidTypeMultiple()
870870
{
871871
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
872-
$this->expectExceptionMessage('The option "foo" with value 42 is expected to be of type "string" or "bool", but is of type "integer".');
872+
$this->expectExceptionMessage('The option "foo" with value 42 is expected to be of type "string" or "bool", but is of type "int".');
873873
$this->resolver->setDefault('foo', 42);
874874
$this->resolver->setAllowedTypes('foo', ['string', 'bool']);
875875

@@ -1906,7 +1906,7 @@ public function testNested2Arrays()
19061906
public function testNestedArraysException()
19071907
{
19081908
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
1909-
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer".');
1909+
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "int".');
19101910
$this->resolver->setDefined('foo');
19111911
$this->resolver->setAllowedTypes('foo', 'float[][][][]');
19121912

@@ -1924,7 +1924,7 @@ public function testNestedArraysException()
19241924
public function testNestedArrayException1()
19251925
{
19261926
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
1927-
$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".');
1927+
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "bool|string|array".');
19281928
$this->resolver->setDefined('foo');
19291929
$this->resolver->setAllowedTypes('foo', 'int[][]');
19301930
$this->resolver->resolve([
@@ -1937,7 +1937,7 @@ public function testNestedArrayException1()
19371937
public function testNestedArrayException2()
19381938
{
19391939
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
1940-
$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".');
1940+
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "bool|string|array".');
19411941
$this->resolver->setDefined('foo');
19421942
$this->resolver->setAllowedTypes('foo', 'int[][]');
19431943
$this->resolver->resolve([
@@ -1950,7 +1950,7 @@ public function testNestedArrayException2()
19501950
public function testNestedArrayException3()
19511951
{
19521952
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
1953-
$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".');
1953+
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string|int".');
19541954
$this->resolver->setDefined('foo');
19551955
$this->resolver->setAllowedTypes('foo', 'string[][][]');
19561956
$this->resolver->resolve([
@@ -1963,7 +1963,7 @@ public function testNestedArrayException3()
19631963
public function testNestedArrayException4()
19641964
{
19651965
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
1966-
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer".');
1966+
$this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "int".');
19671967
$this->resolver->setDefined('foo');
19681968
$this->resolver->setAllowedTypes('foo', 'string[][][]');
19691969
$this->resolver->resolve([
@@ -2031,7 +2031,7 @@ public function testFailsIfMissingRequiredNestedOption()
20312031
public function testFailsIfInvalidTypeNestedOption()
20322032
{
20332033
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
2034-
$this->expectExceptionMessage('The option "database[logging]" with value null is expected to be of type "bool", but is of type "NULL".');
2034+
$this->expectExceptionMessage('The option "database[logging]" with value null is expected to be of type "bool", but is of type "null".');
20352035
$this->resolver->setDefaults([
20362036
'name' => 'default',
20372037
'database' => function (OptionsResolver $resolver) {
@@ -2048,7 +2048,7 @@ public function testFailsIfInvalidTypeNestedOption()
20482048
public function testFailsIfNotArrayIsGivenForNestedOptions()
20492049
{
20502050
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
2051-
$this->expectExceptionMessage('The nested option "database" with value null is expected to be of type array, but is of type "NULL".');
2051+
$this->expectExceptionMessage('The nested option "database" with value null is expected to be of type array, but is of type "null".');
20522052
$this->resolver->setDefaults([
20532053
'name' => 'default',
20542054
'database' => function (OptionsResolver $resolver) {

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"require": {
1919
"php": "^7.2.5",
20-
"symfony/deprecation-contracts": "^2.1"
20+
"symfony/deprecation-contracts": "^2.1",
21+
"symfony/polyfill-php80": "^1.15"
2122
},
2223
"autoload": {
2324
"psr-4": { "Symfony\\Component\\OptionsResolver\\": "" },

0 commit comments

Comments
 (0)