Skip to content

Commit cba9d94

Browse files
drealecsnicolas-grekas
authored andcommitted
Remove some magic from TypeValidator logic and OptionsResolver type verify logic
1 parent 0ab7e5a commit cba9d94

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

OptionsResolver.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@
2727
*/
2828
class OptionsResolver implements Options
2929
{
30+
private const VALIDATION_FUNCTIONS = [
31+
'bool' => 'is_bool',
32+
'boolean' => 'is_bool',
33+
'int' => 'is_int',
34+
'integer' => 'is_int',
35+
'long' => 'is_int',
36+
'float' => 'is_float',
37+
'double' => 'is_float',
38+
'real' => 'is_float',
39+
'numeric' => 'is_numeric',
40+
'string' => 'is_string',
41+
'scalar' => 'is_scalar',
42+
'array' => 'is_array',
43+
'iterable' => 'is_iterable',
44+
'countable' => 'is_countable',
45+
'callable' => 'is_callable',
46+
'object' => 'is_object',
47+
'resource' => 'is_resource',
48+
];
49+
3050
/**
3151
* The names of all defined options.
3252
*/
@@ -110,12 +130,6 @@ class OptionsResolver implements Options
110130

111131
private $parentsOptions = [];
112132

113-
private static $typeAliases = [
114-
'boolean' => 'bool',
115-
'integer' => 'int',
116-
'double' => 'float',
117-
];
118-
119133
/**
120134
* Sets the default value of a given option.
121135
*
@@ -995,8 +1009,6 @@ public function offsetGet($option, bool $triggerDeprecation = true)
9951009
$invalidTypes = [];
9961010

9971011
foreach ($this->allowedTypes[$option] as $type) {
998-
$type = self::$typeAliases[$type] ?? $type;
999-
10001012
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
10011013
break;
10021014
}
@@ -1007,7 +1019,7 @@ public function offsetGet($option, bool $triggerDeprecation = true)
10071019
$fmtAllowedTypes = implode('" or "', $this->allowedTypes[$option]);
10081020
$fmtProvidedTypes = implode('|', array_keys($invalidTypes));
10091021
$allowedContainsArrayType = \count(array_filter($this->allowedTypes[$option], static function ($item) {
1010-
return '[]' === substr(self::$typeAliases[$item] ?? $item, -2);
1022+
return '[]' === substr($item, -2);
10111023
})) > 0;
10121024

10131025
if (\is_array($value) && $allowedContainsArrayType) {
@@ -1135,7 +1147,7 @@ private function verifyTypes(string $type, $value, array &$invalidTypes, int $le
11351147
return $valid;
11361148
}
11371149

1138-
if (('null' === $type && null === $value) || (\function_exists($func = 'is_'.$type) && $func($value)) || $value instanceof $type) {
1150+
if (('null' === $type && null === $value) || (isset(self::VALIDATION_FUNCTIONS[$type]) ? self::VALIDATION_FUNCTIONS[$type]($value) : $value instanceof $type)) {
11391151
return true;
11401152
}
11411153

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require": {
1919
"php": "^7.2.5",
2020
"symfony/deprecation-contracts": "^2.1",
21+
"symfony/polyfill-php73": "~1.0",
2122
"symfony/polyfill-php80": "^1.15"
2223
},
2324
"autoload": {

0 commit comments

Comments
 (0)