Total Complexity | 68 |
Total Lines | 533 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
27 | class Hierarchy extends DataExtension |
||
28 | { |
||
29 | /** |
||
30 | * The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least |
||
31 | * this number, and then stops. Root nodes will always show regardless of this settting. Further nodes can be |
||
32 | * lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30 |
||
33 | * children, the actual node count will be 50 (all root nodes plus first expanded child). |
||
34 | * |
||
35 | * @config |
||
36 | * @var int |
||
37 | */ |
||
38 | private static $node_threshold_total = 50; |
||
39 | |||
40 | /** |
||
41 | * Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available |
||
42 | * server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding |
||
43 | * this value typically won't display any children, although this is configurable through the $nodeCountCallback |
||
44 | * parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting. |
||
45 | * |
||
46 | * @config |
||
47 | * @var int |
||
48 | */ |
||
49 | private static $node_threshold_leaf = 250; |
||
50 | |||
51 | /** |
||
52 | * A list of classnames to exclude from display in both the CMS and front end |
||
53 | * displays. ->Children() and ->AllChildren affected. |
||
54 | * Especially useful for big sets of pages like listings |
||
55 | * If you use this, and still need the classes to be editable |
||
56 | * then add a model admin for the class |
||
57 | * Note: Does not filter subclasses (non-inheriting) |
||
58 | * |
||
59 | * @var array |
||
60 | * @config |
||
61 | */ |
||
62 | private static $hide_from_hierarchy = array(); |
||
63 | |||
64 | /** |
||
65 | * A list of classnames to exclude from display in the page tree views of the CMS, |
||
66 | * unlike $hide_from_hierarchy above which effects both CMS and front end. |
||
67 | * Especially useful for big sets of pages like listings |
||
68 | * If you use this, and still need the classes to be editable |
||
69 | * then add a model admin for the class |
||
70 | * Note: Does not filter subclasses (non-inheriting) |
||
71 | * |
||
72 | * @var array |
||
73 | * @config |
||
74 | */ |
||
75 | private static $hide_from_cms_tree = array(); |
||
76 | |||
77 | /** |
||
78 | * Used to enable or disable the prepopulation of the numchildren cache. |
||
79 | * Defaults to true. |
||
80 | * |
||
81 | * @config |
||
82 | * @var boolean |
||
83 | */ |
||
84 | private static $prepopulate_numchildren_cache = true; |
||
85 | |||
86 | /** |
||
87 | * Prevent virtual page virtualising these fields |
||
88 | * |
||
89 | * @config |
||
90 | * @var array |
||
91 | */ |
||
92 | private static $non_virtual_fields = [ |
||
93 | '_cache_children', |
||
94 | ]; |
||
95 | |||
96 | /** |
||
97 | * A cache used by numChildren(). |
||
98 | * Clear through {@link flushCache()}. |
||
99 | * version (int)0 means not on this stage. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected static $cache_numChildren = []; |
||
104 | |||
105 | public static function get_extra_config($class, $extension, $args) |
||
106 | { |
||
107 | return array( |
||
108 | 'has_one' => array('Parent' => $class) |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Validate the owner object - check for existence of infinite loops. |
||
114 | * |
||
115 | * @param ValidationResult $validationResult |
||
116 | */ |
||
117 | public function validate(ValidationResult $validationResult) |
||
118 | { |
||
119 | // The object is new, won't be looping. |
||
120 | /** @var DataObject|Hierarchy $owner */ |
||
121 | $owner = $this->owner; |
||
122 | if (!$owner->ID) { |
||
123 | return; |
||
124 | } |
||
125 | // The object has no parent, won't be looping. |
||
126 | if (!$owner->ParentID) { |
||
127 | return; |
||
128 | } |
||
129 | // The parent has not changed, skip the check for performance reasons. |
||
130 | if (!$owner->isChanged('ParentID')) { |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | // Walk the hierarchy upwards until we reach the top, or until we reach the originating node again. |
||
135 | $node = $owner; |
||
136 | while ($node && $node->ParentID) { |
||
137 | if ((int)$node->ParentID === (int)$owner->ID) { |
||
138 | // Hierarchy is looping. |
||
139 | $validationResult->addError( |
||
140 | _t( |
||
141 | __CLASS__ . '.InfiniteLoopNotAllowed', |
||
142 | 'Infinite loop found within the "{type}" hierarchy. Please change the parent to resolve this', |
||
143 | 'First argument is the class that makes up the hierarchy.', |
||
144 | array('type' => get_class($owner)) |
||
145 | ), |
||
146 | 'bad', |
||
147 | 'INFINITE_LOOP' |
||
148 | ); |
||
149 | break; |
||
150 | } |
||
151 | $node = $node->Parent(); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Get a list of this DataObject's and all it's descendants IDs. |
||
158 | * |
||
159 | * @return int[] |
||
160 | */ |
||
161 | public function getDescendantIDList() |
||
162 | { |
||
163 | $idList = array(); |
||
164 | $this->loadDescendantIDListInto($idList); |
||
165 | return $idList; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
170 | * |
||
171 | * @param array $idList Array to put results in. |
||
172 | * @param DataObject|Hierarchy $node |
||
173 | */ |
||
174 | protected function loadDescendantIDListInto(&$idList, $node = null) |
||
175 | { |
||
176 | if (!$node) { |
||
177 | $node = $this->owner; |
||
178 | } |
||
179 | $children = $node->AllChildren(); |
||
180 | foreach ($children as $child) { |
||
181 | if (!in_array($child->ID, $idList)) { |
||
182 | $idList[] = $child->ID; |
||
183 | $this->loadDescendantIDListInto($idList, $child); |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the children for this DataObject filtered by canView() |
||
190 | * |
||
191 | * @return SS_List |
||
192 | */ |
||
193 | public function Children() |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Return all children, including those 'not in menus'. |
||
212 | * |
||
213 | * @return DataList |
||
214 | */ |
||
215 | public function AllChildren() |
||
216 | { |
||
217 | return $this->owner->stageChildren(true); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Return all children, including those that have been deleted but are still in live. |
||
222 | * - Deleted children will be marked as "DeletedFromStage" |
||
223 | * - Added children will be marked as "AddedToStage" |
||
224 | * - Modified children will be marked as "ModifiedOnStage" |
||
225 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
226 | * |
||
227 | * @return ArrayList |
||
228 | */ |
||
229 | public function AllChildrenIncludingDeleted() |
||
230 | { |
||
231 | /** @var DataObject|Hierarchy|Versioned $owner */ |
||
232 | $owner = $this->owner; |
||
233 | $stageChildren = $owner->stageChildren(true); |
||
234 | |||
235 | // Add live site content that doesn't exist on the stage site, if required. |
||
236 | if ($owner->hasExtension(Versioned::class) && $owner->hasStages()) { |
||
237 | // Next, go through the live children. Only some of these will be listed |
||
238 | $liveChildren = $owner->liveChildren(true, true); |
||
239 | if ($liveChildren) { |
||
240 | $merged = new ArrayList(); |
||
241 | $merged->merge($stageChildren); |
||
242 | $merged->merge($liveChildren); |
||
243 | $stageChildren = $merged; |
||
244 | } |
||
245 | } |
||
246 | $owner->extend("augmentAllChildrenIncludingDeleted", $stageChildren); |
||
247 | return $stageChildren; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
252 | * |
||
253 | * @return DataList |
||
254 | * @throws Exception |
||
255 | */ |
||
256 | public function AllHistoricalChildren() |
||
272 | ); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Return the number of children that this page ever had, including pages that were deleted. |
||
277 | * |
||
278 | * @return int |
||
279 | */ |
||
280 | public function numHistoricalChildren() |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
287 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
288 | * |
||
289 | * @param bool $cache Whether to retrieve values from cache |
||
290 | * @return int |
||
291 | */ |
||
292 | public function numChildren($cache = true) |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Pre-populate any appropriate caches prior to rendering a tree. |
||
322 | * This is used to allow for the efficient rendering of tree views, notably in the CMS. |
||
323 | * In the cace of Hierarchy, it caches numChildren values. Other extensions can provide an |
||
324 | * onPrepopulateTreeDataCache(DataList $recordList = null, array $options) methods to hook |
||
325 | * into this event as well. |
||
326 | * |
||
327 | * @param DataList|array $recordList The list of records to prepopulate caches for. Null for all records. |
||
328 | * @param array $options A map of hints about what should be cached. "numChildrenMethod" and |
||
329 | * "childrenMethod" are allowed keys. |
||
330 | */ |
||
331 | public function prepopulateTreeDataCache($recordList = null, array $options = []) |
||
332 | { |
||
333 | if (empty($options['numChildrenMethod']) || $options['numChildrenMethod'] === 'numChildren') { |
||
334 | $idList = is_array($recordList) ? $recordList : |
||
335 | ($recordList instanceof DataList ? $recordList->column('ID') : null); |
||
336 | self::prepopulate_numchildren_cache($this->owner->baseClass(), $idList); |
||
337 | } |
||
338 | |||
339 | $this->owner->extend('onPrepopulateTreeDataCache', $recordList, $options); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for |
||
344 | * a list of record IDs, for more efficient database querying. If $idList |
||
345 | * is null, then every record will be pre-cached. |
||
346 | * |
||
347 | * @param string $class |
||
348 | * @param string $stage |
||
349 | * @param array $idList |
||
350 | */ |
||
351 | public static function prepopulate_numchildren_cache($baseClass, $idList = null) |
||
394 | } |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function showingCMSTree() |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Return children in the stage site. |
||
414 | * |
||
415 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
416 | * extension is applied to {@link SiteTree}. |
||
417 | * @param bool $skipParentIDFilter Set to true to supress the ParentID and ID where statements. |
||
418 | * @return DataList |
||
419 | */ |
||
420 | public function stageChildren($showAll = false, $skipParentIDFilter = false) |
||
421 | { |
||
422 | $hideFromHierarchy = $this->owner->config()->hide_from_hierarchy; |
||
423 | $hideFromCMSTree = $this->owner->config()->hide_from_cms_tree; |
||
424 | $baseClass = $this->owner->baseClass(); |
||
425 | $baseTable = $this->owner->baseTable(); |
||
426 | $staged = DataObject::get($baseClass)->where(sprintf( |
||
427 | '%s.%s <> %s.%s', |
||
428 | Convert::symbol2sql($baseTable), |
||
429 | Convert::symbol2sql("ParentID"), |
||
430 | Convert::symbol2sql($baseTable), |
||
431 | Convert::symbol2sql("ID") |
||
432 | )); |
||
433 | |||
434 | if (!$skipParentIDFilter) { |
||
435 | // There's no filtering by ID if we don't have an ID. |
||
436 | $staged = $staged->filter('ParentID', (int)$this->owner->ID); |
||
437 | } |
||
438 | |||
439 | if ($hideFromHierarchy) { |
||
440 | $staged = $staged->exclude('ClassName', $hideFromHierarchy); |
||
441 | } |
||
442 | if ($hideFromCMSTree && $this->showingCMSTree()) { |
||
443 | $staged = $staged->exclude('ClassName', $hideFromCMSTree); |
||
444 | } |
||
445 | if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
||
446 | $staged = $staged->filter('ShowInMenus', 1); |
||
447 | } |
||
448 | $this->owner->extend("augmentStageChildren", $staged, $showAll); |
||
449 | return $staged; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Return children in the live site, if it exists. |
||
454 | * |
||
455 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
456 | * applicable when extension is applied to {@link SiteTree}. |
||
457 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
458 | * @return DataList |
||
459 | * @throws Exception |
||
460 | */ |
||
461 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
||
462 | { |
||
463 | /** @var Versioned|DataObject|Hierarchy $owner */ |
||
464 | $owner = $this->owner; |
||
465 | if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) { |
||
466 | throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied with staging'); |
||
467 | } |
||
468 | |||
469 | $hideFromHierarchy = $owner->config()->hide_from_hierarchy; |
||
470 | $hideFromCMSTree = $owner->config()->hide_from_cms_tree; |
||
471 | $children = DataObject::get($owner->baseClass()) |
||
472 | ->filter('ParentID', (int)$owner->ID) |
||
473 | ->exclude('ID', (int)$owner->ID) |
||
474 | ->setDataQueryParam(array( |
||
475 | 'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage', |
||
476 | 'Versioned.stage' => 'Live' |
||
477 | )); |
||
478 | if ($hideFromHierarchy) { |
||
479 | $children = $children->exclude('ClassName', $hideFromHierarchy); |
||
480 | } |
||
481 | if ($hideFromCMSTree && $this->showingCMSTree()) { |
||
482 | $children = $children->exclude('ClassName', $hideFromCMSTree); |
||
483 | } |
||
484 | if (!$showAll && DataObject::getSchema()->fieldSpec($owner, 'ShowInMenus')) { |
||
485 | $children = $children->filter('ShowInMenus', 1); |
||
486 | } |
||
487 | |||
488 | return $children; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
493 | * is returned. |
||
494 | * |
||
495 | * @param string $filter |
||
496 | * @return DataObject |
||
497 | */ |
||
498 | public function getParent($filter = null) |
||
509 | ]); |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * Return all the parents of this class in a set ordered from the closest to furtherest parent. |
||
514 | * |
||
515 | * @param bool $includeSelf |
||
516 | * @return ArrayList |
||
517 | */ |
||
518 | public function getAncestors($includeSelf = false) |
||
519 | { |
||
520 | $ancestors = new ArrayList(); |
||
521 | $object = $this->owner; |
||
522 | |||
523 | if ($includeSelf) { |
||
524 | $ancestors->push($object); |
||
525 | } |
||
526 | while ($object = $object->getParent()) { |
||
527 | $ancestors->push($object); |
||
528 | } |
||
529 | |||
530 | return $ancestors; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
535 | * |
||
536 | * @param string $separator |
||
537 | * @return string |
||
538 | */ |
||
539 | public function getBreadcrumbs($separator = ' » ') |
||
549 | } |
||
550 | |||
551 | /** |
||
552 | * Flush all Hierarchy caches: |
||
553 | * - Children (instance) |
||
554 | * - NumChildren (instance) |
||
555 | */ |
||
556 | public function flushCache() |
||
560 | } |
||
561 | } |
||
562 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths