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 | public static function get_extra_config($class, $extension, $args) { |
||
49 | |||
50 | /** |
||
51 | * Validate the owner object - check for existence of infinite loops. |
||
52 | * |
||
53 | * @param ValidationResult $validationResult |
||
54 | */ |
||
55 | public function validate(ValidationResult $validationResult) { |
||
84 | |||
85 | /** |
||
86 | * Returns the children of this DataObject as an XHTML UL. This will be called recursively on each child, so if they |
||
87 | * have children they will be displayed as a UL inside a LI. |
||
88 | * |
||
89 | * @param string $attributes Attributes to add to the UL |
||
90 | * @param string|callable $titleEval PHP code to evaluate to start each child - this should include '<li>' |
||
91 | * @param string $extraArg Extra arguments that will be passed on to children, for if they |
||
92 | * overload this function |
||
93 | * @param bool $limitToMarked Display only marked children |
||
94 | * @param string $childrenMethod The name of the method used to get children from each object |
||
95 | * @param bool $rootCall Set to true for this first call, and then to false for calls inside |
||
96 | * the recursion. You should not change this. |
||
97 | * @param int $nodeCountThreshold See {@link self::$node_threshold_total} |
||
98 | * @param callable $nodeCountCallback Called with the node count, which gives the callback an opportunity to |
||
99 | * intercept the query. Useful e.g. to avoid excessive children listings |
||
100 | * (Arguments: $parent, $numChildren) |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getChildrenAsUL($attributes = "", $titleEval = '"<li>" . $child->Title', $extraArg = null, |
||
182 | |||
183 | /** |
||
184 | * Mark a segment of the tree, by calling mark(). |
||
185 | * |
||
186 | * The method performs a breadth-first traversal until the number of nodes is more than minCount. This is used to |
||
187 | * get a limited number of tree nodes to show in the CMS initially. |
||
188 | * |
||
189 | * This method returns the number of nodes marked. After this method is called other methods can check |
||
190 | * {@link isExpanded()} and {@link isMarked()} on individual nodes. |
||
191 | * |
||
192 | * @param int $nodeCountThreshold See {@link getChildrenAsUL()} |
||
193 | * @return int The actual number of nodes marked. |
||
194 | */ |
||
195 | public function markPartialTree($nodeCountThreshold = 30, $context = null, |
||
214 | |||
215 | /** |
||
216 | * Filter the marking to only those object with $node->$parameterName == $parameterValue |
||
217 | * |
||
218 | * @param string $parameterName The parameter on each node to check when marking. |
||
219 | * @param mixed $parameterValue The value the parameter must be to be marked. |
||
220 | */ |
||
221 | public function setMarkingFilter($parameterName, $parameterValue) { |
||
227 | |||
228 | /** |
||
229 | * Filter the marking to only those where the function returns true. The node in question will be passed to the |
||
230 | * function. |
||
231 | * |
||
232 | * @param string $funcName The name of the function to call |
||
233 | */ |
||
234 | public function setMarkingFilterFunction($funcName) { |
||
239 | |||
240 | /** |
||
241 | * Returns true if the marking filter matches on the given node. |
||
242 | * |
||
243 | * @param DataObject $node Node to check |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function markingFilterMatches($node) { |
||
268 | |||
269 | /** |
||
270 | * Mark all children of the given node that match the marking filter. |
||
271 | * |
||
272 | * @param DataObject $node Parent node |
||
273 | * @param mixed $context |
||
274 | * @param string $childrenMethod The name of the instance method to call to get the object's list of children |
||
275 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
276 | * @return DataList |
||
277 | */ |
||
278 | public function markChildren($node, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", |
||
304 | |||
305 | /** |
||
306 | * Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating |
||
307 | * over the tree. |
||
308 | * |
||
309 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
310 | */ |
||
311 | protected function markingFinished($numChildrenMethod = "numChildren") { |
||
321 | |||
322 | /** |
||
323 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
324 | * marking of this DataObject. |
||
325 | * |
||
326 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
327 | * @return string |
||
328 | */ |
||
329 | public function markingClasses($numChildrenMethod="numChildren") { |
||
345 | |||
346 | /** |
||
347 | * Mark the children of the DataObject with the given ID. |
||
348 | * |
||
349 | * @param int $id ID of parent node |
||
350 | * @param bool $open If this is true, mark the parent node as opened |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function markById($id, $open = false) { |
||
364 | |||
365 | /** |
||
366 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
367 | * |
||
368 | * @param DataObject $childObj |
||
369 | */ |
||
370 | public function markToExpose($childObj) { |
||
378 | |||
379 | /** |
||
380 | * Return the IDs of all the marked nodes. |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | public function markedNodeIDs() { |
||
387 | |||
388 | /** |
||
389 | * Return an array of this page and its ancestors, ordered item -> root. |
||
390 | * |
||
391 | * @return SiteTree[] |
||
392 | */ |
||
393 | public function parentStack() { |
||
403 | |||
404 | /** |
||
405 | * Cache of DataObjects' marked statuses: [ClassName][ID] = bool |
||
406 | * @var array |
||
407 | */ |
||
408 | protected static $marked = array(); |
||
409 | |||
410 | /** |
||
411 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
412 | * @var array |
||
413 | */ |
||
414 | protected static $expanded = array(); |
||
415 | |||
416 | /** |
||
417 | * Cache of DataObjects' opened statuses: [ClassName][ID] = bool |
||
418 | * @var array |
||
419 | */ |
||
420 | protected static $treeOpened = array(); |
||
421 | |||
422 | /** |
||
423 | * Mark this DataObject as expanded. |
||
424 | */ |
||
425 | public function markExpanded() { |
||
429 | |||
430 | /** |
||
431 | * Mark this DataObject as unexpanded. |
||
432 | */ |
||
433 | public function markUnexpanded() { |
||
437 | |||
438 | /** |
||
439 | * Mark this DataObject's tree as opened. |
||
440 | */ |
||
441 | public function markOpened() { |
||
445 | |||
446 | /** |
||
447 | * Mark this DataObject's tree as closed. |
||
448 | */ |
||
449 | public function markClosed() { |
||
454 | |||
455 | /** |
||
456 | * Check if this DataObject is marked. |
||
457 | * |
||
458 | * @return bool |
||
459 | */ |
||
460 | public function isMarked() { |
||
465 | |||
466 | /** |
||
467 | * Check if this DataObject is expanded. |
||
468 | * |
||
469 | * @return bool |
||
470 | */ |
||
471 | public function isExpanded() { |
||
476 | |||
477 | /** |
||
478 | * Check if this DataObject's tree is opened. |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | public function isTreeOpened() { |
||
487 | |||
488 | /** |
||
489 | * Get a list of this DataObject's and all it's descendants IDs. |
||
490 | * |
||
491 | * @return int[] |
||
492 | */ |
||
493 | public function getDescendantIDList() { |
||
498 | |||
499 | /** |
||
500 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
501 | * |
||
502 | * @param array $idList Array to put results in. |
||
503 | */ |
||
504 | public function loadDescendantIDListInto(&$idList) { |
||
518 | |||
519 | /** |
||
520 | * Get the children for this DataObject. |
||
521 | * |
||
522 | * @return DataList |
||
523 | */ |
||
524 | public function Children() { |
||
537 | |||
538 | /** |
||
539 | * Return all children, including those 'not in menus'. |
||
540 | * |
||
541 | * @return DataList |
||
542 | */ |
||
543 | public function AllChildren() { |
||
546 | |||
547 | /** |
||
548 | * Return all children, including those that have been deleted but are still in live. |
||
549 | * - Deleted children will be marked as "DeletedFromStage" |
||
550 | * - Added children will be marked as "AddedToStage" |
||
551 | * - Modified children will be marked as "ModifiedOnStage" |
||
552 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
553 | * |
||
554 | * @param mixed $context |
||
555 | * @return ArrayList |
||
556 | */ |
||
557 | public function AllChildrenIncludingDeleted($context = null) { |
||
560 | |||
561 | /** |
||
562 | * @see AllChildrenIncludingDeleted |
||
563 | * |
||
564 | * @param mixed $context |
||
565 | * @return ArrayList |
||
566 | */ |
||
567 | public function doAllChildrenIncludingDeleted($context = null) { |
||
595 | |||
596 | /** |
||
597 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
598 | * |
||
599 | * @return DataList |
||
600 | * @throws Exception |
||
601 | */ |
||
602 | public function AllHistoricalChildren() { |
||
611 | |||
612 | /** |
||
613 | * Return the number of children that this page ever had, including pages that were deleted. |
||
614 | * |
||
615 | * @return int |
||
616 | * @throws Exception |
||
617 | */ |
||
618 | public function numHistoricalChildren() { |
||
626 | |||
627 | /** |
||
628 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
629 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
630 | * |
||
631 | * @param bool $cache Whether to retrieve values from cache |
||
632 | * @return int |
||
633 | */ |
||
634 | public function numChildren($cache = true) { |
||
645 | |||
646 | /** |
||
647 | * Return children in the stage site. |
||
648 | * |
||
649 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
650 | * extension is applied to {@link SiteTree}. |
||
651 | * @return DataList |
||
652 | */ |
||
653 | public function stageChildren($showAll = false) { |
||
664 | |||
665 | /** |
||
666 | * Return children in the live site, if it exists. |
||
667 | * |
||
668 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
669 | * applicable when extension is applied to {@link SiteTree}. |
||
670 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
671 | * @return DataList |
||
672 | * @throws Exception |
||
673 | */ |
||
674 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) { |
||
692 | |||
693 | /** |
||
694 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
695 | * is returned. |
||
696 | * |
||
697 | * @param string $filter |
||
698 | * @return DataObject |
||
699 | */ |
||
700 | public function getParent($filter = null) { |
||
710 | |||
711 | /** |
||
712 | * Return all the parents of this class in a set ordered from the lowest to highest parent. |
||
713 | * |
||
714 | * @return ArrayList |
||
715 | */ |
||
716 | public function getAncestors() { |
||
726 | |||
727 | /** |
||
728 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
729 | * |
||
730 | * @param string $separator |
||
731 | * @return string |
||
732 | */ |
||
733 | public function getBreadcrumbs($separator = ' » ') { |
||
740 | |||
741 | /** |
||
742 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
743 | * then search the parents. |
||
744 | * |
||
745 | * @todo Write! |
||
746 | * |
||
747 | * @param string $className Class name of the node to find |
||
748 | * @param DataObject $afterNode Used for recursive calls to this function |
||
749 | * @return DataObject |
||
750 | */ |
||
751 | public function naturalPrev($className, $afterNode = null ) { |
||
754 | |||
755 | /** |
||
756 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
757 | * then search the parents. |
||
758 | * @param string $className Class name of the node to find. |
||
759 | * @param string|int $root ID/ClassName of the node to limit the search to |
||
760 | * @param DataObject $afterNode Used for recursive calls to this function |
||
761 | * @return DataObject |
||
762 | */ |
||
763 | public function naturalNext($className = null, $root = 0, $afterNode = null ) { |
||
807 | |||
808 | /** |
||
809 | * Flush all Hierarchy caches: |
||
810 | * - Children (instance) |
||
811 | * - NumChildren (instance) |
||
812 | * - Marked (global) |
||
813 | * - Expanded (global) |
||
814 | * - TreeOpened (global) |
||
815 | */ |
||
816 | public function flushCache() { |
||
823 | |||
824 | /** |
||
825 | * Reset global Hierarchy caches: |
||
826 | * - Marked |
||
827 | * - Expanded |
||
828 | * - TreeOpened |
||
829 | */ |
||
830 | public static function reset() { |
||
835 | |||
836 | } |
||
837 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: