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 = null, $context = null, |
||
244 | |||
245 | /** |
||
246 | * Filter the marking to only those object with $node->$parameterName == $parameterValue |
||
247 | * |
||
248 | * @param string $parameterName The parameter on each node to check when marking. |
||
249 | * @param mixed $parameterValue The value the parameter must be to be marked. |
||
250 | */ |
||
251 | public function setMarkingFilter($parameterName, $parameterValue) { |
||
257 | |||
258 | /** |
||
259 | * Filter the marking to only those where the function returns true. The node in question will be passed to the |
||
260 | * function. |
||
261 | * |
||
262 | * @param string $funcName The name of the function to call |
||
263 | */ |
||
264 | public function setMarkingFilterFunction($funcName) { |
||
269 | |||
270 | /** |
||
271 | * Returns true if the marking filter matches on the given node. |
||
272 | * |
||
273 | * @param DataObject $node Node to check |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function markingFilterMatches($node) { |
||
298 | |||
299 | /** |
||
300 | * Mark all children of the given node that match the marking filter. |
||
301 | * |
||
302 | * @param DataObject $node Parent node |
||
303 | * @param mixed $context |
||
304 | * @param string $childrenMethod The name of the instance method to call to get the object's list of children |
||
305 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
306 | * @return DataList |
||
307 | */ |
||
308 | public function markChildren($node, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", |
||
335 | |||
336 | /** |
||
337 | * Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating |
||
338 | * over the tree. |
||
339 | * |
||
340 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
341 | */ |
||
342 | protected function markingFinished($numChildrenMethod = "numChildren") { |
||
352 | |||
353 | /** |
||
354 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
355 | * marking of this DataObject. |
||
356 | * |
||
357 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
358 | * @return string |
||
359 | */ |
||
360 | public function markingClasses($numChildrenMethod="numChildren") { |
||
376 | |||
377 | /** |
||
378 | * Mark the children of the DataObject with the given ID. |
||
379 | * |
||
380 | * @param int $id ID of parent node |
||
381 | * @param bool $open If this is true, mark the parent node as opened |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function markById($id, $open = false) { |
||
395 | |||
396 | /** |
||
397 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
398 | * |
||
399 | * @param DataObject $childObj |
||
400 | */ |
||
401 | public function markToExpose($childObj) { |
||
409 | |||
410 | /** |
||
411 | * Return the IDs of all the marked nodes. |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | public function markedNodeIDs() { |
||
418 | |||
419 | /** |
||
420 | * Return an array of this page and its ancestors, ordered item -> root. |
||
421 | * |
||
422 | * @return SiteTree[] |
||
423 | */ |
||
424 | public function parentStack() { |
||
434 | |||
435 | /** |
||
436 | * Cache of DataObjects' marked statuses: [ClassName][ID] = bool |
||
437 | * @var array |
||
438 | */ |
||
439 | protected static $marked = array(); |
||
440 | |||
441 | /** |
||
442 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
443 | * @var array |
||
444 | */ |
||
445 | protected static $expanded = array(); |
||
446 | |||
447 | /** |
||
448 | * Cache of DataObjects' opened statuses: [ClassName][ID] = bool |
||
449 | * @var array |
||
450 | */ |
||
451 | protected static $treeOpened = array(); |
||
452 | |||
453 | /** |
||
454 | * Mark this DataObject as expanded. |
||
455 | */ |
||
456 | public function markExpanded() { |
||
460 | |||
461 | /** |
||
462 | * Mark this DataObject as unexpanded. |
||
463 | */ |
||
464 | public function markUnexpanded() { |
||
468 | |||
469 | /** |
||
470 | * Mark this DataObject's tree as opened. |
||
471 | */ |
||
472 | public function markOpened() { |
||
476 | |||
477 | /** |
||
478 | * Mark this DataObject's tree as closed. |
||
479 | */ |
||
480 | public function markClosed() { |
||
485 | |||
486 | /** |
||
487 | * Check if this DataObject is marked. |
||
488 | * |
||
489 | * @return bool |
||
490 | */ |
||
491 | public function isMarked() { |
||
496 | |||
497 | /** |
||
498 | * Check if this DataObject is expanded. |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | public function isExpanded() { |
||
507 | |||
508 | /** |
||
509 | * Check if this DataObject's tree is opened. |
||
510 | * |
||
511 | * @return bool |
||
512 | */ |
||
513 | public function isTreeOpened() { |
||
518 | |||
519 | /** |
||
520 | * Get a list of this DataObject's and all it's descendants IDs. |
||
521 | * |
||
522 | * @return int[] |
||
523 | */ |
||
524 | public function getDescendantIDList() { |
||
529 | |||
530 | /** |
||
531 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
532 | * |
||
533 | * @param array $idList Array to put results in. |
||
534 | */ |
||
535 | public function loadDescendantIDListInto(&$idList) { |
||
549 | |||
550 | /** |
||
551 | * Get the children for this DataObject. |
||
552 | * |
||
553 | * @return DataList |
||
554 | */ |
||
555 | public function Children() { |
||
568 | |||
569 | /** |
||
570 | * Return all children, including those 'not in menus'. |
||
571 | * |
||
572 | * @return DataList |
||
573 | */ |
||
574 | public function AllChildren() { |
||
577 | |||
578 | /** |
||
579 | * Return all children, including those that have been deleted but are still in live. |
||
580 | * - Deleted children will be marked as "DeletedFromStage" |
||
581 | * - Added children will be marked as "AddedToStage" |
||
582 | * - Modified children will be marked as "ModifiedOnStage" |
||
583 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
584 | * |
||
585 | * @param mixed $context |
||
586 | * @return ArrayList |
||
587 | */ |
||
588 | public function AllChildrenIncludingDeleted($context = null) { |
||
591 | |||
592 | /** |
||
593 | * @see AllChildrenIncludingDeleted |
||
594 | * |
||
595 | * @param mixed $context |
||
596 | * @return ArrayList |
||
597 | */ |
||
598 | public function doAllChildrenIncludingDeleted($context = null) { |
||
626 | |||
627 | /** |
||
628 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
629 | * |
||
630 | * @return DataList |
||
631 | * @throws Exception |
||
632 | */ |
||
633 | public function AllHistoricalChildren() { |
||
642 | |||
643 | /** |
||
644 | * Return the number of children that this page ever had, including pages that were deleted. |
||
645 | * |
||
646 | * @return int |
||
647 | * @throws Exception |
||
648 | */ |
||
649 | public function numHistoricalChildren() { |
||
657 | |||
658 | /** |
||
659 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
660 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
661 | * |
||
662 | * @param bool $cache Whether to retrieve values from cache |
||
663 | * @return int |
||
664 | */ |
||
665 | public function numChildren($cache = true) { |
||
676 | |||
677 | /** |
||
678 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
679 | * |
||
680 | * @return bool |
||
681 | */ |
||
682 | public function showingCMSTree() { |
||
688 | |||
689 | /** |
||
690 | * Return children in the stage site. |
||
691 | * |
||
692 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
693 | * extension is applied to {@link SiteTree}. |
||
694 | * @return DataList |
||
695 | */ |
||
696 | public function stageChildren($showAll = false) { |
||
715 | |||
716 | /** |
||
717 | * Return children in the live site, if it exists. |
||
718 | * |
||
719 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
720 | * applicable when extension is applied to {@link SiteTree}. |
||
721 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
722 | * @return DataList |
||
723 | * @throws Exception |
||
724 | */ |
||
725 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) { |
||
750 | |||
751 | /** |
||
752 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
753 | * is returned. |
||
754 | * |
||
755 | * @param string $filter |
||
756 | * @return DataObject |
||
757 | */ |
||
758 | public function getParent($filter = null) { |
||
768 | |||
769 | /** |
||
770 | * Return all the parents of this class in a set ordered from the lowest to highest parent. |
||
771 | * |
||
772 | * @return ArrayList |
||
773 | */ |
||
774 | public function getAncestors() { |
||
784 | |||
785 | /** |
||
786 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
787 | * |
||
788 | * @param string $separator |
||
789 | * @return string |
||
790 | */ |
||
791 | public function getBreadcrumbs($separator = ' » ') { |
||
798 | |||
799 | /** |
||
800 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
801 | * then search the parents. |
||
802 | * |
||
803 | * @todo Write! |
||
804 | * |
||
805 | * @param string $className Class name of the node to find |
||
806 | * @param DataObject $afterNode Used for recursive calls to this function |
||
807 | * @return DataObject |
||
808 | */ |
||
809 | public function naturalPrev($className, $afterNode = null ) { |
||
812 | |||
813 | /** |
||
814 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
815 | * then search the parents. |
||
816 | * @param string $className Class name of the node to find. |
||
817 | * @param string|int $root ID/ClassName of the node to limit the search to |
||
818 | * @param DataObject $afterNode Used for recursive calls to this function |
||
819 | * @return DataObject |
||
820 | */ |
||
821 | public function naturalNext($className = null, $root = 0, $afterNode = null ) { |
||
865 | |||
866 | /** |
||
867 | * Flush all Hierarchy caches: |
||
868 | * - Children (instance) |
||
869 | * - NumChildren (instance) |
||
870 | * - Marked (global) |
||
871 | * - Expanded (global) |
||
872 | * - TreeOpened (global) |
||
873 | */ |
||
874 | public function flushCache() { |
||
881 | |||
882 | /** |
||
883 | * Reset global Hierarchy caches: |
||
884 | * - Marked |
||
885 | * - Expanded |
||
886 | * - TreeOpened |
||
887 | */ |
||
888 | public static function reset() { |
||
893 | |||
894 | } |
||
895 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: