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", |
||
305 | |||
306 | /** |
||
307 | * Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating |
||
308 | * over the tree. |
||
309 | * |
||
310 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
311 | */ |
||
312 | protected function markingFinished($numChildrenMethod = "numChildren") { |
||
322 | |||
323 | /** |
||
324 | * Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the |
||
325 | * marking of this DataObject. |
||
326 | * |
||
327 | * @param string $numChildrenMethod The name of the instance method to call to count the object's children |
||
328 | * @return string |
||
329 | */ |
||
330 | public function markingClasses($numChildrenMethod="numChildren") { |
||
346 | |||
347 | /** |
||
348 | * Mark the children of the DataObject with the given ID. |
||
349 | * |
||
350 | * @param int $id ID of parent node |
||
351 | * @param bool $open If this is true, mark the parent node as opened |
||
352 | * @return bool |
||
353 | */ |
||
354 | public function markById($id, $open = false) { |
||
365 | |||
366 | /** |
||
367 | * Expose the given object in the tree, by marking this page and all it ancestors. |
||
368 | * |
||
369 | * @param DataObject $childObj |
||
370 | */ |
||
371 | public function markToExpose($childObj) { |
||
379 | |||
380 | /** |
||
381 | * Return the IDs of all the marked nodes. |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | public function markedNodeIDs() { |
||
388 | |||
389 | /** |
||
390 | * Return an array of this page and its ancestors, ordered item -> root. |
||
391 | * |
||
392 | * @return SiteTree[] |
||
393 | */ |
||
394 | public function parentStack() { |
||
404 | |||
405 | /** |
||
406 | * Cache of DataObjects' marked statuses: [ClassName][ID] = bool |
||
407 | * @var array |
||
408 | */ |
||
409 | protected static $marked = array(); |
||
410 | |||
411 | /** |
||
412 | * Cache of DataObjects' expanded statuses: [ClassName][ID] = bool |
||
413 | * @var array |
||
414 | */ |
||
415 | protected static $expanded = array(); |
||
416 | |||
417 | /** |
||
418 | * Cache of DataObjects' opened statuses: [ClassName][ID] = bool |
||
419 | * @var array |
||
420 | */ |
||
421 | protected static $treeOpened = array(); |
||
422 | |||
423 | /** |
||
424 | * Mark this DataObject as expanded. |
||
425 | */ |
||
426 | public function markExpanded() { |
||
430 | |||
431 | /** |
||
432 | * Mark this DataObject as unexpanded. |
||
433 | */ |
||
434 | public function markUnexpanded() { |
||
438 | |||
439 | /** |
||
440 | * Mark this DataObject's tree as opened. |
||
441 | */ |
||
442 | public function markOpened() { |
||
446 | |||
447 | /** |
||
448 | * Mark this DataObject's tree as closed. |
||
449 | */ |
||
450 | public function markClosed() { |
||
455 | |||
456 | /** |
||
457 | * Check if this DataObject is marked. |
||
458 | * |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function isMarked() { |
||
466 | |||
467 | /** |
||
468 | * Check if this DataObject is expanded. |
||
469 | * |
||
470 | * @return bool |
||
471 | */ |
||
472 | public function isExpanded() { |
||
477 | |||
478 | /** |
||
479 | * Check if this DataObject's tree is opened. |
||
480 | * |
||
481 | * @return bool |
||
482 | */ |
||
483 | public function isTreeOpened() { |
||
488 | |||
489 | /** |
||
490 | * Get a list of this DataObject's and all it's descendants IDs. |
||
491 | * |
||
492 | * @return int[] |
||
493 | */ |
||
494 | public function getDescendantIDList() { |
||
499 | |||
500 | /** |
||
501 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
502 | * |
||
503 | * @param array $idList Array to put results in. |
||
504 | */ |
||
505 | public function loadDescendantIDListInto(&$idList) { |
||
519 | |||
520 | /** |
||
521 | * Get the children for this DataObject. |
||
522 | * |
||
523 | * @return DataList |
||
524 | */ |
||
525 | public function Children() { |
||
538 | |||
539 | /** |
||
540 | * Return all children, including those 'not in menus'. |
||
541 | * |
||
542 | * @return DataList |
||
543 | */ |
||
544 | public function AllChildren() { |
||
547 | |||
548 | /** |
||
549 | * Return all children, including those that have been deleted but are still in live. |
||
550 | * - Deleted children will be marked as "DeletedFromStage" |
||
551 | * - Added children will be marked as "AddedToStage" |
||
552 | * - Modified children will be marked as "ModifiedOnStage" |
||
553 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
554 | * |
||
555 | * @param mixed $context |
||
556 | * @return ArrayList |
||
557 | */ |
||
558 | public function AllChildrenIncludingDeleted($context = null) { |
||
561 | |||
562 | /** |
||
563 | * @see AllChildrenIncludingDeleted |
||
564 | * |
||
565 | * @param mixed $context |
||
566 | * @return ArrayList |
||
567 | */ |
||
568 | public function doAllChildrenIncludingDeleted($context = null) { |
||
596 | |||
597 | /** |
||
598 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
599 | * |
||
600 | * @return DataList |
||
601 | * @throws Exception |
||
602 | */ |
||
603 | public function AllHistoricalChildren() { |
||
612 | |||
613 | /** |
||
614 | * Return the number of children that this page ever had, including pages that were deleted. |
||
615 | * |
||
616 | * @return int |
||
617 | * @throws Exception |
||
618 | */ |
||
619 | public function numHistoricalChildren() { |
||
627 | |||
628 | /** |
||
629 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
630 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
631 | * |
||
632 | * @param bool $cache Whether to retrieve values from cache |
||
633 | * @return int |
||
634 | */ |
||
635 | public function numChildren($cache = true) { |
||
646 | |||
647 | /** |
||
648 | * Return children in the stage site. |
||
649 | * |
||
650 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
651 | * extension is applied to {@link SiteTree}. |
||
652 | * @return DataList |
||
653 | */ |
||
654 | public function stageChildren($showAll = false) { |
||
665 | |||
666 | /** |
||
667 | * Return children in the live site, if it exists. |
||
668 | * |
||
669 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
670 | * applicable when extension is applied to {@link SiteTree}. |
||
671 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
672 | * @return DataList |
||
673 | * @throws Exception |
||
674 | */ |
||
675 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) { |
||
693 | |||
694 | /** |
||
695 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
696 | * is returned. |
||
697 | * |
||
698 | * @param string $filter |
||
699 | * @return DataObject |
||
700 | */ |
||
701 | public function getParent($filter = null) { |
||
711 | |||
712 | /** |
||
713 | * Return all the parents of this class in a set ordered from the lowest to highest parent. |
||
714 | * |
||
715 | * @return ArrayList |
||
716 | */ |
||
717 | public function getAncestors() { |
||
727 | |||
728 | /** |
||
729 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
730 | * |
||
731 | * @param string $separator |
||
732 | * @return string |
||
733 | */ |
||
734 | public function getBreadcrumbs($separator = ' » ') { |
||
741 | |||
742 | /** |
||
743 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
744 | * then search the parents. |
||
745 | * |
||
746 | * @todo Write! |
||
747 | * |
||
748 | * @param string $className Class name of the node to find |
||
749 | * @param DataObject $afterNode Used for recursive calls to this function |
||
750 | * @return DataObject |
||
751 | */ |
||
752 | public function naturalPrev($className, $afterNode = null ) { |
||
755 | |||
756 | /** |
||
757 | * Get the next node in the tree of the type. If there is no instance of the className descended from this node, |
||
758 | * then search the parents. |
||
759 | * @param string $className Class name of the node to find. |
||
760 | * @param string|int $root ID/ClassName of the node to limit the search to |
||
761 | * @param DataObject $afterNode Used for recursive calls to this function |
||
762 | * @return DataObject |
||
763 | */ |
||
764 | public function naturalNext($className = null, $root = 0, $afterNode = null ) { |
||
808 | |||
809 | /** |
||
810 | * Flush all Hierarchy caches: |
||
811 | * - Children (instance) |
||
812 | * - NumChildren (instance) |
||
813 | * - Marked (global) |
||
814 | * - Expanded (global) |
||
815 | * - TreeOpened (global) |
||
816 | */ |
||
817 | public function flushCache() { |
||
824 | |||
825 | /** |
||
826 | * Reset global Hierarchy caches: |
||
827 | * - Marked |
||
828 | * - Expanded |
||
829 | * - TreeOpened |
||
830 | */ |
||
831 | public static function reset() { |
||
836 | |||
837 | } |
||
838 |
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: