Total Complexity | 63 |
Total Lines | 510 |
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 the cache for Versioned::get_versionnumber_by_stage() for |
||
322 | * a list of record IDs, for more efficient database querying. If $idList |
||
323 | * is null, then every record will be pre-cached. |
||
324 | * |
||
325 | * @param string $class |
||
326 | * @param string $stage |
||
327 | * @param array $idList |
||
328 | */ |
||
329 | public static function prepopulate_numchildren_cache($baseClass, $stage, $idList = null) |
||
330 | { |
||
331 | if (!Config::inst()->get(static::class, 'prepopulate_numchildren_cache')) { |
||
332 | return; |
||
333 | } |
||
334 | |||
335 | /** @var Versioned|DataObject $singleton */ |
||
336 | $dummyObject = DataObject::singleton($baseClass); |
||
337 | $baseTable = $dummyObject->baseTable(); |
||
338 | |||
339 | $idColumn = Convert::symbol2sql("{$baseTable}.ID"); |
||
340 | |||
341 | // Get the stageChildren() result of a dummy object and break down into a generic query |
||
342 | $query = $dummyObject->stageChildren(true, true)->dataQuery()->query(); |
||
343 | |||
344 | // optional ID-list filter |
||
345 | if ($idList) { |
||
346 | // Validate the ID list |
||
347 | foreach ($idList as $id) { |
||
348 | if (!is_numeric($id)) { |
||
349 | user_error( |
||
350 | "Bad ID passed to Versioned::prepopulate_numchildren_cache() in \$idList: " . $id, |
||
351 | E_USER_ERROR |
||
352 | ); |
||
353 | } |
||
354 | } |
||
355 | $query->addWhere(['"ParentID" IN (' . DB::placeholders($idList) . ')' => $idList]); |
||
356 | } |
||
357 | |||
358 | $query->setOrderBy(null); |
||
359 | |||
360 | $query->setSelect([ |
||
361 | '"ParentID"', |
||
362 | "COUNT(DISTINCT $idColumn) AS \"NumChildren\"", |
||
363 | ]); |
||
364 | $query->setGroupBy([ |
||
365 | "ParentID |
||
366 | "]); |
||
367 | |||
368 | $numChildren = $query->execute()->map(); |
||
369 | self::$cache_numChildren[$baseClass][$stage] = $numChildren; |
||
370 | if (!$idList) { |
||
371 | // If all objects are being cached, mark this cache as complete |
||
372 | // to avoid counting children of childless object. |
||
373 | self::$cache_numChildren[$baseClass][$stage]['_complete'] = true; |
||
374 | } |
||
375 | |||
376 | var_dump(self::$cache_numChildren); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function showingCMSTree() |
||
385 | { |
||
386 | if (!Controller::has_curr() || !class_exists(LeftAndMain::class)) { |
||
387 | return false; |
||
388 | } |
||
389 | $controller = Controller::curr(); |
||
390 | return $controller instanceof LeftAndMain |
||
391 | && in_array($controller->getAction(), array("treeview", "listview", "getsubtree")); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Return children in the stage site. |
||
396 | * |
||
397 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
398 | * extension is applied to {@link SiteTree}. |
||
399 | * @param bool $IDLess Set to true to supress the ParentID and ID where statements. |
||
400 | * @return DataList |
||
401 | */ |
||
402 | public function stageChildren($showAll = false, $IDLess = false) |
||
403 | { |
||
404 | $hideFromHierarchy = $this->owner->config()->hide_from_hierarchy; |
||
405 | $hideFromCMSTree = $this->owner->config()->hide_from_cms_tree; |
||
406 | $baseClass = $this->owner->baseClass(); |
||
407 | $staged = DataObject::get($baseClass); |
||
408 | |||
409 | if (!$IDLess) { |
||
410 | // There's no filtering by ID if we don't have an ID. |
||
411 | $staged = $staged |
||
412 | ->filter('ParentID', (int)$this->owner->ID) |
||
413 | ->exclude('ID', (int)$this->owner->ID); |
||
414 | } |
||
415 | |||
416 | if ($hideFromHierarchy) { |
||
417 | $staged = $staged->exclude('ClassName', $hideFromHierarchy); |
||
418 | } |
||
419 | if ($hideFromCMSTree && $this->showingCMSTree()) { |
||
420 | $staged = $staged->exclude('ClassName', $hideFromCMSTree); |
||
421 | } |
||
422 | if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
||
423 | $staged = $staged->filter('ShowInMenus', 1); |
||
424 | } |
||
425 | $this->owner->extend("augmentStageChildren", $staged, $showAll); |
||
426 | return $staged; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Return children in the live site, if it exists. |
||
431 | * |
||
432 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
433 | * applicable when extension is applied to {@link SiteTree}. |
||
434 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
435 | * @return DataList |
||
436 | * @throws Exception |
||
437 | */ |
||
438 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
||
439 | { |
||
440 | /** @var Versioned|DataObject|Hierarchy $owner */ |
||
441 | $owner = $this->owner; |
||
442 | if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) { |
||
443 | throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied with staging'); |
||
444 | } |
||
445 | |||
446 | $hideFromHierarchy = $owner->config()->hide_from_hierarchy; |
||
447 | $hideFromCMSTree = $owner->config()->hide_from_cms_tree; |
||
448 | $children = DataObject::get($owner->baseClass()) |
||
449 | ->filter('ParentID', (int)$owner->ID) |
||
450 | ->exclude('ID', (int)$owner->ID) |
||
451 | ->setDataQueryParam(array( |
||
452 | 'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage', |
||
453 | 'Versioned.stage' => 'Live' |
||
454 | )); |
||
455 | if ($hideFromHierarchy) { |
||
456 | $children = $children->exclude('ClassName', $hideFromHierarchy); |
||
457 | } |
||
458 | if ($hideFromCMSTree && $this->showingCMSTree()) { |
||
459 | $children = $children->exclude('ClassName', $hideFromCMSTree); |
||
460 | } |
||
461 | if (!$showAll && DataObject::getSchema()->fieldSpec($owner, 'ShowInMenus')) { |
||
462 | $children = $children->filter('ShowInMenus', 1); |
||
463 | } |
||
464 | |||
465 | return $children; |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
470 | * is returned. |
||
471 | * |
||
472 | * @param string $filter |
||
473 | * @return DataObject |
||
474 | */ |
||
475 | public function getParent($filter = null) |
||
486 | ]); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Return all the parents of this class in a set ordered from the closest to furtherest parent. |
||
491 | * |
||
492 | * @param bool $includeSelf |
||
493 | * @return ArrayList |
||
494 | */ |
||
495 | public function getAncestors($includeSelf = false) |
||
496 | { |
||
497 | $ancestors = new ArrayList(); |
||
498 | $object = $this->owner; |
||
499 | |||
500 | if ($includeSelf) { |
||
501 | $ancestors->push($object); |
||
502 | } |
||
503 | while ($object = $object->getParent()) { |
||
504 | $ancestors->push($object); |
||
505 | } |
||
506 | |||
507 | return $ancestors; |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
512 | * |
||
513 | * @param string $separator |
||
514 | * @return string |
||
515 | */ |
||
516 | public function getBreadcrumbs($separator = ' » ') |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Flush all Hierarchy caches: |
||
530 | * - Children (instance) |
||
531 | * - NumChildren (instance) |
||
532 | */ |
||
533 | public function flushCache() |
||
537 | } |
||
538 | } |
||
539 |
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