Raphhh /
trex-reflection
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace TRex\Reflection; |
||
| 3 | |||
| 4 | use \InvalidArgumentException; |
||
| 5 | |||
| 6 | |||
| 7 | /** |
||
| 8 | * TypeReflection reflect the types. |
||
| 9 | * |
||
| 10 | * @package TRex\Reflection |
||
| 11 | * @author Raphaël Lefebvre <[email protected]> |
||
| 12 | * @transient |
||
| 13 | */ |
||
| 14 | class TypeReflection |
||
| 15 | { |
||
| 16 | |||
| 17 | const VOID_TYPE = 'void'; |
||
| 18 | const MIXED_TYPE = 'mixed'; |
||
| 19 | const NULL_TYPE = 'null'; |
||
| 20 | const BOOLEAN_TYPE = 'boolean'; |
||
| 21 | const STRING_TYPE = 'string'; |
||
| 22 | const INTEGER_TYPE = 'integer'; |
||
| 23 | const FLOAT_TYPE = 'float'; |
||
| 24 | const NUMBER_TYPE = 'number'; |
||
| 25 | const SCALAR_TYPE = 'scalar'; |
||
| 26 | const ARRAY_TYPE = 'array'; |
||
| 27 | const OBJECT_TYPE = 'object'; |
||
| 28 | const RESOURCE_TYPE = 'resource'; |
||
| 29 | const CALLABLE_TYPE = 'callable'; |
||
| 30 | const UNKNOWN_TYPE = 'unknown type'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $typeMappingList = array( |
||
| 36 | self::VOID_TYPE => array( |
||
| 37 | 'void', |
||
| 38 | ), |
||
| 39 | self::MIXED_TYPE => array( |
||
| 40 | 'mixed', |
||
| 41 | ), |
||
| 42 | self::NULL_TYPE => array( |
||
| 43 | 'null', |
||
| 44 | ), |
||
| 45 | self::BOOLEAN_TYPE => array( |
||
| 46 | 'bool', |
||
| 47 | 'boolean', |
||
| 48 | ), |
||
| 49 | self::STRING_TYPE => array( |
||
| 50 | 'string', |
||
| 51 | ), |
||
| 52 | self::INTEGER_TYPE => array( |
||
| 53 | 'int', |
||
| 54 | 'integer', |
||
| 55 | 'long', |
||
| 56 | ), |
||
| 57 | self::FLOAT_TYPE => array( |
||
| 58 | 'float', |
||
| 59 | 'double', |
||
| 60 | 'real', |
||
| 61 | ), |
||
| 62 | self::NUMBER_TYPE => array( |
||
| 63 | 'int', |
||
| 64 | 'integer', |
||
| 65 | 'long', |
||
| 66 | 'float', |
||
| 67 | 'double', |
||
| 68 | 'real', |
||
| 69 | ), |
||
| 70 | self::SCALAR_TYPE => array( |
||
| 71 | 'bool', |
||
| 72 | 'boolean', |
||
| 73 | 'string', |
||
| 74 | 'int', |
||
| 75 | 'integer', |
||
| 76 | 'long', |
||
| 77 | 'float', |
||
| 78 | 'double', |
||
| 79 | 'real', |
||
| 80 | ), |
||
| 81 | self::ARRAY_TYPE => array( |
||
| 82 | 'array', |
||
| 83 | ), |
||
| 84 | self::OBJECT_TYPE => array( |
||
| 85 | 'object', |
||
| 86 | ), |
||
| 87 | self::RESOURCE_TYPE => array( |
||
| 88 | 'resource', |
||
| 89 | ), |
||
| 90 | self::CALLABLE_TYPE => array( |
||
| 91 | 'callable', |
||
| 92 | ), |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Reflected type or class name |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | private $type; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | private $enableAutoload; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param mixed $variable |
||
| 109 | * @return TypeReflection |
||
| 110 | */ |
||
| 111 | 5 | public static function createFromVariable($variable) |
|
| 112 | { |
||
| 113 | 5 | return new self(is_object($variable) ? get_class($variable) : strtolower(gettype($variable))); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Constructor. |
||
| 118 | * |
||
| 119 | * @param string $type Reflected type or class name |
||
| 120 | * @param bool $enableAutoload |
||
| 121 | */ |
||
| 122 | 11 | public function __construct($type, $enableAutoload = false) |
|
| 123 | { |
||
| 124 | 11 | $this->setType($type); |
|
| 125 | 11 | $this->enableAutoload = $enableAutoload; |
|
| 126 | 11 | } |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Getter of $enableAutoload |
||
| 130 | * |
||
| 131 | * @return boolean |
||
| 132 | */ |
||
| 133 | 1 | public function isAutoloadEnabled() |
|
| 134 | { |
||
| 135 | 1 | return $this->enableAutoload; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Setter of $enableAutoload |
||
| 140 | * |
||
| 141 | * @param boolean $enableAutoload |
||
| 142 | */ |
||
| 143 | public function setEnableAutoload($enableAutoload) |
||
| 144 | { |
||
| 145 | $this->enableAutoload = (boolean)$enableAutoload; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Gets the reflected type. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 11 | public function getType() |
|
| 154 | { |
||
| 155 | 11 | return $this->type; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Depending on the type, returns a standardized type, a constant value of XXX_TYPE. |
||
| 160 | * Note that if the type is a class name, object type is returned. |
||
| 161 | * |
||
| 162 | * |
||
| 163 | * @return string |
||
|
0 ignored issues
–
show
|
|||
| 164 | */ |
||
| 165 | 1 | public function getStandardizedType() |
|
| 166 | { |
||
| 167 | 1 | foreach ($this->getStandardizedTypes() as $standardizedType) { |
|
| 168 | 1 | if ($this->is($standardizedType)) { |
|
| 169 | 1 | return $standardizedType; |
|
| 170 | } |
||
| 171 | 1 | } |
|
| 172 | 1 | return self::UNKNOWN_TYPE; |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Indicates if the type is recognized. |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function isValid() |
||
| 181 | { |
||
| 182 | return !$this->is(self::UNKNOWN_TYPE); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Indicates whether the reflected type is void. |
||
| 187 | * Valid values are: |
||
| 188 | * - void |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function isVoid() |
||
| 193 | { |
||
| 194 | return $this->is(self::VOID_TYPE); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Indicates whether the reflected type is mixed. |
||
| 199 | * Valid values are: |
||
| 200 | * - mixed |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function isMixed() |
||
| 205 | { |
||
| 206 | return $this->is(self::MIXED_TYPE); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Indicates whether the reflected type is null. |
||
| 211 | * Valid values are: |
||
| 212 | * - null |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function isNull() |
||
| 217 | { |
||
| 218 | return $this->is(self::NULL_TYPE); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Indicates whether the reflected type is boolean. |
||
| 223 | * Valid values are: |
||
| 224 | * - bool |
||
| 225 | * - boolean |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function isBoolean() |
||
| 230 | { |
||
| 231 | return $this->is(self::BOOLEAN_TYPE); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Indicates whether the reflected type is string. |
||
| 236 | * Valid values are: |
||
| 237 | * - string |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function isString() |
||
| 242 | { |
||
| 243 | return $this->is(self::STRING_TYPE); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Indicates whether the reflected type is integer. |
||
| 248 | * Valid values are |
||
| 249 | * - int |
||
| 250 | * - integer |
||
| 251 | * - long |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 1 | public function isInteger() |
|
| 256 | { |
||
| 257 | 1 | return $this->is(self::INTEGER_TYPE); |
|
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Indicates whether the reflected type is float. |
||
| 262 | * Valid values are |
||
| 263 | * - float |
||
| 264 | * - double |
||
| 265 | * - real |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 1 | public function isFloat() |
|
| 270 | { |
||
| 271 | 1 | return $this->is(self::FLOAT_TYPE); |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Indicates whether the reflected type is number. |
||
| 276 | * Valid values are: |
||
| 277 | * - every value valid for int |
||
| 278 | * - every value valid for float |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 1 | public function isNumber() |
|
| 283 | { |
||
| 284 | 1 | return $this->is(self::NUMBER_TYPE); |
|
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Indicates whether the reflected type is scalar. |
||
| 289 | * Valid values are: |
||
| 290 | * - every value valid for boolean |
||
| 291 | * - every value valid for string |
||
| 292 | * - every value valid for int |
||
| 293 | * - every value valid for float |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 1 | public function isScalar() |
|
| 298 | { |
||
| 299 | 1 | return $this->is(self::SCALAR_TYPE); |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Indicates whether the reflected type is array. |
||
| 304 | * Valid values are: |
||
| 305 | * - array |
||
| 306 | * - xxx[] |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function isArray() |
||
| 311 | { |
||
| 312 | return $this->is(self::ARRAY_TYPE); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Indicates whether the reflected type is object. |
||
| 317 | * Valid values are: |
||
| 318 | * - object |
||
| 319 | * - valid class name |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isObject() |
||
| 324 | { |
||
| 325 | return $this->is(self::OBJECT_TYPE); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Indicates whether the reflected type is resource. |
||
| 330 | * Valid values are: |
||
| 331 | * - resource |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function isResource() |
||
| 336 | { |
||
| 337 | return $this->is(self::RESOURCE_TYPE); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Indicates whether the reflected type is callable. |
||
| 342 | * Valid values are: |
||
| 343 | * - callable |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function isCallable() |
||
| 348 | { |
||
| 349 | return $this->is(self::CALLABLE_TYPE); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Indicates whether the reflected type is $standardizedType. |
||
| 354 | * |
||
| 355 | * @param string $standardizedType see specific const. |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | 6 | public function is($standardizedType) |
|
| 359 | { |
||
| 360 | 6 | if ($standardizedType === self::OBJECT_TYPE && class_exists($this->getType(), $this->isAutoloadEnabled())) { |
|
| 361 | 1 | return true; |
|
| 362 | } |
||
| 363 | 6 | if ($standardizedType === self::ARRAY_TYPE && stripos($this->getType(), '[]') !== false) { |
|
| 364 | 1 | return true; |
|
| 365 | } |
||
| 366 | 6 | return $this->isInTypeMapping($standardizedType); |
|
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Setter of $type. |
||
| 371 | * |
||
| 372 | * @param string $type |
||
| 373 | */ |
||
| 374 | 11 | private function setType($type) |
|
| 375 | { |
||
| 376 | 11 | $this->type = (string)$type; |
|
| 377 | 11 | } |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Getter of $typeMappingList. |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | 6 | private function getTypeMappingList() |
|
| 385 | { |
||
| 386 | 6 | return $this->typeMappingList; |
|
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns a type mapping. |
||
| 391 | * |
||
| 392 | * @param string $standardizeType |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | 6 | private function getTypeMapping($standardizeType) |
|
| 396 | { |
||
| 397 | 6 | if(!isset($this->getTypeMappingList()[$standardizeType])){ |
|
| 398 | 1 | throw new InvalidArgumentException(sprintf( |
|
| 399 | 1 | '$standardizeType not valid. Should be on of the values: %s. "%s" given.', |
|
| 400 | 1 | json_encode($this->getStandardizedTypes()), |
|
| 401 | $standardizeType |
||
| 402 | 1 | )); |
|
| 403 | } |
||
| 404 | 5 | return $this->getTypeMappingList()[$standardizeType]; |
|
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Indicates whether self::$type is a possible type of $standardizedType. |
||
| 409 | * |
||
| 410 | * @param string $standardizedType |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | 6 | private function isInTypeMapping($standardizedType) |
|
| 414 | { |
||
| 415 | 6 | return in_array(strtolower($this->getType()), $this->getTypeMapping($standardizedType), true); |
|
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * get the possible types. |
||
| 420 | * |
||
| 421 | * @return array |
||
|
0 ignored issues
–
show
|
|||
| 422 | */ |
||
| 423 | 2 | private function getStandardizedTypes() |
|
| 424 | { |
||
| 425 | 2 | return array_keys($this->getTypeMappingList()); |
|
| 426 | } |
||
| 427 | } |
||
| 428 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.