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 |
||
| 27 | class Hierarchy extends DataExtension { |
||
| 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) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Validate the owner object - check for existence of infinite loops. |
||
| 92 | * |
||
| 93 | * @param ValidationResult $validationResult |
||
| 94 | */ |
||
| 95 | public function validate(ValidationResult $validationResult) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the children of this DataObject as an XHTML UL. This will be called recursively on each child, so if they |
||
| 127 | * have children they will be displayed as a UL inside a LI. |
||
| 128 | * |
||
| 129 | * @param string $attributes Attributes to add to the UL |
||
| 130 | * @param string|callable $titleEval PHP code to evaluate to start each child - this should include '<li>' |
||
| 131 | * @param string $extraArg Extra arguments that will be passed on to children, for if they |
||
| 132 | * overload this function |
||
| 133 | * @param bool $limitToMarked Display only marked children |
||
| 134 | * @param string $childrenMethod The name of the method used to get children from each object |
||
| 135 | * @param bool $rootCall Set to true for this first call, and then to false for calls inside |
||
| 136 | * the recursion. You should not change this. |
||
| 137 | * @param int $nodeCountThreshold See {@link self::$node_threshold_total} |
||
| 138 | * @param callable $nodeCountCallback Called with the node count, which gives the callback an opportunity to |
||
| 139 | * intercept the query. Useful e.g. to avoid excessive children listings |
||
| 140 | * (Arguments: $parent, $numChildren) |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getChildrenAsUL($attributes = "", $titleEval = '"<li>" . $child->Title', $extraArg = null, |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Mark a segment of the tree, by calling mark(). |
||
| 225 | * |
||
| 226 | * The method performs a breadth-first traversal until the number of nodes is more than minCount. This is used to |
||
| 227 | * get a limited number of tree nodes to show in the CMS initially. |
||
| 228 | * |
||
| 229 | * This method returns the number of nodes marked. After this method is called other methods can check |
||
| 230 | * {@link isExpanded()} and {@link isMarked()} on individual nodes. |
||
| 231 | * |
||
| 232 | * @param int $nodeCountThreshold See {@link getChildrenAsUL()} |
||
| 233 | * @return int The actual number of nodes marked. |
||
| 234 | */ |
||
| 235 | public function markPartialTree($nodeCountThreshold = 30, $context = null, |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Filter the marking to only those object with $node->$parameterName == $parameterValue |
||
| 257 | * |
||
| 258 | * @param string $parameterName The parameter on each node to check when marking. |
||
| 259 | * @param mixed $parameterValue The value the parameter must be to be marked. |
||
| 260 | */ |
||
| 261 | public function setMarkingFilter($parameterName, $parameterValue) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Filter the marking to only those where the function returns true. The node in question will be passed to the |
||
| 270 | * function. |
||
| 271 | * |
||
| 272 | * @param string $funcName The name of the function to call |
||
| 273 | */ |
||
| 274 | public function setMarkingFilterFunction($funcName) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns true if the marking filter matches on the given node. |
||
| 282 | * |
||
| 283 | * @param DataObject $node Node to check |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function markingFilterMatches($node) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Mark all children of the given node that match the marking filter. |
||
| 311 | * |
||
| 312 | * @param DataObject $node Parent node |
||
| 313 | * @param mixed $context |
||
| 314 | * @param string $childrenMethod The name of the instance method to call to get the object's list of children |
||
| 315 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 316 | * @return DataList |
||
| 317 | */ |
||
| 318 | public function markChildren($node, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating |
||
| 347 | * over the tree. |
||
| 348 | * |
||
| 349 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 350 | */ |
||
| 351 | protected function markingFinished($numChildrenMethod = "numChildren") { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
| 364 | * marking of this DataObject. |
||
| 365 | * |
||
| 366 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function markingClasses($numChildrenMethod="numChildren") { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Mark the children of the DataObject with the given ID. |
||
| 388 | * |
||
| 389 | * @param int $id ID of parent node |
||
| 390 | * @param bool $open If this is true, mark the parent node as opened |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function markById($id, $open = false) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
| 407 | * |
||
| 408 | * @param DataObject $childObj |
||
| 409 | */ |
||
| 410 | public function markToExpose($childObj) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Return the IDs of all the marked nodes. |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function markedNodeIDs() { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return an array of this page and its ancestors, ordered item -> root. |
||
| 430 | * |
||
| 431 | * @return SiteTree[] |
||
| 432 | */ |
||
| 433 | public function parentStack() { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Cache of DataObjects' marked statuses: [ClassName][ID] = bool |
||
| 446 | * @var array |
||
| 447 | */ |
||
| 448 | protected static $marked = array(); |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
| 452 | * @var array |
||
| 453 | */ |
||
| 454 | protected static $expanded = array(); |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Cache of DataObjects' opened statuses: [ClassName][ID] = bool |
||
| 458 | * @var array |
||
| 459 | */ |
||
| 460 | protected static $treeOpened = array(); |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Mark this DataObject as expanded. |
||
| 464 | */ |
||
| 465 | public function markExpanded() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Mark this DataObject as unexpanded. |
||
| 472 | */ |
||
| 473 | public function markUnexpanded() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Mark this DataObject's tree as opened. |
||
| 480 | */ |
||
| 481 | public function markOpened() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Mark this DataObject's tree as closed. |
||
| 488 | */ |
||
| 489 | public function markClosed() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Check if this DataObject is marked. |
||
| 497 | * |
||
| 498 | * @return bool |
||
| 499 | */ |
||
| 500 | public function isMarked() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Check if this DataObject is expanded. |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | public function isExpanded() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Check if this DataObject's tree is opened. |
||
| 519 | * |
||
| 520 | * @return bool |
||
| 521 | */ |
||
| 522 | public function isTreeOpened() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get a list of this DataObject's and all it's descendants IDs. |
||
| 530 | * |
||
| 531 | * @return int[] |
||
| 532 | */ |
||
| 533 | public function getDescendantIDList() { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
| 541 | * |
||
| 542 | * @param array $idList Array to put results in. |
||
| 543 | */ |
||
| 544 | public function loadDescendantIDListInto(&$idList) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Get the children for this DataObject. |
||
| 561 | * |
||
| 562 | * @return DataList |
||
| 563 | */ |
||
| 564 | public function Children() { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Return all children, including those 'not in menus'. |
||
| 580 | * |
||
| 581 | * @return DataList |
||
| 582 | */ |
||
| 583 | public function AllChildren() { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Return all children, including those that have been deleted but are still in live. |
||
| 589 | * - Deleted children will be marked as "DeletedFromStage" |
||
| 590 | * - Added children will be marked as "AddedToStage" |
||
| 591 | * - Modified children will be marked as "ModifiedOnStage" |
||
| 592 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
| 593 | * |
||
| 594 | * @param mixed $context |
||
| 595 | * @return ArrayList |
||
| 596 | */ |
||
| 597 | public function AllChildrenIncludingDeleted($context = null) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @see AllChildrenIncludingDeleted |
||
| 603 | * |
||
| 604 | * @param mixed $context |
||
| 605 | * @return ArrayList |
||
| 606 | */ |
||
| 607 | public function doAllChildrenIncludingDeleted($context = null) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
| 638 | * |
||
| 639 | * @return DataList |
||
| 640 | * @throws Exception |
||
| 641 | */ |
||
| 642 | public function AllHistoricalChildren() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Return the number of children that this page ever had, including pages that were deleted. |
||
| 658 | * |
||
| 659 | * @return int |
||
| 660 | * @throws Exception |
||
| 661 | */ |
||
| 662 | public function numHistoricalChildren() { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
| 672 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
| 673 | * |
||
| 674 | * @param bool $cache Whether to retrieve values from cache |
||
| 675 | * @return int |
||
| 676 | */ |
||
| 677 | public function numChildren($cache = true) { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
| 691 | * |
||
| 692 | * @return bool |
||
| 693 | */ |
||
| 694 | public function showingCMSTree() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Return children in the stage site. |
||
| 703 | * |
||
| 704 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
| 705 | * extension is applied to {@link SiteTree}. |
||
| 706 | * @return DataList |
||
| 707 | */ |
||
| 708 | public function stageChildren($showAll = false) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Return children in the live site, if it exists. |
||
| 730 | * |
||
| 731 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
| 732 | * applicable when extension is applied to {@link SiteTree}. |
||
| 733 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
| 734 | * @return DataList |
||
| 735 | * @throws Exception |
||
| 736 | */ |
||
| 737 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) { |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
| 765 | * is returned. |
||
| 766 | * |
||
| 767 | * @param string $filter |
||
| 768 | * @return DataObject |
||
| 769 | */ |
||
| 770 | public function getParent($filter = null) { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Return all the parents of this class in a set ordered from the lowest to highest parent. |
||
| 784 | * |
||
| 785 | * @return ArrayList |
||
| 786 | */ |
||
| 787 | public function getAncestors() { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
| 800 | * |
||
| 801 | * @param string $separator |
||
| 802 | * @return string |
||
| 803 | */ |
||
| 804 | public function getBreadcrumbs($separator = ' » ') { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
| 814 | * then search the parents. |
||
| 815 | * |
||
| 816 | * @todo Write! |
||
| 817 | * |
||
| 818 | * @param string $className Class name of the node to find |
||
| 819 | * @param DataObject $afterNode Used for recursive calls to this function |
||
| 820 | * @return DataObject |
||
| 821 | */ |
||
| 822 | public function naturalPrev($className, $afterNode = null ) { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
| 828 | * then search the parents. |
||
| 829 | * @param string $className Class name of the node to find. |
||
| 830 | * @param string|int $root ID/ClassName of the node to limit the search to |
||
| 831 | * @param DataObject $afterNode Used for recursive calls to this function |
||
| 832 | * @return DataObject |
||
| 833 | */ |
||
| 834 | public function naturalNext($className = null, $root = 0, $afterNode = null ) { |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Flush all Hierarchy caches: |
||
| 881 | * - Children (instance) |
||
| 882 | * - NumChildren (instance) |
||
| 883 | * - Marked (global) |
||
| 884 | * - Expanded (global) |
||
| 885 | * - TreeOpened (global) |
||
| 886 | */ |
||
| 887 | public function flushCache() { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Reset global Hierarchy caches: |
||
| 897 | * - Marked |
||
| 898 | * - Expanded |
||
| 899 | * - TreeOpened |
||
| 900 | */ |
||
| 901 | public static function reset() { |
||
| 906 | |||
| 907 | } |
||
| 908 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.