Skip to content

Commit e25bc4b

Browse files
minor #36799 Remove some magic from TypeValidator logic and OptionsResolver type verify logic (drealecs)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- Remove some magic from TypeValidator logic and OptionsResolver type verify logic | Q | A | ------------- | --- | Branch? | 5.0 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Following discussion on https://twitter.com/Ocramius/status/1260236117129015297 The changes in this PR could ensure that types that can be verified by the TypeValidator is limited and there is no way to magically use it. For example you can break it by defining you own function like: ```php function is_usercollection { return true; } ``` that will validate any value that have the validator type as 'UserCollection'. Another example would be defining `is_lower` or `ctype_int` that would also break the implementation. Commits ------- e8c9049a5a Remove some magic from TypeValidator logic and OptionsResolver type verify logic
2 parents 62e2fbb + cba9d94 commit e25bc4b

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
*
@@ -996,8 +1010,6 @@ public function offsetGet($option, bool $triggerDeprecation = true)
9961010
$invalidTypes = [];
9971011

9981012
foreach ($this->allowedTypes[$option] as $type) {
999-
$type = self::$typeAliases[$type] ?? $type;
1000-
10011013
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
10021014
break;
10031015
}
@@ -1008,7 +1020,7 @@ public function offsetGet($option, bool $triggerDeprecation = true)
10081020
$fmtAllowedTypes = implode('" or "', $this->allowedTypes[$option]);
10091021
$fmtProvidedTypes = implode('|', array_keys($invalidTypes));
10101022
$allowedContainsArrayType = \count(array_filter($this->allowedTypes[$option], static function ($item) {
1011-
return '[]' === substr(self::$typeAliases[$item] ?? $item, -2);
1023+
return '[]' === substr($item, -2);
10121024
})) > 0;
10131025

10141026
if (\is_array($value) && $allowedContainsArrayType) {
@@ -1136,7 +1148,7 @@ private function verifyTypes(string $type, $value, array &$invalidTypes, int $le
11361148
return $valid;
11371149
}
11381150

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

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)