|
24 | 24 | use __PHP_Incomplete_Class;
|
25 | 25 | use InvalidArgumentException;
|
26 | 26 | use TypeError;
|
| 27 | +use UnexpectedValueException; |
| 28 | +use function class_implements; |
| 29 | +use function get_class; |
| 30 | +use function get_parent_class; |
| 31 | +use function get_resource_type; |
| 32 | +use function is_array; |
| 33 | +use function is_bool; |
| 34 | +use function is_float; |
| 35 | +use function is_int; |
| 36 | +use function is_object; |
| 37 | +use function is_resource; |
| 38 | +use function is_string; |
| 39 | +use function key; |
| 40 | +use function strpos; |
27 | 41 |
|
28 | 42 | /**
|
29 | 43 | * Convenience trait including simple assertions and error reporting functions
|
@@ -51,40 +65,16 @@ private static function assertClass(string $varName, $classNames, $userInput): v
|
51 | 65 | }
|
52 | 66 |
|
53 | 67 | /**
|
54 |
| - * Get debug type method stolen from the symfony polyfill |
| 68 | + * Get debug type method stolen and refactored from the symfony polyfill |
55 | 69 | *
|
56 | 70 | * @see https://github.com/symfony/polyfill/blob/main/src/Php80/Php80.php
|
57 | 71 | */
|
58 | 72 | public static function getDebugType($value): string
|
59 | 73 | {
|
60 |
| - switch (true) { |
61 |
| - case null === $value: return 'null'; |
62 |
| - case is_bool($value): return 'bool'; |
63 |
| - case is_string($value): return 'string'; |
64 |
| - case is_array($value): return 'array'; |
65 |
| - case is_int($value): return 'int'; |
66 |
| - case is_float($value): return 'float'; |
67 |
| - case is_object($value): break; |
68 |
| - case $value instanceof __PHP_Incomplete_Class: return '__PHP_Incomplete_Class'; |
69 |
| - default: |
70 |
| - if (null === $type = @get_resource_type($value)) { |
71 |
| - return 'unknown'; |
72 |
| - } |
73 |
| - |
74 |
| - if ('Unknown' === $type) { |
75 |
| - $type = 'closed'; |
76 |
| - } |
77 |
| - |
78 |
| - return "resource ($type)"; |
79 |
| - } |
80 |
| - |
81 |
| - $class = get_class($value); |
82 |
| - |
83 |
| - if (false === strpos($class, '@')) { |
84 |
| - return $class; |
85 |
| - } |
86 |
| - |
87 |
| - return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous'; |
| 74 | + return self::detectScalar($value) |
| 75 | + ?? self::detectClass($value) |
| 76 | + ?? self::detectResource($value) |
| 77 | + ?? 'unknown'; |
88 | 78 | }
|
89 | 79 |
|
90 | 80 | /**
|
@@ -159,4 +149,90 @@ private static function getTypeErrorText(string $varName, array $classNames, $us
|
159 | 149 | self::getDebugType($userInput)
|
160 | 150 | );
|
161 | 151 | }
|
| 152 | + |
| 153 | + /** |
| 154 | + * Returns the name of the scalar type of the value if it is one. |
| 155 | + * |
| 156 | + * @param mixed $value |
| 157 | + * @return string|null |
| 158 | + */ |
| 159 | + private static function detectScalar($value): ?string |
| 160 | + { |
| 161 | + if ($value === null) { |
| 162 | + return 'null'; |
| 163 | + } |
| 164 | + |
| 165 | + if (is_bool($value)) { |
| 166 | + return 'bool'; |
| 167 | + } |
| 168 | + |
| 169 | + if (is_string($value)) { |
| 170 | + return 'string'; |
| 171 | + } |
| 172 | + |
| 173 | + if (is_array($value)) { |
| 174 | + return 'array'; |
| 175 | + } |
| 176 | + |
| 177 | + if (is_int($value)) { |
| 178 | + return 'int'; |
| 179 | + } |
| 180 | + |
| 181 | + if (is_float($value)) { |
| 182 | + return 'float'; |
| 183 | + } |
| 184 | + |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns the name of the class of the value if it is one. |
| 190 | + * |
| 191 | + * @param mixed $value |
| 192 | + * |
| 193 | + * @return string|null |
| 194 | + */ |
| 195 | + private static function detectClass($value): ?string |
| 196 | + { |
| 197 | + if ($value instanceof __PHP_Incomplete_Class) { |
| 198 | + return '__PHP_Incomplete_Class'; |
| 199 | + } |
| 200 | + |
| 201 | + if (is_object($value)) { |
| 202 | + $class = get_class($value); |
| 203 | + |
| 204 | + if (false === strpos($class, '@')) { |
| 205 | + return $class; |
| 206 | + } |
| 207 | + |
| 208 | + return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous'; |
| 209 | + } |
| 210 | + |
| 211 | + return null; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns the name of the resource of the value if it is one. |
| 216 | + * |
| 217 | + * @param mixed $value |
| 218 | + * |
| 219 | + * @return string|null |
| 220 | + */ |
| 221 | + private static function detectResource($value): ?string |
| 222 | + { |
| 223 | + if (is_resource($value)) { |
| 224 | + $type = @get_resource_type($value); |
| 225 | + if (null === $type) { |
| 226 | + return 'unknown'; |
| 227 | + } |
| 228 | + |
| 229 | + if ('Unknown' === $type) { |
| 230 | + $type = 'closed'; |
| 231 | + } |
| 232 | + |
| 233 | + return "resource ($type)"; |
| 234 | + } |
| 235 | + |
| 236 | + return null; |
| 237 | + } |
162 | 238 | }
|
0 commit comments