Complex classes like ReflectionClassLikeTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReflectionClassLikeTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | trait ReflectionClassLikeTrait |
||
| 30 | { |
||
| 31 | use InitializationTrait; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ClassLike |
||
| 35 | */ |
||
| 36 | protected $classLikeNode; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Short name of the class, without namespace |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $className; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * List of all constants from the class |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $constants; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Interfaces, empty array or null if not initialized yet |
||
| 54 | * |
||
| 55 | * @var \ReflectionClass[]|array|null |
||
| 56 | */ |
||
| 57 | protected $interfaceClasses; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * List of traits, empty array or null if not initialized yet |
||
| 61 | * |
||
| 62 | * @var \ReflectionClass[]|array|null |
||
| 63 | */ |
||
| 64 | protected $traits; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Additional list of trait adaptations |
||
| 68 | * |
||
| 69 | * @var TraitUseAdaptation[]|array |
||
| 70 | */ |
||
| 71 | protected $traitAdaptations; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array|ReflectionMethod[] |
||
| 75 | */ |
||
| 76 | protected $methods; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Namespace name |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $namespaceName = ''; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Parent class, or false if not present, null if uninitialized yet |
||
| 87 | * |
||
| 88 | * @var \ReflectionClass|false|null |
||
| 89 | */ |
||
| 90 | protected $parentClass; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array|ReflectionProperty[] |
||
| 94 | */ |
||
| 95 | protected $properties; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the string representation of the ReflectionClass object. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function __toString() |
||
| 103 | { |
||
| 104 | $isObject = $this instanceof \ReflectionObject; |
||
| 105 | |||
| 106 | $staticProperties = $staticMethods = $defaultProperties = $dynamicProperties = $methods = []; |
||
| 107 | |||
| 108 | $format = "%s [ <user> %sclass %s%s%s ] {\n"; |
||
| 109 | $format .= " @@ %s %d-%d\n\n"; |
||
| 110 | $format .= " - Constants [%d] {%s\n }\n\n"; |
||
| 111 | $format .= " - Static properties [%d] {%s\n }\n\n"; |
||
| 112 | $format .= " - Static methods [%d] {%s\n }\n\n"; |
||
| 113 | $format .= " - Properties [%d] {%s\n }\n\n"; |
||
| 114 | $format .= ($isObject ? " - Dynamic properties [%d] {%s\n }\n\n" : '%s%s'); |
||
| 115 | $format .= " - Methods [%d] {%s\n }\n"; |
||
| 116 | $format .= "}\n"; |
||
| 117 | |||
| 118 | foreach ($this->getProperties() as $property) { |
||
| 119 | if ($property->isStatic()) { |
||
| 120 | $staticProperties[] = $property; |
||
| 121 | } elseif ($property->isDefault()) { |
||
| 122 | $defaultProperties[] = $property; |
||
| 123 | } else { |
||
| 124 | $dynamicProperties[] = $property; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | foreach ($this->getMethods() as $method) { |
||
| 129 | if ($method->isStatic()) { |
||
| 130 | $staticMethods[] = $method; |
||
| 131 | } else { |
||
| 132 | $methods[] = $method; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | $buildString = function (array $items, $indentLevel = 4) { |
||
| 137 | if (!count($items)) { |
||
| 138 | return ''; |
||
| 139 | } |
||
| 140 | $indent = "\n" . str_repeat(' ', $indentLevel); |
||
| 141 | return $indent . implode($indent, explode("\n", implode("\n", $items))); |
||
| 142 | }; |
||
| 143 | $buildConstants = function (array $items, $indentLevel = 4) { |
||
| 144 | $str = ''; |
||
| 145 | foreach ($items as $name => $value) { |
||
| 146 | $str .= "\n" . str_repeat(' ', $indentLevel); |
||
| 147 | $str .= sprintf( |
||
| 148 | 'Constant [ %s %s ] { %s }', |
||
| 149 | gettype($value), |
||
| 150 | $name, |
||
| 151 | $value |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | return $str; |
||
| 155 | }; |
||
| 156 | $interfaceNames = $this->getInterfaceNames(); |
||
| 157 | $parentClass = $this->getParentClass(); |
||
| 158 | $modifiers = ''; |
||
| 159 | if ($this->isAbstract()) { |
||
| 160 | $modifiers = 'abstract '; |
||
| 161 | } elseif ($this->isFinal()) { |
||
| 162 | $modifiers = 'final '; |
||
| 163 | }; |
||
| 164 | |||
| 165 | $string = sprintf( |
||
| 166 | $format, |
||
| 167 | ($isObject ? 'Object of class' : 'Class'), |
||
| 168 | $modifiers, |
||
| 169 | $this->getName(), |
||
| 170 | false !== $parentClass ? (' extends ' . $parentClass->getName()) : '', |
||
| 171 | $interfaceNames ? (' implements ' . implode(', ', $interfaceNames)) : '', |
||
| 172 | $this->getFileName(), |
||
| 173 | $this->getStartLine(), |
||
| 174 | $this->getEndLine(), |
||
| 175 | count($this->getConstants()), |
||
| 176 | $buildConstants($this->getConstants()), |
||
| 177 | count($staticProperties), |
||
| 178 | $buildString($staticProperties), |
||
| 179 | count($staticMethods), |
||
| 180 | $buildString($staticMethods), |
||
| 181 | count($defaultProperties), |
||
| 182 | $buildString($defaultProperties), |
||
| 183 | $isObject ? count($dynamicProperties) : '', |
||
| 184 | $isObject ? $buildString($dynamicProperties) : '', |
||
| 185 | count($methods), |
||
| 186 | $buildString($methods) |
||
| 187 | ); |
||
| 188 | |||
| 189 | return $string; |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritDoc} |
||
| 195 | */ |
||
| 196 | 9 | public function getConstant($name) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritDoc} |
||
| 207 | */ |
||
| 208 | 37 | public function getConstants() |
|
| 209 | { |
||
| 210 | 37 | if (!isset($this->constants)) { |
|
| 211 | $this->constants = $this->recursiveCollect(function (array &$result, \ReflectionClass $instance) { |
||
| 212 | 10 | $result += $instance->getConstants(); |
|
| 213 | 37 | }); |
|
| 214 | 37 | $this->collectSelfConstants(); |
|
| 215 | } |
||
| 216 | |||
| 217 | 37 | return $this->constants; |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritDoc} |
||
| 222 | */ |
||
| 223 | 19 | public function getConstructor() |
|
| 224 | { |
||
| 225 | 19 | $constructor = $this->getMethod('__construct'); |
|
| 226 | 19 | if (!$constructor) { |
|
| 227 | 17 | return null; |
|
| 228 | } |
||
| 229 | |||
| 230 | 2 | return $constructor; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Gets default properties from a class (including inherited properties). |
||
| 235 | * |
||
| 236 | * @link http://php.net/manual/en/reflectionclass.getdefaultproperties.php |
||
| 237 | * |
||
| 238 | * @return array An array of default properties, with the key being the name of the property and the value being |
||
| 239 | * the default value of the property or NULL if the property doesn't have a default value |
||
| 240 | */ |
||
| 241 | 29 | public function getDefaultProperties() |
|
| 242 | { |
||
| 243 | 29 | $defaultValues = []; |
|
| 244 | 29 | $properties = $this->getProperties(); |
|
| 245 | 29 | $staticOrder = [true, false]; |
|
| 246 | 29 | foreach ($staticOrder as $shouldBeStatic) { |
|
| 247 | 29 | foreach ($properties as $property) { |
|
| 248 | 7 | $isStaticProperty = $property->isStatic(); |
|
| 249 | 7 | if ($shouldBeStatic !== $isStaticProperty) { |
|
| 250 | 7 | continue; |
|
| 251 | } |
||
| 252 | 7 | $propertyName = $property->getName(); |
|
| 253 | 7 | $isInternalReflection = get_class($property) == \ReflectionProperty::class; |
|
| 254 | |||
| 255 | 7 | if (!$isInternalReflection || $isStaticProperty) { |
|
| 256 | 7 | $defaultValues[$propertyName] = $property->getValue(); |
|
| 257 | } elseif (!$isStaticProperty) { |
||
| 258 | // Internal reflection and dynamic property |
||
| 259 | $classProperties = $property->getDeclaringClass()->getDefaultProperties(); |
||
| 260 | 29 | $defaultValues[$propertyName] = $classProperties[$propertyName]; |
|
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | 29 | return $defaultValues; |
|
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritDoc} |
||
| 270 | */ |
||
| 271 | 29 | public function getDocComment() |
|
| 272 | { |
||
| 273 | 29 | $docComment = $this->classLikeNode->getDocComment(); |
|
| 274 | |||
| 275 | 29 | return $docComment ? $docComment->getText() : false; |
|
| 276 | } |
||
| 277 | |||
| 278 | 29 | public function getEndLine() |
|
| 279 | { |
||
| 280 | 29 | return $this->classLikeNode->getAttribute('endLine'); |
|
| 281 | } |
||
| 282 | |||
| 283 | 29 | public function getExtension() |
|
| 284 | { |
||
| 285 | 29 | return null; |
|
| 286 | } |
||
| 287 | |||
| 288 | 29 | public function getExtensionName() |
|
| 289 | { |
||
| 290 | 29 | return false; |
|
| 291 | } |
||
| 292 | |||
| 293 | 19 | public function getFileName() |
|
| 294 | { |
||
| 295 | 19 | return $this->classLikeNode->getAttribute('fileName'); |
|
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritDoc} |
||
| 300 | */ |
||
| 301 | 30 | public function getInterfaceNames() |
|
| 302 | { |
||
| 303 | 30 | return array_keys($this->getInterfaces()); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritDoc} |
||
| 308 | */ |
||
| 309 | 59 | public function getInterfaces() |
|
| 310 | { |
||
| 311 | 59 | if (!isset($this->interfaceClasses)) { |
|
| 312 | $this->interfaceClasses = $this->recursiveCollect(function (array &$result, \ReflectionClass $instance) { |
||
| 313 | 7 | if ($instance->isInterface()) { |
|
| 314 | 1 | $result[$instance->name] = $instance; |
|
| 315 | } |
||
| 316 | 7 | $result += $instance->getInterfaces(); |
|
| 317 | 30 | }); |
|
| 318 | } |
||
| 319 | |||
| 320 | 59 | return $this->interfaceClasses; |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | * @param string $name |
||
| 326 | */ |
||
| 327 | 2047 | public function getMethod($name) |
|
| 328 | { |
||
| 329 | 2047 | $methods = $this->getMethods(); |
|
| 330 | 2047 | foreach ($methods as $method) { |
|
| 331 | 2038 | if ($method->getName() == $name) { |
|
| 332 | 2038 | return $method; |
|
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | 17 | return false; |
|
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns list of reflection methods |
||
| 341 | * |
||
| 342 | * @param null|integer $filter Optional filter |
||
| 343 | * |
||
| 344 | * @return array|\ReflectionMethod[] |
||
| 345 | */ |
||
| 346 | 2077 | public function getMethods($filter = null) |
|
| 347 | { |
||
| 348 | 2077 | if (!isset($this->methods)) { |
|
| 349 | 60 | $directMethods = ReflectionMethod::collectFromClassNode($this->classLikeNode, $this); |
|
| 350 | $parentMethods = $this->recursiveCollect(function (array &$result, \ReflectionClass $instance, $isParent) { |
||
| 351 | 18 | $reflectionMethods = []; |
|
| 352 | 18 | foreach ($instance->getMethods() as $reflectionMethod) { |
|
| 353 | 12 | if (!$isParent || !$reflectionMethod->isPrivate()) { |
|
| 354 | 12 | $reflectionMethods[$reflectionMethod->name] = $reflectionMethod; |
|
| 355 | } |
||
| 356 | } |
||
| 357 | 18 | $result += $reflectionMethods; |
|
| 358 | 60 | }); |
|
| 359 | 60 | $methods = $directMethods + $parentMethods; |
|
| 360 | |||
| 361 | 60 | $this->methods = $methods; |
|
| 362 | } |
||
| 363 | 2077 | if (!isset($filter)) { |
|
| 364 | 2073 | return array_values($this->methods); |
|
| 365 | } |
||
| 366 | |||
| 367 | 5 | $methods = []; |
|
| 368 | 5 | foreach ($this->methods as $method) { |
|
| 369 | 4 | if (!($filter & $method->getModifiers())) { |
|
| 370 | 4 | continue; |
|
| 371 | } |
||
| 372 | 2 | $methods[] = $method; |
|
| 373 | } |
||
| 374 | |||
| 375 | 5 | return $methods; |
|
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns a bitfield of the access modifiers for this class. |
||
| 380 | * |
||
| 381 | * @link http://php.net/manual/en/reflectionclass.getmodifiers.php |
||
| 382 | * |
||
| 383 | * NB: this method is not fully compatible with original value because of hidden internal constants |
||
| 384 | * |
||
| 385 | * @return int |
||
| 386 | */ |
||
| 387 | 4 | public function getModifiers() |
|
| 388 | { |
||
| 389 | 4 | $modifiers = 0; |
|
| 390 | |||
| 391 | 4 | if ($this->isFinal()) { |
|
| 392 | 1 | $modifiers += \ReflectionClass::IS_FINAL; |
|
| 393 | } |
||
| 394 | |||
| 395 | 4 | if (PHP_VERSION_ID < 70000 && $this->isTrait()) { |
|
| 396 | $modifiers += \ReflectionClass::IS_EXPLICIT_ABSTRACT; |
||
| 397 | } |
||
| 398 | |||
| 399 | 4 | if ($this->classLikeNode instanceof Class_ && $this->classLikeNode->isAbstract()) { |
|
| 400 | 1 | $modifiers += \ReflectionClass::IS_EXPLICIT_ABSTRACT; |
|
| 401 | } |
||
| 402 | |||
| 403 | 4 | if ($this->isInterface()) { |
|
| 404 | 1 | $abstractMethods = $this->getMethods(); |
|
| 405 | } else { |
||
| 406 | 4 | $abstractMethods = $this->getMethods(\ReflectionMethod::IS_ABSTRACT); |
|
| 407 | } |
||
| 408 | 4 | if (!empty($abstractMethods)) { |
|
| 409 | 1 | $modifiers += \ReflectionClass::IS_IMPLICIT_ABSTRACT; |
|
| 410 | } |
||
| 411 | |||
| 412 | 4 | return $modifiers; |
|
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * {@inheritDoc} |
||
| 417 | */ |
||
| 418 | 2997 | public function getName() |
|
| 419 | { |
||
| 420 | 2997 | $namespaceName = $this->namespaceName ? $this->namespaceName . '\\' : ''; |
|
| 421 | |||
| 422 | 2997 | return $namespaceName . $this->getShortName(); |
|
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * {@inheritDoc} |
||
| 427 | */ |
||
| 428 | 48 | public function getNamespaceName() |
|
| 429 | { |
||
| 430 | 48 | return $this->namespaceName; |
|
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritDoc} |
||
| 435 | */ |
||
| 436 | 230 | public function getParentClass() |
|
| 437 | { |
||
| 438 | 230 | if (!isset($this->parentClass)) { |
|
| 439 | 89 | static $extendsField = 'extends'; |
|
| 440 | |||
| 441 | 89 | $parentClass = false; |
|
| 442 | 89 | $hasExtends = in_array($extendsField, $this->classLikeNode->getSubNodeNames()); |
|
| 443 | 89 | $extendsNode = $hasExtends ? $this->classLikeNode->$extendsField : null; |
|
| 444 | 89 | if ($extendsNode instanceof FullyQualified) { |
|
| 445 | 23 | $extendsName = $extendsNode->toString(); |
|
| 446 | 23 | $parentClass = $this->createReflectionForClass($extendsName); |
|
| 447 | } |
||
| 448 | 89 | $this->parentClass = $parentClass; |
|
| 449 | } |
||
| 450 | |||
| 451 | 230 | return $this->parentClass; |
|
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Retrieves reflected properties. |
||
| 456 | * |
||
| 457 | * @param int $filter The optional filter, for filtering desired property types. |
||
| 458 | * It's configured using the ReflectionProperty constants, and defaults to all property types. |
||
| 459 | * |
||
| 460 | * @return array|\Go\ParserReflection\ReflectionProperty[] |
||
| 461 | */ |
||
| 462 | 317 | public function getProperties($filter = null) |
|
| 463 | { |
||
| 464 | 317 | if (!isset($this->properties)) { |
|
| 465 | 43 | $directProperties = ReflectionProperty::collectFromClassNode($this->classLikeNode, $this->getName()); |
|
| 466 | 43 | $parentProperties = $this->recursiveCollect(function (array &$result, \ReflectionClass $instance, $isParent) { |
|
| 467 | 9 | $reflectionProperties = []; |
|
| 468 | 9 | foreach ($instance->getProperties() as $reflectionProperty) { |
|
| 469 | 3 | if (!$isParent || !$reflectionProperty->isPrivate()) { |
|
| 470 | 3 | $reflectionProperties[$reflectionProperty->name] = $reflectionProperty; |
|
| 471 | } |
||
| 472 | } |
||
| 473 | 9 | $result += $reflectionProperties; |
|
| 474 | 43 | }); |
|
| 475 | 43 | $properties = $directProperties + $parentProperties; |
|
| 476 | |||
| 477 | 43 | $this->properties = $properties; |
|
| 478 | } |
||
| 479 | |||
| 480 | // Without filter we can just return the full list |
||
| 481 | 317 | if (!isset($filter)) { |
|
| 482 | 288 | return array_values($this->properties); |
|
| 483 | } |
||
| 484 | |||
| 485 | 29 | $properties = []; |
|
| 486 | 29 | foreach ($this->properties as $property) { |
|
| 487 | 7 | if (!($filter & $property->getModifiers())) { |
|
| 488 | 5 | continue; |
|
| 489 | } |
||
| 490 | 5 | $properties[] = $property; |
|
| 491 | } |
||
| 492 | |||
| 493 | 29 | return $properties; |
|
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * {@inheritdoc} |
||
| 498 | */ |
||
| 499 | 255 | public function getProperty($name) |
|
| 500 | { |
||
| 501 | 255 | $properties = $this->getProperties(); |
|
| 502 | 255 | foreach ($properties as $property) { |
|
| 503 | 255 | if ($property->getName() == $name) { |
|
| 504 | 255 | return $property; |
|
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | return false; |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * {@inheritDoc} |
||
| 513 | */ |
||
| 514 | 2997 | public function getShortName() |
|
| 515 | { |
||
| 516 | 2997 | return $this->className; |
|
| 517 | } |
||
| 518 | |||
| 519 | 29 | public function getStartLine() |
|
| 520 | { |
||
| 521 | 29 | return $this->classLikeNode->getAttribute('startLine'); |
|
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Returns an array of trait aliases |
||
| 526 | * |
||
| 527 | * @link http://php.net/manual/en/reflectionclass.gettraitaliases.php |
||
| 528 | * |
||
| 529 | * @return array|null an array with new method names in keys and original names (in the format "TraitName::original") in |
||
| 530 | * values. |
||
| 531 | */ |
||
| 532 | 33 | public function getTraitAliases() |
|
| 533 | { |
||
| 534 | 33 | $aliases = []; |
|
| 535 | 33 | $traits = $this->getTraits(); |
|
| 536 | 33 | foreach ($this->traitAdaptations as $adaptation) { |
|
| 537 | if ($adaptation instanceof TraitUseAdaptation\Alias) { |
||
| 538 | $methodName = $adaptation->method; |
||
| 539 | $traitName = null; |
||
| 540 | foreach ($traits as $trait) { |
||
| 541 | if ($trait->hasMethod($methodName)) { |
||
| 542 | $traitName = $trait->getName(); |
||
| 543 | break; |
||
| 544 | } |
||
| 545 | } |
||
| 546 | $aliases[$adaptation->newName] = $traitName . '::'. $methodName; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | 33 | return $aliases; |
|
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Returns an array of names of traits used by this class |
||
| 555 | * |
||
| 556 | * @link http://php.net/manual/en/reflectionclass.gettraitnames.php |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | 29 | public function getTraitNames() |
|
| 561 | { |
||
| 562 | 29 | return array_keys($this->getTraits()); |
|
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Returns an array of traits used by this class |
||
| 567 | * |
||
| 568 | * @link http://php.net/manual/en/reflectionclass.gettraits.php |
||
| 569 | * |
||
| 570 | * @return array|\ReflectionClass[] |
||
| 571 | */ |
||
| 572 | 225 | public function getTraits() |
|
| 573 | { |
||
| 574 | 225 | if (!isset($this->traits)) { |
|
| 575 | 89 | $traitAdaptations = []; |
|
| 576 | 89 | $this->traits = ReflectionClass::collectTraitsFromClassNode($this->classLikeNode, $traitAdaptations); |
|
| 577 | 89 | $this->traitAdaptations = $traitAdaptations; |
|
| 578 | } |
||
| 579 | |||
| 580 | 225 | return $this->traits; |
|
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * {@inheritDoc} |
||
| 585 | */ |
||
| 586 | 10 | public function hasConstant($name) |
|
| 587 | { |
||
| 588 | 10 | $constants = $this->getConstants(); |
|
| 589 | 10 | $hasConstant = isset($constants[$name]) || array_key_exists($name, $constants); |
|
| 590 | |||
| 591 | 10 | return $hasConstant; |
|
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * {@inheritdoc} |
||
| 596 | * @param string $name |
||
| 597 | */ |
||
| 598 | 20 | public function hasMethod($name) |
|
| 599 | { |
||
| 600 | 20 | $methods = $this->getMethods(); |
|
| 601 | 20 | foreach ($methods as $method) { |
|
| 602 | 11 | if ($method->getName() == $name) { |
|
| 603 | 11 | return true; |
|
| 604 | } |
||
| 605 | } |
||
| 606 | |||
| 607 | 18 | return false; |
|
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * {@inheritdoc} |
||
| 612 | */ |
||
| 613 | public function hasProperty($name) |
||
| 614 | { |
||
| 615 | $properties = $this->getProperties(); |
||
| 616 | foreach ($properties as $property) { |
||
| 617 | if ($property->getName() == $name) { |
||
| 618 | return true; |
||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | return false; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * {@inheritDoc} |
||
| 627 | * @param string $interfaceName |
||
| 628 | */ |
||
| 629 | 29 | public function implementsInterface($interfaceName) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * {@inheritDoc} |
||
| 638 | */ |
||
| 639 | 29 | public function inNamespace() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * {@inheritDoc} |
||
| 646 | */ |
||
| 647 | 75 | public function isAbstract() |
|
| 648 | { |
||
| 649 | 75 | if ($this->classLikeNode instanceof Class_ && $this->classLikeNode->isAbstract()) { |
|
| 650 | 12 | return true; |
|
| 651 | 63 | } elseif ($this->isInterface() && !empty($this->getMethods())) { |
|
| 652 | 2 | return true; |
|
| 653 | 61 | } elseif ($this->isTrait()) { |
|
| 654 | 3 | return PHP_VERSION_ID < 70000 ? true : false; |
|
| 655 | } |
||
| 656 | |||
| 657 | 58 | return false; |
|
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Currently, anonymous classes aren't supported for parsed reflection |
||
| 662 | */ |
||
| 663 | public function isAnonymous() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * {@inheritDoc} |
||
| 670 | */ |
||
| 671 | 29 | public function isCloneable() |
|
| 683 | |||
| 684 | /** |
||
| 685 | * {@inheritDoc} |
||
| 686 | */ |
||
| 687 | 33 | public function isFinal() |
|
| 693 | |||
| 694 | /** |
||
| 695 | * {@inheritDoc} |
||
| 696 | */ |
||
| 697 | public function isInstance($object) |
||
| 698 | { |
||
| 699 | if (!is_object($object)) { |
||
| 700 | throw new \RuntimeException(sprintf('Parameter must be an object, "%s" provided.', gettype($object))); |
||
| 701 | } |
||
| 702 | |||
| 703 | $className = $this->getName(); |
||
| 704 | return $className === get_class($object) || is_subclass_of($object, $className); |
||
|
|
|||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * {@inheritDoc} |
||
| 709 | */ |
||
| 710 | 29 | public function isInstantiable() |
|
| 722 | |||
| 723 | /** |
||
| 724 | * {@inheritDoc} |
||
| 725 | */ |
||
| 726 | 246 | public function isInterface() |
|
| 730 | |||
| 731 | /** |
||
| 732 | * {@inheritDoc} |
||
| 733 | */ |
||
| 734 | 29 | public function isInternal() |
|
| 739 | |||
| 740 | /** |
||
| 741 | * {@inheritDoc} |
||
| 742 | */ |
||
| 743 | 29 | public function isIterateable() |
|
| 747 | |||
| 748 | /** |
||
| 749 | * {@inheritDoc} |
||
| 750 | */ |
||
| 751 | public function isSubclassOf($class) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * {@inheritDoc} |
||
| 777 | */ |
||
| 778 | 106 | public function isTrait() |
|
| 782 | |||
| 783 | /** |
||
| 784 | * {@inheritDoc} |
||
| 785 | */ |
||
| 786 | 29 | public function isUserDefined() |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Gets static properties |
||
| 794 | * |
||
| 795 | * @link http://php.net/manual/en/reflectionclass.getstaticproperties.php |
||
| 796 | * |
||
| 797 | * @return array |
||
| 798 | */ |
||
| 799 | 30 | public function getStaticProperties() |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Gets static property value |
||
| 823 | * |
||
| 824 | * @param string $name The name of the static property for which to return a value. |
||
| 825 | * @param mixed $default A default value to return in case the class does not declare |
||
| 826 | * a static property with the given name |
||
| 827 | * |
||
| 828 | * @return mixed |
||
| 829 | * @throws ReflectionException If there is no such property and no default value was given |
||
| 830 | */ |
||
| 831 | 1 | public function getStaticPropertyValue($name, $default = null) |
|
| 842 | |||
| 843 | |||
| 844 | /** |
||
| 845 | * Creates a new class instance from given arguments. |
||
| 846 | * |
||
| 847 | * @link http://php.net/manual/en/reflectionclass.newinstance.php |
||
| 848 | * |
||
| 849 | * Signature was hacked to support both 5.6, 7.1.x and 7.2.0 versions |
||
| 850 | * @see https://3v4l.org/hW9O9 |
||
| 851 | * @see https://3v4l.org/sWT3j |
||
| 852 | * @see https://3v4l.org/eeVf8 |
||
| 853 | * |
||
| 854 | * @param mixed $arg First argument |
||
| 855 | * @param mixed $args Accepts a variable number of arguments which are passed to the class constructor |
||
| 856 | * |
||
| 857 | * @return object |
||
| 858 | */ |
||
| 859 | 1 | public function newInstance($arg = null, ...$args) |
|
| 860 | { |
||
| 861 | 1 | $args = array_slice(array_merge([$arg], $args), 0, \func_num_args()); |
|
| 862 | 1 | $this->initializeInternalReflection(); |
|
| 863 | |||
| 864 | 1 | return parent::newInstance(...$args); |
|
| 865 | } |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Creates a new class instance from given arguments. |
||
| 869 | * |
||
| 870 | * @link http://php.net/manual/en/reflectionclass.newinstanceargs.php |
||
| 871 | * |
||
| 872 | * @param array $args The parameters to be passed to the class constructor as an array. |
||
| 873 | * |
||
| 874 | * @return object |
||
| 875 | */ |
||
| 876 | 1 | public function newInstanceArgs(array $args = []) |
|
| 883 | |||
| 884 | /** |
||
| 885 | * Creates a new class instance without invoking the constructor. |
||
| 886 | * |
||
| 887 | * @link http://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php |
||
| 888 | * |
||
| 889 | * @return object |
||
| 890 | */ |
||
| 891 | 1 | public function newInstanceWithoutConstructor($args = null) |
|
| 898 | |||
| 899 | /** |
||
| 900 | * Sets static property value |
||
| 901 | * |
||
| 902 | * @link http://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php |
||
| 903 | * |
||
| 904 | * @param string $name Property name |
||
| 905 | * @param mixed $value New property value |
||
| 906 | */ |
||
| 907 | 1 | public function setStaticPropertyValue($name, $value) |
|
| 913 | |||
| 914 | 167 | private function recursiveCollect(\Closure $collector) |
|
| 936 | |||
| 937 | /** |
||
| 938 | * Collects list of constants from the class itself |
||
| 939 | */ |
||
| 940 | 37 | private function collectSelfConstants() |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Create a ReflectionClass for a given class name. |
||
| 962 | * |
||
| 963 | * @param string $className |
||
| 964 | * The name of the class to create a reflection for. |
||
| 965 | * |
||
| 966 | * @return ReflectionClass |
||
| 967 | * The apropriate reflection object. |
||
| 968 | */ |
||
| 969 | abstract protected function createReflectionForClass($className); |
||
| 970 | } |
||
| 971 |