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 |
||
| 13 | class Hierarchy extends DataExtension { |
||
| 14 | |||
| 15 | protected $markedNodes; |
||
| 16 | |||
| 17 | protected $markingFilter; |
||
| 18 | |||
| 19 | /** @var int */ |
||
| 20 | protected $_cache_numChildren; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least |
||
| 24 | * this number, and then stops. Root nodes will always show regardless of this settting. Further nodes can be |
||
| 25 | * lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30 |
||
| 26 | * children, the actual node count will be 50 (all root nodes plus first expanded child). |
||
| 27 | * |
||
| 28 | * @config |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | private static $node_threshold_total = 50; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available |
||
| 35 | * server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding |
||
| 36 | * this value typically won't display any children, although this is configurable through the $nodeCountCallback |
||
| 37 | * parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting. |
||
| 38 | * |
||
| 39 | * @config |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private static $node_threshold_leaf = 250; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * A list of classnames to exclude from display in both the CMS and front end |
||
| 46 | * displays. ->Children() and ->AllChildren affected. |
||
| 47 | * Especially useful for big sets of pages like listings |
||
| 48 | * If you use this, and still need the classes to be editable |
||
| 49 | * then add a model admin for the class |
||
| 50 | * Note: Does not filter subclasses (non-inheriting) |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | * @config |
||
| 54 | */ |
||
| 55 | private static $hide_from_hierarchy = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * A list of classnames to exclude from display in the page tree views of the CMS, |
||
| 59 | * unlike $hide_from_hierarchy above which effects both CMS and front end. |
||
| 60 | * Especially useful for big sets of pages like listings |
||
| 61 | * If you use this, and still need the classes to be editable |
||
| 62 | * then add a model admin for the class |
||
| 63 | * Note: Does not filter subclasses (non-inheriting) |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | * @config |
||
| 67 | */ |
||
| 68 | private static $hide_from_cms_tree = array(); |
||
| 69 | |||
| 70 | public static function get_extra_config($class, $extension, $args) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Validate the owner object - check for existence of infinite loops. |
||
| 78 | * |
||
| 79 | * @param ValidationResult $validationResult |
||
| 80 | */ |
||
| 81 | public function validate(ValidationResult $validationResult) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the children of this DataObject as an XHTML UL. This will be called recursively on each child, so if they |
||
| 113 | * have children they will be displayed as a UL inside a LI. |
||
| 114 | * |
||
| 115 | * @param string $attributes Attributes to add to the UL |
||
| 116 | * @param string|callable $titleEval PHP code to evaluate to start each child - this should include '<li>' |
||
| 117 | * @param string $extraArg Extra arguments that will be passed on to children, for if they |
||
| 118 | * overload this function |
||
| 119 | * @param bool $limitToMarked Display only marked children |
||
| 120 | * @param string $childrenMethod The name of the method used to get children from each object |
||
| 121 | * @param bool $rootCall Set to true for this first call, and then to false for calls inside |
||
| 122 | * the recursion. You should not change this. |
||
| 123 | * @param int $nodeCountThreshold See {@link self::$node_threshold_total} |
||
| 124 | * @param callable $nodeCountCallback Called with the node count, which gives the callback an opportunity to |
||
| 125 | * intercept the query. Useful e.g. to avoid excessive children listings |
||
| 126 | * (Arguments: $parent, $numChildren) |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getChildrenAsUL($attributes = "", $titleEval = '"<li>" . $child->Title', $extraArg = null, |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Mark a segment of the tree, by calling mark(). |
||
| 211 | * |
||
| 212 | * The method performs a breadth-first traversal until the number of nodes is more than minCount. This is used to |
||
| 213 | * get a limited number of tree nodes to show in the CMS initially. |
||
| 214 | * |
||
| 215 | * This method returns the number of nodes marked. After this method is called other methods can check |
||
| 216 | * {@link isExpanded()} and {@link isMarked()} on individual nodes. |
||
| 217 | * |
||
| 218 | * @param int $nodeCountThreshold See {@link getChildrenAsUL()} |
||
| 219 | * @return int The actual number of nodes marked. |
||
| 220 | */ |
||
| 221 | public function markPartialTree($nodeCountThreshold = 30, $context = null, |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Filter the marking to only those object with $node->$parameterName == $parameterValue |
||
| 243 | * |
||
| 244 | * @param string $parameterName The parameter on each node to check when marking. |
||
| 245 | * @param mixed $parameterValue The value the parameter must be to be marked. |
||
| 246 | */ |
||
| 247 | public function setMarkingFilter($parameterName, $parameterValue) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Filter the marking to only those where the function returns true. The node in question will be passed to the |
||
| 256 | * function. |
||
| 257 | * |
||
| 258 | * @param string $funcName The name of the function to call |
||
| 259 | */ |
||
| 260 | public function setMarkingFilterFunction($funcName) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns true if the marking filter matches on the given node. |
||
| 268 | * |
||
| 269 | * @param DataObject $node Node to check |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | public function markingFilterMatches($node) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Mark all children of the given node that match the marking filter. |
||
| 297 | * |
||
| 298 | * @param DataObject $node Parent node |
||
| 299 | * @param mixed $context |
||
| 300 | * @param string $childrenMethod The name of the instance method to call to get the object's list of children |
||
| 301 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 302 | * @return DataList |
||
| 303 | */ |
||
| 304 | public function markChildren($node, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating |
||
| 333 | * over the tree. |
||
| 334 | * |
||
| 335 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 336 | */ |
||
| 337 | protected function markingFinished($numChildrenMethod = "numChildren") { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
| 350 | * marking of this DataObject. |
||
| 351 | * |
||
| 352 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function markingClasses($numChildrenMethod="numChildren") { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Mark the children of the DataObject with the given ID. |
||
| 374 | * |
||
| 375 | * @param int $id ID of parent node |
||
| 376 | * @param bool $open If this is true, mark the parent node as opened |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function markById($id, $open = false) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
| 393 | * |
||
| 394 | * @param DataObject $childObj |
||
| 395 | */ |
||
| 396 | public function markToExpose($childObj) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Return the IDs of all the marked nodes. |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function markedNodeIDs() { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Return an array of this page and its ancestors, ordered item -> root. |
||
| 416 | * |
||
| 417 | * @return SiteTree[] |
||
| 418 | */ |
||
| 419 | public function parentStack() { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Cache of DataObjects' marked statuses: [ClassName][ID] = bool |
||
| 432 | * @var array |
||
| 433 | */ |
||
| 434 | protected static $marked = array(); |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
| 438 | * @var array |
||
| 439 | */ |
||
| 440 | protected static $expanded = array(); |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Cache of DataObjects' opened statuses: [ClassName][ID] = bool |
||
| 444 | * @var array |
||
| 445 | */ |
||
| 446 | protected static $treeOpened = array(); |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Mark this DataObject as expanded. |
||
| 450 | */ |
||
| 451 | public function markExpanded() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Mark this DataObject as unexpanded. |
||
| 458 | */ |
||
| 459 | public function markUnexpanded() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Mark this DataObject's tree as opened. |
||
| 466 | */ |
||
| 467 | public function markOpened() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Mark this DataObject's tree as closed. |
||
| 474 | */ |
||
| 475 | public function markClosed() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Check if this DataObject is marked. |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public function isMarked() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Check if this DataObject is expanded. |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | public function isExpanded() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Check if this DataObject's tree is opened. |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | public function isTreeOpened() { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get a list of this DataObject's and all it's descendants IDs. |
||
| 516 | * |
||
| 517 | * @return int[] |
||
| 518 | */ |
||
| 519 | public function getDescendantIDList() { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
| 527 | * |
||
| 528 | * @param array $idList Array to put results in. |
||
| 529 | */ |
||
| 530 | public function loadDescendantIDListInto(&$idList) { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get the children for this DataObject. |
||
| 547 | * |
||
| 548 | * @return DataList |
||
| 549 | */ |
||
| 550 | public function Children() { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Return all children, including those 'not in menus'. |
||
| 566 | * |
||
| 567 | * @return DataList |
||
| 568 | */ |
||
| 569 | public function AllChildren() { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Return all children, including those that have been deleted but are still in live. |
||
| 575 | * - Deleted children will be marked as "DeletedFromStage" |
||
| 576 | * - Added children will be marked as "AddedToStage" |
||
| 577 | * - Modified children will be marked as "ModifiedOnStage" |
||
| 578 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
| 579 | * |
||
| 580 | * @param mixed $context |
||
| 581 | * @return ArrayList |
||
| 582 | */ |
||
| 583 | public function AllChildrenIncludingDeleted($context = null) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @see AllChildrenIncludingDeleted |
||
| 589 | * |
||
| 590 | * @param mixed $context |
||
| 591 | * @return ArrayList |
||
| 592 | */ |
||
| 593 | public function doAllChildrenIncludingDeleted($context = null) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
| 624 | * |
||
| 625 | * @return DataList |
||
| 626 | * @throws Exception |
||
| 627 | */ |
||
| 628 | public function AllHistoricalChildren() { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Return the number of children that this page ever had, including pages that were deleted. |
||
| 640 | * |
||
| 641 | * @return int |
||
| 642 | * @throws Exception |
||
| 643 | */ |
||
| 644 | public function numHistoricalChildren() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
| 655 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
| 656 | * |
||
| 657 | * @param bool $cache Whether to retrieve values from cache |
||
| 658 | * @return int |
||
| 659 | */ |
||
| 660 | public function numChildren($cache = true) { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
| 674 | * |
||
| 675 | * @return bool |
||
| 676 | */ |
||
| 677 | public function showingCMSTree() { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Return children in the stage site. |
||
| 686 | * |
||
| 687 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
| 688 | * extension is applied to {@link SiteTree}. |
||
| 689 | * @return DataList |
||
| 690 | */ |
||
| 691 | public function stageChildren($showAll = false) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Return children in the live site, if it exists. |
||
| 713 | * |
||
| 714 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
| 715 | * applicable when extension is applied to {@link SiteTree}. |
||
| 716 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
| 717 | * @return DataList |
||
| 718 | * @throws Exception |
||
| 719 | */ |
||
| 720 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) { |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
| 748 | * is returned. |
||
| 749 | * |
||
| 750 | * @param string $filter |
||
| 751 | * @return DataObject |
||
| 752 | */ |
||
| 753 | public function getParent($filter = null) { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Return all the parents of this class in a set ordered from the lowest to highest parent. |
||
| 766 | * |
||
| 767 | * @return ArrayList |
||
| 768 | */ |
||
| 769 | public function getAncestors() { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
| 782 | * |
||
| 783 | * @param string $separator |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | public function getBreadcrumbs($separator = ' » ') { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
| 796 | * then search the parents. |
||
| 797 | * |
||
| 798 | * @todo Write! |
||
| 799 | * |
||
| 800 | * @param string $className Class name of the node to find |
||
| 801 | * @param DataObject $afterNode Used for recursive calls to this function |
||
| 802 | * @return DataObject |
||
| 803 | */ |
||
| 804 | public function naturalPrev($className, $afterNode = null ) { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
| 810 | * then search the parents. |
||
| 811 | * @param string $className Class name of the node to find. |
||
| 812 | * @param string|int $root ID/ClassName of the node to limit the search to |
||
| 813 | * @param DataObject $afterNode Used for recursive calls to this function |
||
| 814 | * @return DataObject |
||
| 815 | */ |
||
| 816 | public function naturalNext($className = null, $root = 0, $afterNode = null ) { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Flush all Hierarchy caches: |
||
| 863 | * - Children (instance) |
||
| 864 | * - NumChildren (instance) |
||
| 865 | * - Marked (global) |
||
| 866 | * - Expanded (global) |
||
| 867 | * - TreeOpened (global) |
||
| 868 | */ |
||
| 869 | public function flushCache() { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Reset global Hierarchy caches: |
||
| 879 | * - Marked |
||
| 880 | * - Expanded |
||
| 881 | * - TreeOpened |
||
| 882 | */ |
||
| 883 | public static function reset() { |
||
| 888 | |||
| 889 | } |
||
| 890 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.