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 |
||
| 28 | trait ReflectionClassLikeTrait |
||
| 29 | { |
||
| 30 | use InitializationTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var ClassLike |
||
| 34 | */ |
||
| 35 | protected $classLikeNode; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Short name of the class, without namespace |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $className; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * List of all constants from the class |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $constants; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Interfaces, empty array or null if not initialized yet |
||
| 53 | * |
||
| 54 | * @var \ReflectionClass[]|array|null |
||
| 55 | */ |
||
| 56 | protected $interfaceClasses; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * List of traits, empty array or null if not initialized yet |
||
| 60 | * |
||
| 61 | * @var \ReflectionClass[]|array|null |
||
| 62 | */ |
||
| 63 | protected $traits; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array|ReflectionMethod[] |
||
| 67 | */ |
||
| 68 | protected $methods; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Namespace name |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $namespaceName = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Parent class, or false if not present, null if uninitialized yet |
||
| 79 | * |
||
| 80 | * @var \ReflectionClass|false|null |
||
| 81 | */ |
||
| 82 | protected $parentClass; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array|ReflectionProperty[] |
||
| 86 | */ |
||
| 87 | protected $properties; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Emulating original behaviour of reflection |
||
| 91 | */ |
||
| 92 | public function __debugInfo() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the string representation of the ReflectionClass object. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function __toString() |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritDoc} |
||
| 197 | */ |
||
| 198 | 3 | public function getConstant($name) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritDoc} |
||
| 209 | */ |
||
| 210 | 3 | public function getConstants() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritDoc} |
||
| 227 | */ |
||
| 228 | 2 | public function getConstructor() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritDoc} |
||
| 240 | */ |
||
| 241 | 2 | public function getDocComment() |
|
| 245 | |||
| 246 | 2 | public function getEndLine() |
|
| 250 | |||
| 251 | 2 | public function getExtension() |
|
| 255 | |||
| 256 | 2 | public function getExtensionName() |
|
| 260 | |||
| 261 | 5 | public function getFileName() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritDoc} |
||
| 268 | */ |
||
| 269 | 2 | public function getInterfaceNames() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritDoc} |
||
| 276 | */ |
||
| 277 | 2 | public function getInterfaces() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | 5 | public function getMethod($name) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Returns list of reflection methods |
||
| 308 | * |
||
| 309 | * @return ReflectionMethod[]|array |
||
| 310 | */ |
||
| 311 | 6 | public function getMethods($filter = null) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritDoc} |
||
| 329 | */ |
||
| 330 | 13 | public function getName() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritDoc} |
||
| 339 | */ |
||
| 340 | 5 | public function getNamespaceName() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritDoc} |
||
| 347 | */ |
||
| 348 | 9 | public function getParentClass() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Retrieves reflected properties. |
||
| 368 | * |
||
| 369 | * @param int $filter The optional filter, for filtering desired property types. |
||
| 370 | * It's configured using the ReflectionProperty constants, and defaults to all property types. |
||
| 371 | * |
||
| 372 | * @return array|\Go\ParserReflection\ReflectionProperty[] |
||
| 373 | */ |
||
| 374 | 5 | public function getProperties($filter = null) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * {@inheritdoc} |
||
| 419 | */ |
||
| 420 | 3 | public function getProperty($name) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritDoc} |
||
| 434 | */ |
||
| 435 | 13 | public function getShortName() |
|
| 439 | |||
| 440 | 2 | public function getStartLine() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Returns an array of names of traits used by this class |
||
| 447 | * |
||
| 448 | * @link http://php.net/manual/en/reflectionclass.gettraitnames.php |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 2 | public function getTraitNames() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Returns an array of traits used by this class |
||
| 459 | * |
||
| 460 | * @link http://php.net/manual/en/reflectionclass.gettraits.php |
||
| 461 | * |
||
| 462 | * @return array|\ReflectionClass[] |
||
| 463 | */ |
||
| 464 | 2 | public function getTraits() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * {@inheritDoc} |
||
| 475 | */ |
||
| 476 | 3 | public function hasConstant($name) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | 4 | public function hasMethod($name) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | */ |
||
| 502 | public function hasProperty($name) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * {@inheritDoc} |
||
| 516 | */ |
||
| 517 | 2 | public function implementsInterface($interfaceName) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * {@inheritDoc} |
||
| 526 | */ |
||
| 527 | 2 | public function inNamespace() |
|
| 531 | |||
| 532 | /** |
||
| 533 | * {@inheritDoc} |
||
| 534 | */ |
||
| 535 | 2 | public function isAbstract() |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Currently, anonymous classes aren't supported for parsed reflection |
||
| 548 | */ |
||
| 549 | public function isAnonymous() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * {@inheritDoc} |
||
| 556 | */ |
||
| 557 | 2 | public function isCloneable() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * {@inheritDoc} |
||
| 572 | */ |
||
| 573 | 2 | public function isFinal() |
|
| 579 | |||
| 580 | /** |
||
| 581 | * {@inheritDoc} |
||
| 582 | */ |
||
| 583 | public function isInstance($object) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * {@inheritDoc} |
||
| 596 | */ |
||
| 597 | 2 | public function isInstantiable() |
|
| 609 | |||
| 610 | /** |
||
| 611 | * {@inheritDoc} |
||
| 612 | */ |
||
| 613 | 2 | public function isInterface() |
|
| 617 | |||
| 618 | /** |
||
| 619 | * {@inheritDoc} |
||
| 620 | */ |
||
| 621 | 2 | public function isInternal() |
|
| 626 | |||
| 627 | /** |
||
| 628 | * {@inheritDoc} |
||
| 629 | */ |
||
| 630 | 2 | public function isIterateable() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * {@inheritDoc} |
||
| 637 | */ |
||
| 638 | public function isSubclassOf($class) |
||
| 639 | { |
||
| 640 | if (is_object($class)) { |
||
| 641 | if ($class instanceof ReflectionClass) { |
||
| 642 | $class = $class->name; |
||
| 643 | } else { |
||
| 644 | $class = get_class($class); |
||
| 645 | } |
||
| 646 | } |
||
| 647 | |||
| 648 | if (!$this->classLikeNode instanceof Class_) { |
||
| 649 | return false; |
||
| 650 | } else { |
||
| 651 | $extends = $this->classLikeNode->extends; |
||
| 652 | if ($extends && $extends->toString() == $class) { |
||
| 653 | return true; |
||
| 654 | } |
||
| 655 | } |
||
| 656 | |||
| 657 | $parent = $this->getParentClass(); |
||
| 658 | |||
| 659 | return false === $parent ? false : $parent->isSubclassOf($class); |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * {@inheritDoc} |
||
| 664 | */ |
||
| 665 | 2 | public function isTrait() |
|
| 669 | |||
| 670 | /** |
||
| 671 | * {@inheritDoc} |
||
| 672 | */ |
||
| 673 | 2 | public function isUserDefined() |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Gets static properties |
||
| 681 | * |
||
| 682 | * @link http://php.net/manual/en/reflectionclass.getstaticproperties.php |
||
| 683 | * |
||
| 684 | * @return array |
||
| 685 | */ |
||
| 686 | 3 | public function getStaticProperties() |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Gets static property value |
||
| 710 | * |
||
| 711 | * @param string $name The name of the static property for which to return a value. |
||
| 712 | * @param mixed $default A default value to return in case the class does not declare |
||
| 713 | * a static property with the given name |
||
| 714 | * |
||
| 715 | * @return mixed |
||
| 716 | * @throws ReflectionException If there is no such property and no default value was given |
||
| 717 | */ |
||
| 718 | 1 | public function getStaticPropertyValue($name, $default = null) |
|
| 729 | |||
| 730 | |||
| 731 | /** |
||
| 732 | * Creates a new class instance from given arguments. |
||
| 733 | * |
||
| 734 | * @link http://php.net/manual/en/reflectionclass.newinstance.php |
||
| 735 | * @param mixed $args Accepts a variable number of arguments which are passed to the class constructor |
||
| 736 | * |
||
| 737 | * @return object |
||
| 738 | */ |
||
| 739 | 1 | public function newInstance($args = null) |
|
| 745 | |||
| 746 | /** |
||
| 747 | * Creates a new class instance from given arguments. |
||
| 748 | * |
||
| 749 | * @link http://php.net/manual/en/reflectionclass.newinstanceargs.php |
||
| 750 | * |
||
| 751 | * @param array $args The parameters to be passed to the class constructor as an array. |
||
| 752 | * |
||
| 753 | * @return object |
||
| 754 | */ |
||
| 755 | 1 | public function newInstanceArgs(array $args = []) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Creates a new class instance without invoking the constructor. |
||
| 765 | * |
||
| 766 | * @link http://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php |
||
| 767 | * |
||
| 768 | * @return object |
||
| 769 | */ |
||
| 770 | 1 | public function newInstanceWithoutConstructor($args = null) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Sets static property value |
||
| 780 | * |
||
| 781 | * @link http://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php |
||
| 782 | * |
||
| 783 | * @param string $name Property name |
||
| 784 | * @param mixed $value New property value |
||
| 785 | */ |
||
| 786 | 1 | public function setStaticPropertyValue($name, $value) |
|
| 792 | |||
| 793 | 9 | private function recursiveCollect(\Closure $collector) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Returns list of constants from the class |
||
| 812 | * |
||
| 813 | * @return array |
||
| 814 | */ |
||
| 815 | 3 | private function findConstants() |
|
| 835 | } |
||
| 836 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.