Complex classes like Hierarchy 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 Hierarchy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Hierarchy extends DataExtension implements Resettable |
||
| 27 | { |
||
| 28 | |||
| 29 | protected $markedNodes; |
||
| 30 | |||
| 31 | protected $markingFilter; |
||
| 32 | |||
| 33 | /** @var int */ |
||
| 34 | protected $_cache_numChildren; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least |
||
| 38 | * this number, and then stops. Root nodes will always show regardless of this settting. Further nodes can be |
||
| 39 | * lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30 |
||
| 40 | * children, the actual node count will be 50 (all root nodes plus first expanded child). |
||
| 41 | * |
||
| 42 | * @config |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private static $node_threshold_total = 50; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available |
||
| 49 | * server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding |
||
| 50 | * this value typically won't display any children, although this is configurable through the $nodeCountCallback |
||
| 51 | * parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting. |
||
| 52 | * |
||
| 53 | * @config |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | private static $node_threshold_leaf = 250; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * A list of classnames to exclude from display in both the CMS and front end |
||
| 60 | * displays. ->Children() and ->AllChildren affected. |
||
| 61 | * Especially useful for big sets of pages like listings |
||
| 62 | * If you use this, and still need the classes to be editable |
||
| 63 | * then add a model admin for the class |
||
| 64 | * Note: Does not filter subclasses (non-inheriting) |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | * @config |
||
| 68 | */ |
||
| 69 | private static $hide_from_hierarchy = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * A list of classnames to exclude from display in the page tree views of the CMS, |
||
| 73 | * unlike $hide_from_hierarchy above which effects both CMS and front end. |
||
| 74 | * Especially useful for big sets of pages like listings |
||
| 75 | * If you use this, and still need the classes to be editable |
||
| 76 | * then add a model admin for the class |
||
| 77 | * Note: Does not filter subclasses (non-inheriting) |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | * @config |
||
| 81 | */ |
||
| 82 | private static $hide_from_cms_tree = array(); |
||
| 83 | |||
| 84 | public static function get_extra_config($class, $extension, $args) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Validate the owner object - check for existence of infinite loops. |
||
| 93 | * |
||
| 94 | * @param ValidationResult $validationResult |
||
| 95 | */ |
||
| 96 | public function validate(ValidationResult $validationResult) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the children of this DataObject as an XHTML UL. This will be called recursively on each child, so if they |
||
| 136 | * have children they will be displayed as a UL inside a LI. |
||
| 137 | * |
||
| 138 | * @param string $attributes Attributes to add to the UL |
||
| 139 | * @param string|callable $titleEval PHP code to evaluate to start each child - this should include '<li>' |
||
| 140 | * @param string $extraArg Extra arguments that will be passed on to children, for if they overload this function |
||
| 141 | * @param bool $limitToMarked Display only marked children |
||
| 142 | * @param string $childrenMethod The name of the method used to get children from each object |
||
| 143 | * @param string $numChildrenMethod |
||
| 144 | * @param bool $rootCall Set to true for this first call, and then to false for calls inside the recursion. |
||
| 145 | * You should not change this. |
||
| 146 | * @param int $nodeCountThreshold See {@link self::$node_threshold_total} |
||
| 147 | * @param callable $nodeCountCallback Called with the node count, which gives the callback an opportunity to |
||
| 148 | * intercept the query. Useful e.g. to avoid excessive children listings (Arguments: $parent, $numChildren) |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getChildrenAsUL( |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Mark a segment of the tree, by calling mark(). |
||
| 252 | * |
||
| 253 | * The method performs a breadth-first traversal until the number of nodes is more than minCount. This is used to |
||
| 254 | * get a limited number of tree nodes to show in the CMS initially. |
||
| 255 | * |
||
| 256 | * This method returns the number of nodes marked. After this method is called other methods can check |
||
| 257 | * {@link isExpanded()} and {@link isMarked()} on individual nodes. |
||
| 258 | * |
||
| 259 | * @param int $nodeCountThreshold See {@link getChildrenAsUL()} |
||
| 260 | * @param mixed $context |
||
| 261 | * @param string $childrenMethod |
||
| 262 | * @param string $numChildrenMethod |
||
| 263 | * @return int The actual number of nodes marked. |
||
| 264 | */ |
||
| 265 | public function markPartialTree( |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Filter the marking to only those object with $node->$parameterName == $parameterValue |
||
| 296 | * |
||
| 297 | * @param string $parameterName The parameter on each node to check when marking. |
||
| 298 | * @param mixed $parameterValue The value the parameter must be to be marked. |
||
| 299 | */ |
||
| 300 | public function setMarkingFilter($parameterName, $parameterValue) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Filter the marking to only those where the function returns true. The node in question will be passed to the |
||
| 310 | * function. |
||
| 311 | * |
||
| 312 | * @param string $funcName The name of the function to call |
||
| 313 | */ |
||
| 314 | public function setMarkingFilterFunction($funcName) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns true if the marking filter matches on the given node. |
||
| 323 | * |
||
| 324 | * @param DataObject $node Node to check |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function markingFilterMatches($node) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Mark all children of the given node that match the marking filter. |
||
| 353 | * |
||
| 354 | * @param DataObject $node Parent node |
||
| 355 | * @param mixed $context |
||
| 356 | * @param string $childrenMethod The name of the instance method to call to get the object's list of children |
||
| 357 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 358 | * @return DataList |
||
| 359 | */ |
||
| 360 | public function markChildren( |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating |
||
| 398 | * over the tree. |
||
| 399 | * |
||
| 400 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 401 | */ |
||
| 402 | protected function markingFinished($numChildrenMethod = "numChildren") |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
| 416 | * marking of this DataObject. |
||
| 417 | * |
||
| 418 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function markingClasses($numChildrenMethod = "numChildren") |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Mark the children of the DataObject with the given ID. |
||
| 441 | * |
||
| 442 | * @param int $id ID of parent node |
||
| 443 | * @param bool $open If this is true, mark the parent node as opened |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function markById($id, $open = false) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
| 461 | * |
||
| 462 | * @param DataObject $childObj |
||
| 463 | */ |
||
| 464 | public function markToExpose($childObj) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Return the IDs of all the marked nodes. |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function markedNodeIDs() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Return an array of this page and its ancestors, ordered item -> root. |
||
| 486 | * |
||
| 487 | * @return SiteTree[] |
||
| 488 | */ |
||
| 489 | public function parentStack() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Cache of DataObjects' marked statuses: [ClassName][ID] = bool |
||
| 503 | * @var array |
||
| 504 | */ |
||
| 505 | protected static $marked = array(); |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
| 509 | * @var array |
||
| 510 | */ |
||
| 511 | protected static $expanded = array(); |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Cache of DataObjects' opened statuses: [ClassName][ID] = bool |
||
| 515 | * @var array |
||
| 516 | */ |
||
| 517 | protected static $treeOpened = array(); |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Mark this DataObject as expanded. |
||
| 521 | */ |
||
| 522 | public function markExpanded() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Mark this DataObject as unexpanded. |
||
| 530 | */ |
||
| 531 | public function markUnexpanded() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Mark this DataObject's tree as opened. |
||
| 539 | */ |
||
| 540 | public function markOpened() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Mark this DataObject's tree as closed. |
||
| 548 | */ |
||
| 549 | public function markClosed() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Check if this DataObject is marked. |
||
| 558 | * |
||
| 559 | * @return bool |
||
| 560 | */ |
||
| 561 | public function isMarked() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Check if this DataObject is expanded. |
||
| 570 | * |
||
| 571 | * @return bool |
||
| 572 | */ |
||
| 573 | public function isExpanded() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Check if this DataObject's tree is opened. |
||
| 582 | * |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | public function isTreeOpened() |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get a list of this DataObject's and all it's descendants IDs. |
||
| 594 | * |
||
| 595 | * @return int[] |
||
| 596 | */ |
||
| 597 | public function getDescendantIDList() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
| 606 | * |
||
| 607 | * @param array $idList Array to put results in. |
||
| 608 | */ |
||
| 609 | public function loadDescendantIDListInto(&$idList) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get the children for this DataObject. |
||
| 628 | * |
||
| 629 | * @return DataList |
||
| 630 | */ |
||
| 631 | public function Children() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Return all children, including those 'not in menus'. |
||
| 648 | * |
||
| 649 | * @return DataList |
||
| 650 | */ |
||
| 651 | public function AllChildren() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Return all children, including those that have been deleted but are still in live. |
||
| 658 | * - Deleted children will be marked as "DeletedFromStage" |
||
| 659 | * - Added children will be marked as "AddedToStage" |
||
| 660 | * - Modified children will be marked as "ModifiedOnStage" |
||
| 661 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
| 662 | * |
||
| 663 | * @param mixed $context |
||
| 664 | * @return ArrayList |
||
| 665 | */ |
||
| 666 | public function AllChildrenIncludingDeleted($context = null) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @see AllChildrenIncludingDeleted |
||
| 673 | * |
||
| 674 | * @param mixed $context |
||
| 675 | * @return ArrayList |
||
| 676 | */ |
||
| 677 | public function doAllChildrenIncludingDeleted($context = null) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
| 712 | * |
||
| 713 | * @return DataList |
||
| 714 | * @throws Exception |
||
| 715 | */ |
||
| 716 | public function AllHistoricalChildren() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Return the number of children that this page ever had, including pages that were deleted. |
||
| 733 | * |
||
| 734 | * @return int |
||
| 735 | * @throws Exception |
||
| 736 | */ |
||
| 737 | public function numHistoricalChildren() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
| 748 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
| 749 | * |
||
| 750 | * @param bool $cache Whether to retrieve values from cache |
||
| 751 | * @return int |
||
| 752 | */ |
||
| 753 | public function numChildren($cache = true) |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
| 768 | * |
||
| 769 | * @return bool |
||
| 770 | */ |
||
| 771 | public function showingCMSTree() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Return children in the stage site. |
||
| 783 | * |
||
| 784 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
| 785 | * extension is applied to {@link SiteTree}. |
||
| 786 | * @return DataList |
||
| 787 | */ |
||
| 788 | public function stageChildren($showAll = false) |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Return children in the live site, if it exists. |
||
| 811 | * |
||
| 812 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
| 813 | * applicable when extension is applied to {@link SiteTree}. |
||
| 814 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
| 815 | * @return DataList |
||
| 816 | * @throws Exception |
||
| 817 | */ |
||
| 818 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
| 849 | * is returned. |
||
| 850 | * |
||
| 851 | * @param string $filter |
||
| 852 | * @return DataObject |
||
| 853 | */ |
||
| 854 | public function getParent($filter = null) |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Return all the parents of this class in a set ordered from the lowest to highest parent. |
||
| 869 | * |
||
| 870 | * @return ArrayList |
||
| 871 | */ |
||
| 872 | public function getAncestors() |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
| 886 | * |
||
| 887 | * @param string $separator |
||
| 888 | * @return string |
||
| 889 | */ |
||
| 890 | public function getBreadcrumbs($separator = ' » ') |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
| 903 | * then search the parents. |
||
| 904 | * |
||
| 905 | * @todo Write! |
||
| 906 | * |
||
| 907 | * @param string $className Class name of the node to find |
||
| 908 | * @param DataObject $afterNode Used for recursive calls to this function |
||
| 909 | * @return DataObject |
||
| 910 | */ |
||
| 911 | public function naturalPrev($className, $afterNode = null) |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
| 918 | * then search the parents. |
||
| 919 | * @param string $className Class name of the node to find. |
||
| 920 | * @param string|int $root ID/ClassName of the node to limit the search to |
||
| 921 | * @param DataObject $afterNode Used for recursive calls to this function |
||
| 922 | * @return DataObject |
||
| 923 | */ |
||
| 924 | public function naturalNext($className = null, $root = 0, $afterNode = null) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Flush all Hierarchy caches: |
||
| 971 | * - Children (instance) |
||
| 972 | * - NumChildren (instance) |
||
| 973 | * - Marked (global) |
||
| 974 | * - Expanded (global) |
||
| 975 | * - TreeOpened (global) |
||
| 976 | */ |
||
| 977 | public function flushCache() |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Reset global Hierarchy caches: |
||
| 988 | * - Marked |
||
| 989 | * - Expanded |
||
| 990 | * - TreeOpened |
||
| 991 | */ |
||
| 992 | public static function reset() |
||
| 998 | } |
||
| 999 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.