Complex classes like MarkedSet 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 MarkedSet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class MarkedSet |
||
| 21 | { |
||
| 22 | use Injectable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Marked nodes for a given subtree. The first item in this list |
||
| 26 | * is the root object of the subtree. |
||
| 27 | * |
||
| 28 | * A marked item is an item in a tree which will be included in |
||
| 29 | * a resulting tree. |
||
| 30 | * |
||
| 31 | * @var array Map of [itemID => itemInstance] |
||
| 32 | */ |
||
| 33 | protected $markedNodes; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Optional filter callback for filtering nodes to mark |
||
| 37 | * |
||
| 38 | * Array with keys: |
||
| 39 | * - parameter |
||
| 40 | * - value |
||
| 41 | * - func |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | * @temp made public |
||
| 45 | */ |
||
| 46 | public $markingFilter; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var DataObject |
||
| 50 | */ |
||
| 51 | protected $rootNode = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Method to use for getting children. Defaults to 'AllChildrenIncludingDeleted' |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $childrenMethod = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Method to use for counting children. Defaults to `numChildren` |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $numChildrenMethod = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Minimum number of nodes to iterate over before stopping recursion |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | protected $nodeCountThreshold = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Max number of nodes to return from a single children collection |
||
| 76 | * |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | protected $maxChildNodes; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Enable limiting |
||
| 83 | * |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $enableLimiting = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Create an empty set with the given class |
||
| 90 | * |
||
| 91 | * @param DataObject $rootNode Root node for this set. To collect the entire tree, |
||
| 92 | * pass in a singelton object. |
||
| 93 | * @param string $childrenMethod Override children method |
||
| 94 | * @param string $numChildrenMethod Override children counting method |
||
| 95 | * @param int $nodeCountThreshold Minimum threshold for number nodes to mark |
||
| 96 | * @param int $maxChildNodes Maximum threshold for number of child nodes to include |
||
| 97 | */ |
||
| 98 | public function __construct( |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get total number of nodes to get. This acts as a soft lower-bounds for |
||
| 127 | * number of nodes to search until found. |
||
| 128 | * Defaults to value of node_threshold_total of hierarchy class. |
||
| 129 | * |
||
| 130 | * @return int |
||
| 131 | */ |
||
| 132 | public function getNodeCountThreshold() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Max number of nodes that can be physically rendered at any level. |
||
| 140 | * Acts as a hard upper bound, after which nodes will be trimmed for |
||
| 141 | * performance reasons. |
||
| 142 | * |
||
| 143 | * @return int |
||
| 144 | */ |
||
| 145 | public function getMaxChildNodes() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set hard limit of number of nodes to get for this level |
||
| 153 | * |
||
| 154 | * @param int $count |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function setMaxChildNodes($count) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set max node count |
||
| 165 | * |
||
| 166 | * @param int $total |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setNodeCountThreshold($total) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get method to use for getting children |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getChildrenMethod() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get children from this node |
||
| 187 | * |
||
| 188 | * @param DataObject $node |
||
| 189 | * @return SS_List |
||
| 190 | */ |
||
| 191 | protected function getChildren(DataObject $node) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set method to use for getting children |
||
| 199 | * |
||
| 200 | * @param string $method |
||
| 201 | * @throws InvalidArgumentException |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | public function setChildrenMethod($method) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get method name for num children |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getNumChildrenMethod() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Count children |
||
| 230 | * |
||
| 231 | * @param DataObject $node |
||
| 232 | * @return int |
||
| 233 | */ |
||
| 234 | protected function getNumChildren(DataObject $node) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set method name to get num children |
||
| 242 | * |
||
| 243 | * @param string $method |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function setNumChildrenMethod($method) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the children of this DataObject as an XHTML UL. This will be called recursively on each child, so if they |
||
| 262 | * have children they will be displayed as a UL inside a LI. |
||
| 263 | * |
||
| 264 | * @param string $template Template for items in the list |
||
| 265 | * @param array|callable $context Additional arguments to add to template when rendering |
||
| 266 | * due to excessive line length. If callable, this will be executed with the current node dataobject |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function renderChildren( |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get child data formatted as JSON |
||
| 287 | * |
||
| 288 | * @param callable $serialiseEval A callback that takes a DataObject as a single parameter, |
||
| 289 | * and should return an array containing a simple array representation. This result will |
||
| 290 | * replace the 'node' property at each point in the tree. |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | public function getChildrenAsArray($serialiseEval = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Render a node in the tree with the given template |
||
| 313 | * |
||
| 314 | * @param array $data array data for current node |
||
| 315 | * @param string|array $template Template to use |
||
| 316 | * @param array|callable $context Additional arguments to add to template when rendering |
||
| 317 | * due to excessive line length. If callable, this will be executed with the current node dataobject |
||
| 318 | * @return ArrayData Viewable object representing the root node. use getField('SubTree') to get HTML |
||
| 319 | */ |
||
| 320 | protected function renderSubtree($data, $template, $context = []) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return sub-tree as json array |
||
| 352 | * |
||
| 353 | * @param array $data |
||
| 354 | * @param callable $serialiseEval A callback that takes a DataObject as a single parameter, |
||
| 355 | * and should return an array containing a simple array representation. This result will |
||
| 356 | * replace the 'node' property at each point in the tree. |
||
| 357 | * @return mixed|string |
||
| 358 | */ |
||
| 359 | protected function getSubtreeAsArray($data, $serialiseEval) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get tree data for node |
||
| 387 | * |
||
| 388 | * @param DataObject $node |
||
| 389 | * @param int $depth |
||
| 390 | * @return array|string |
||
| 391 | */ |
||
| 392 | protected function getSubtree($node, $depth = 0) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Mark a segment of the tree, by calling mark(). |
||
| 431 | * |
||
| 432 | * The method performs a breadth-first traversal until the number of nodes is more than minCount. This is used to |
||
| 433 | * get a limited number of tree nodes to show in the CMS initially. |
||
| 434 | * |
||
| 435 | * This method returns the number of nodes marked. After this method is called other methods can check |
||
| 436 | * {@link isExpanded()} and {@link isMarked()} on individual nodes. |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function markPartialTree() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Filter the marking to only those object with $node->$parameterName == $parameterValue |
||
| 468 | * |
||
| 469 | * @param string $parameterName The parameter on each node to check when marking. |
||
| 470 | * @param mixed $parameterValue The value the parameter must be to be marked. |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | public function setMarkingFilter($parameterName, $parameterValue) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Filter the marking to only those where the function returns true. The node in question will be passed to the |
||
| 484 | * function. |
||
| 485 | * |
||
| 486 | * @param callable $callback Callback to filter |
||
| 487 | * @return $this |
||
| 488 | */ |
||
| 489 | public function setMarkingFilterFunction($callback) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns true if the marking filter matches on the given node. |
||
| 499 | * |
||
| 500 | * @param DataObject $node Node to check |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | protected function markingFilterMatches(DataObject $node) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Mark all children of the given node that match the marking filter. |
||
| 532 | * |
||
| 533 | * @param DataObject $node Parent node |
||
| 534 | * @return array List of children marked by this operation |
||
| 535 | */ |
||
| 536 | protected function markChildren(DataObject $node) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
| 574 | * marking of this DataObject. |
||
| 575 | * |
||
| 576 | * @param DataObject $node |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | protected function markingClasses($node) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Mark the children of the DataObject with the given ID. |
||
| 602 | * |
||
| 603 | * @param int $id ID of parent node |
||
| 604 | * @param bool $open If this is true, mark the parent node as opened |
||
| 605 | * @return bool |
||
| 606 | */ |
||
| 607 | public function markById($id, $open = false) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
| 622 | * |
||
| 623 | * @param DataObject|Hierarchy $childObj |
||
| 624 | * @return $this |
||
| 625 | */ |
||
| 626 | public function markToExpose(DataObject $childObj) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Return the IDs of all the marked nodes. |
||
| 640 | * |
||
| 641 | * @refactor called from CMSMain |
||
| 642 | * @return array |
||
| 643 | */ |
||
| 644 | public function markedNodeIDs() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
| 651 | * @var array |
||
| 652 | */ |
||
| 653 | protected $expanded = []; |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Cache of DataObjects' opened statuses: [ID] = bool |
||
| 657 | * @var array |
||
| 658 | */ |
||
| 659 | protected $treeOpened = []; |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Reset marked nodes |
||
| 663 | */ |
||
| 664 | public function clearMarks() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Mark this DataObject as expanded. |
||
| 673 | * |
||
| 674 | * @param DataObject $node |
||
| 675 | * @return $this |
||
| 676 | */ |
||
| 677 | public function markExpanded(DataObject $node) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Mark this DataObject as unexpanded. |
||
| 687 | * |
||
| 688 | * @param DataObject $node |
||
| 689 | * @return $this |
||
| 690 | */ |
||
| 691 | public function markUnexpanded(DataObject $node) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Mark this DataObject's tree as opened. |
||
| 701 | * |
||
| 702 | * @param DataObject $node |
||
| 703 | * @return $this |
||
| 704 | */ |
||
| 705 | public function markOpened(DataObject $node) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Mark this DataObject's tree as closed. |
||
| 715 | * |
||
| 716 | * @param DataObject $node |
||
| 717 | * @return $this |
||
| 718 | */ |
||
| 719 | public function markClosed(DataObject $node) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Check if this DataObject is marked. |
||
| 729 | * |
||
| 730 | * @param DataObject $node |
||
| 731 | * @return bool |
||
| 732 | */ |
||
| 733 | public function isMarked(DataObject $node) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Check if this DataObject is expanded. |
||
| 741 | * An expanded object has had it's children iterated through. |
||
| 742 | * |
||
| 743 | * @param DataObject $node |
||
| 744 | * @return bool |
||
| 745 | */ |
||
| 746 | public function isExpanded(DataObject $node) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Check if this DataObject's tree is opened. |
||
| 754 | * This is an expanded node which also should have children visually shown. |
||
| 755 | * |
||
| 756 | * @param DataObject $node |
||
| 757 | * @return bool |
||
| 758 | */ |
||
| 759 | public function isTreeOpened(DataObject $node) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Check if this node has too many children |
||
| 767 | * |
||
| 768 | * @param DataObject|Hierarchy $node |
||
| 769 | * @param int $count Children count (if already calculated) |
||
| 770 | * @return bool |
||
| 771 | */ |
||
| 772 | protected function isNodeLimited(DataObject $node, $count = null) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Toggle limiting on or off |
||
| 793 | * |
||
| 794 | * @param bool $enabled |
||
| 795 | * @return $this |
||
| 796 | */ |
||
| 797 | public function setLimitingEnabled($enabled) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Check if limiting is enabled |
||
| 805 | * |
||
| 806 | * @return bool |
||
| 807 | */ |
||
| 808 | public function getLimitingEnabled() |
||
| 812 | } |
||
| 813 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: