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 | * Returns the string representation of the ReflectionClass object. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function __toString() |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritDoc} |
||
| 187 | */ |
||
| 188 | 3 | public function getConstant($name) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritDoc} |
||
| 199 | */ |
||
| 200 | 3 | public function getConstants() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritDoc} |
||
| 217 | */ |
||
| 218 | 2 | public function getConstructor() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritDoc} |
||
| 230 | */ |
||
| 231 | 2 | public function getDocComment() |
|
| 235 | |||
| 236 | 2 | public function getEndLine() |
|
| 240 | |||
| 241 | 2 | public function getExtension() |
|
| 245 | |||
| 246 | 2 | public function getExtensionName() |
|
| 250 | |||
| 251 | 5 | public function getFileName() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritDoc} |
||
| 258 | */ |
||
| 259 | 2 | public function getInterfaceNames() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritDoc} |
||
| 266 | */ |
||
| 267 | 2 | public function getInterfaces() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | 5 | public function getMethod($name) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Returns list of reflection methods |
||
| 298 | * |
||
| 299 | * @return ReflectionMethod[]|array |
||
| 300 | */ |
||
| 301 | 6 | public function getMethods($filter = null) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritDoc} |
||
| 319 | */ |
||
| 320 | 13 | public function getName() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritDoc} |
||
| 329 | */ |
||
| 330 | 5 | public function getNamespaceName() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritDoc} |
||
| 337 | */ |
||
| 338 | 9 | public function getParentClass() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Retrieves reflected properties. |
||
| 358 | * |
||
| 359 | * @param int $filter The optional filter, for filtering desired property types. |
||
| 360 | * It's configured using the ReflectionProperty constants, and defaults to all property types. |
||
| 361 | * |
||
| 362 | * @return array|\Go\ParserReflection\ReflectionProperty[] |
||
| 363 | */ |
||
| 364 | 5 | public function getProperties($filter = null) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * {@inheritdoc} |
||
| 409 | */ |
||
| 410 | 3 | public function getProperty($name) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * {@inheritDoc} |
||
| 424 | */ |
||
| 425 | 13 | public function getShortName() |
|
| 429 | |||
| 430 | 2 | public function getStartLine() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Returns an array of names of traits used by this class |
||
| 437 | * |
||
| 438 | * @link http://php.net/manual/en/reflectionclass.gettraitnames.php |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | 2 | public function getTraitNames() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Returns an array of traits used by this class |
||
| 449 | * |
||
| 450 | * @link http://php.net/manual/en/reflectionclass.gettraits.php |
||
| 451 | * |
||
| 452 | * @return array|\ReflectionClass[] |
||
| 453 | */ |
||
| 454 | 2 | public function getTraits() |
|
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritDoc} |
||
| 465 | */ |
||
| 466 | 3 | public function hasConstant($name) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * {@inheritdoc} |
||
| 476 | */ |
||
| 477 | 4 | public function hasMethod($name) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc} |
||
| 491 | */ |
||
| 492 | public function hasProperty($name) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritDoc} |
||
| 506 | */ |
||
| 507 | 2 | public function implementsInterface($interfaceName) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * {@inheritDoc} |
||
| 516 | */ |
||
| 517 | 2 | public function inNamespace() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * {@inheritDoc} |
||
| 524 | */ |
||
| 525 | 2 | public function isAbstract() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Currently, anonymous classes aren't supported for parsed reflection |
||
| 538 | */ |
||
| 539 | public function isAnonymous() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * {@inheritDoc} |
||
| 546 | */ |
||
| 547 | 2 | public function isCloneable() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * {@inheritDoc} |
||
| 562 | */ |
||
| 563 | 2 | public function isFinal() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * {@inheritDoc} |
||
| 572 | */ |
||
| 573 | public function isInstance($object) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * {@inheritDoc} |
||
| 586 | */ |
||
| 587 | 2 | public function isInstantiable() |
|
| 599 | |||
| 600 | /** |
||
| 601 | * {@inheritDoc} |
||
| 602 | */ |
||
| 603 | 2 | public function isInterface() |
|
| 607 | |||
| 608 | /** |
||
| 609 | * {@inheritDoc} |
||
| 610 | */ |
||
| 611 | 2 | public function isInternal() |
|
| 616 | |||
| 617 | /** |
||
| 618 | * {@inheritDoc} |
||
| 619 | */ |
||
| 620 | 2 | public function isIterateable() |
|
| 624 | |||
| 625 | /** |
||
| 626 | * {@inheritDoc} |
||
| 627 | */ |
||
| 628 | public function isSubclassOf($class) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * {@inheritDoc} |
||
| 654 | */ |
||
| 655 | 2 | public function isTrait() |
|
| 659 | |||
| 660 | /** |
||
| 661 | * {@inheritDoc} |
||
| 662 | */ |
||
| 663 | 2 | public function isUserDefined() |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Gets static properties |
||
| 671 | * |
||
| 672 | * @link http://php.net/manual/en/reflectionclass.getstaticproperties.php |
||
| 673 | * |
||
| 674 | * @return array |
||
| 675 | */ |
||
| 676 | 3 | public function getStaticProperties() |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Gets static property value |
||
| 700 | * |
||
| 701 | * @param string $name The name of the static property for which to return a value. |
||
| 702 | * @param mixed $default A default value to return in case the class does not declare |
||
| 703 | * a static property with the given name |
||
| 704 | * |
||
| 705 | * @return mixed |
||
| 706 | * @throws ReflectionException If there is no such property and no default value was given |
||
| 707 | */ |
||
| 708 | 1 | public function getStaticPropertyValue($name, $default = null) |
|
| 719 | |||
| 720 | |||
| 721 | /** |
||
| 722 | * Creates a new class instance from given arguments. |
||
| 723 | * |
||
| 724 | * @link http://php.net/manual/en/reflectionclass.newinstance.php |
||
| 725 | * @param mixed $args Accepts a variable number of arguments which are passed to the class constructor |
||
| 726 | * |
||
| 727 | * @return object |
||
| 728 | */ |
||
| 729 | 1 | public function newInstance($args = null) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Creates a new class instance from given arguments. |
||
| 738 | * |
||
| 739 | * @link http://php.net/manual/en/reflectionclass.newinstanceargs.php |
||
| 740 | * |
||
| 741 | * @param array $args The parameters to be passed to the class constructor as an array. |
||
| 742 | * |
||
| 743 | * @return object |
||
| 744 | */ |
||
| 745 | 1 | public function newInstanceArgs(array $args = []) |
|
| 752 | |||
| 753 | /** |
||
| 754 | * Creates a new class instance without invoking the constructor. |
||
| 755 | * |
||
| 756 | * @link http://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php |
||
| 757 | * |
||
| 758 | * @return object |
||
| 759 | */ |
||
| 760 | 1 | public function newInstanceWithoutConstructor($args = null) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Sets static property value |
||
| 770 | * |
||
| 771 | * @link http://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php |
||
| 772 | * |
||
| 773 | * @param string $name Property name |
||
| 774 | * @param mixed $value New property value |
||
| 775 | */ |
||
| 776 | 1 | public function setStaticPropertyValue($name, $value) |
|
| 782 | |||
| 783 | 9 | private function recursiveCollect(\Closure $collector) |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Returns list of constants from the class |
||
| 802 | * |
||
| 803 | * @return array |
||
| 804 | */ |
||
| 805 | 3 | private function findConstants() |
|
| 825 | } |
||
| 826 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.