| Total Complexity | 67 |
| Total Lines | 520 |
| 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() |
||
| 194 | { |
||
| 195 | $children = $this->owner->_cache_children; |
||
| 196 | if ($children) { |
||
| 197 | return $children; |
||
| 198 | } |
||
| 199 | |||
| 200 | $children = $this |
||
| 201 | ->owner |
||
| 202 | ->stageChildren(false) |
||
| 203 | ->filterByCallback(function (DataObject $record) { |
||
| 204 | return $record->canView(); |
||
| 205 | }); |
||
| 206 | $this->owner->_cache_children = $children; |
||
| 207 | return $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() |
||
| 257 | { |
||
| 258 | /** @var DataObject|Versioned|Hierarchy $owner */ |
||
| 259 | $owner = $this->owner; |
||
| 260 | if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) { |
||
| 261 | throw new Exception( |
||
| 262 | 'Hierarchy->AllHistoricalChildren() only works with Versioned extension applied with staging' |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | $baseTable = $owner->baseTable(); |
||
| 267 | $parentIDColumn = $owner->getSchema()->sqlColumnForField($owner, 'ParentID'); |
||
| 268 | return Versioned::get_including_deleted( |
||
| 269 | $owner->baseClass(), |
||
| 270 | [ $parentIDColumn => $owner->ID ], |
||
| 271 | "\"{$baseTable}\".\"ID\" ASC" |
||
| 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() |
||
| 281 | { |
||
| 282 | return $this->AllHistoricalChildren()->count(); |
||
| 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) |
||
| 293 | { |
||
| 294 | |||
| 295 | $baseClass = $this->owner->baseClass(); |
||
| 296 | $stage = 'Stage'; |
||
| 297 | $id = $this->owner->ID; |
||
| 298 | |||
| 299 | // cached call |
||
| 300 | if ($cache && isset(self::$cache_numChildren[$baseClass][$stage][$id])) { |
||
| 301 | return self::$cache_numChildren[$baseClass][$stage][$id]; |
||
| 302 | } |
||
| 303 | |||
| 304 | // We call stageChildren(), because Children() has canView() filtering |
||
| 305 | $numChildren = (int)$this->owner->stageChildren(true)->Count(); |
||
| 306 | |||
| 307 | // Save if caching |
||
| 308 | if ($cache) { |
||
| 309 | self::$cache_numChildren[$baseClass][$stage][$id] = $numChildren; |
||
| 310 | } |
||
| 311 | |||
| 312 | return $numChildren; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for |
||
| 317 | * a list of record IDs, for more efficient database querying. If $idList |
||
| 318 | * is null, then every record will be pre-cached. |
||
| 319 | * |
||
| 320 | * @param string $class |
||
| 321 | * @param string $stage |
||
| 322 | * @param array $idList |
||
| 323 | */ |
||
| 324 | public static function prepopulate_numchildren_cache($baseClass, $stage, $idList = null) |
||
| 325 | { |
||
| 326 | if (!Config::inst()->get(static::class, 'prepopulate_numchildren_cache')) { |
||
| 327 | return; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** @var Versioned|DataObject $singleton */ |
||
| 331 | $dummyObject = new $baseClass(); |
||
| 332 | $dummyObject->ID = -23478234; // going to use this for search & replace |
||
| 333 | $baseTable = $dummyObject->baseTable(); |
||
| 334 | |||
| 335 | $idColumn = Convert::symbol2sql("{$baseTable}.ID"); |
||
| 336 | $parentIdColumn = Convert::symbol2sql("ParentID"); |
||
| 337 | |||
| 338 | |||
| 339 | // Get the stageChildren() result of a dummy object and break down into a generic query |
||
| 340 | $query = $dummyObject->stageChildren(true)->dataQuery()->query(); |
||
| 341 | $where = $query->getWhere(); |
||
| 342 | |||
| 343 | $newWhere = []; |
||
| 344 | |||
| 345 | foreach ($where as $i => $compoundClause) { |
||
| 346 | foreach ($compoundClause as $clause => $params) { |
||
| 347 | // Skip any "WHERE ParentID = [marker]" clauses as this will be replaced with a GROUP BY |
||
| 348 | if (!(preg_match('/^"[^"]+"\."ParentID" = \?$/', $clause) && $clause[1] == $dummyObject->ID)) { |
||
| 349 | // Replace any marker IDs with "<basetable>"."ID" |
||
| 350 | $paramNum = 0; |
||
| 351 | foreach ($params as $j => $param) { |
||
| 352 | if ($param == $dummyObject->ID) { |
||
| 353 | unset($params[$j]); |
||
| 354 | $clause = DB::replace_parameter($clause, $paramNum, $parentIdColumn, true); |
||
| 355 | } else { |
||
| 356 | $paramNum++; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | $newWhere[] = [ $clause => $params ]; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | // optional ID-list filter |
||
| 366 | if ($idList) { |
||
| 367 | // Validate the ID list |
||
| 368 | foreach ($idList as $id) { |
||
| 369 | if (!is_numeric($id)) { |
||
| 370 | user_error( |
||
| 371 | "Bad ID passed to Versioned::prepopulate_numchildren_cache() in \$idList: " . $id, |
||
| 372 | E_USER_ERROR |
||
| 373 | ); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | $newWhere[] = ['"ParentID" IN (' . DB::placeholders($idList) . ')' => $idList]; |
||
| 377 | } |
||
| 378 | |||
| 379 | $query->setWhere($newWhere); |
||
| 380 | $query->setOrderBy(null); |
||
| 381 | |||
| 382 | $query->setSelect([ |
||
| 383 | '"ParentID"', |
||
| 384 | "COUNT(DISTINCT $idColumn) AS \"NumChildren\"", |
||
| 385 | ]); |
||
| 386 | $query->setGroupBy([ |
||
| 387 | "ParentID |
||
| 388 | "]); |
||
| 389 | |||
| 390 | $numChildrens = $query->execute()->map(); |
||
| 391 | foreach ($numChildrens as $id => $numChildren) { |
||
| 392 | self::$cache_numChildren[$baseClass][$stage][$id] = $numChildren; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | public function showingCMSTree() |
||
| 402 | { |
||
| 403 | if (!Controller::has_curr() || !class_exists(LeftAndMain::class)) { |
||
| 404 | return false; |
||
| 405 | } |
||
| 406 | $controller = Controller::curr(); |
||
| 407 | return $controller instanceof LeftAndMain |
||
| 408 | && in_array($controller->getAction(), array("treeview", "listview", "getsubtree")); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Return children in the stage site. |
||
| 413 | * |
||
| 414 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
| 415 | * extension is applied to {@link SiteTree}. |
||
| 416 | * @return DataList |
||
| 417 | */ |
||
| 418 | public function stageChildren($showAll = false) |
||
| 419 | { |
||
| 420 | $hideFromHierarchy = $this->owner->config()->hide_from_hierarchy; |
||
| 421 | $hideFromCMSTree = $this->owner->config()->hide_from_cms_tree; |
||
| 422 | $baseClass = $this->owner->baseClass(); |
||
| 423 | $staged = DataObject::get($baseClass) |
||
| 424 | ->filter('ParentID', (int)$this->owner->ID) |
||
| 425 | ->exclude('ID', (int)$this->owner->ID); |
||
| 426 | if ($hideFromHierarchy) { |
||
| 427 | $staged = $staged->exclude('ClassName', $hideFromHierarchy); |
||
| 428 | } |
||
| 429 | if ($hideFromCMSTree && $this->showingCMSTree()) { |
||
| 430 | $staged = $staged->exclude('ClassName', $hideFromCMSTree); |
||
| 431 | } |
||
| 432 | if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
||
| 433 | $staged = $staged->filter('ShowInMenus', 1); |
||
| 434 | } |
||
| 435 | $this->owner->extend("augmentStageChildren", $staged, $showAll); |
||
| 436 | return $staged; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Return children in the live site, if it exists. |
||
| 441 | * |
||
| 442 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
| 443 | * applicable when extension is applied to {@link SiteTree}. |
||
| 444 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
| 445 | * @return DataList |
||
| 446 | * @throws Exception |
||
| 447 | */ |
||
| 448 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
||
| 449 | { |
||
| 450 | /** @var Versioned|DataObject|Hierarchy $owner */ |
||
| 451 | $owner = $this->owner; |
||
| 452 | if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) { |
||
| 453 | throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied with staging'); |
||
| 454 | } |
||
| 455 | |||
| 456 | $hideFromHierarchy = $owner->config()->hide_from_hierarchy; |
||
| 457 | $hideFromCMSTree = $owner->config()->hide_from_cms_tree; |
||
| 458 | $children = DataObject::get($owner->baseClass()) |
||
| 459 | ->filter('ParentID', (int)$owner->ID) |
||
| 460 | ->exclude('ID', (int)$owner->ID) |
||
| 461 | ->setDataQueryParam(array( |
||
| 462 | 'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage', |
||
| 463 | 'Versioned.stage' => 'Live' |
||
| 464 | )); |
||
| 465 | if ($hideFromHierarchy) { |
||
| 466 | $children = $children->exclude('ClassName', $hideFromHierarchy); |
||
| 467 | } |
||
| 468 | if ($hideFromCMSTree && $this->showingCMSTree()) { |
||
| 469 | $children = $children->exclude('ClassName', $hideFromCMSTree); |
||
| 470 | } |
||
| 471 | if (!$showAll && DataObject::getSchema()->fieldSpec($owner, 'ShowInMenus')) { |
||
| 472 | $children = $children->filter('ShowInMenus', 1); |
||
| 473 | } |
||
| 474 | |||
| 475 | return $children; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
| 480 | * is returned. |
||
| 481 | * |
||
| 482 | * @param string $filter |
||
| 483 | * @return DataObject |
||
| 484 | */ |
||
| 485 | public function getParent($filter = null) |
||
| 496 | ]); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Return all the parents of this class in a set ordered from the closest to furtherest parent. |
||
| 501 | * |
||
| 502 | * @param bool $includeSelf |
||
| 503 | * @return ArrayList |
||
| 504 | */ |
||
| 505 | public function getAncestors($includeSelf = false) |
||
| 506 | { |
||
| 507 | $ancestors = new ArrayList(); |
||
| 508 | $object = $this->owner; |
||
| 509 | |||
| 510 | if ($includeSelf) { |
||
| 511 | $ancestors->push($object); |
||
| 512 | } |
||
| 513 | while ($object = $object->getParent()) { |
||
| 514 | $ancestors->push($object); |
||
| 515 | } |
||
| 516 | |||
| 517 | return $ancestors; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
| 522 | * |
||
| 523 | * @param string $separator |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public function getBreadcrumbs($separator = ' » ') |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Flush all Hierarchy caches: |
||
| 540 | * - Children (instance) |
||
| 541 | * - NumChildren (instance) |
||
| 542 | */ |
||
| 543 | public function flushCache() |
||
| 544 | { |
||
| 547 | } |
||
| 548 | } |
||
| 549 |
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