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 |
||
| 24 | class Hierarchy extends DataExtension |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Cache for {@see numChildren()} |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $_cache_numChildren = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Cache for {@see Children()} |
||
| 36 | * |
||
| 37 | * @var SS_List |
||
| 38 | */ |
||
| 39 | protected $_cache_children = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least |
||
| 43 | * this number, and then stops. Root nodes will always show regardless of this settting. Further nodes can be |
||
| 44 | * lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30 |
||
| 45 | * children, the actual node count will be 50 (all root nodes plus first expanded child). |
||
| 46 | * |
||
| 47 | * @config |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | private static $node_threshold_total = 50; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available |
||
| 54 | * server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding |
||
| 55 | * this value typically won't display any children, although this is configurable through the $nodeCountCallback |
||
| 56 | * parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting. |
||
| 57 | * |
||
| 58 | * @config |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | private static $node_threshold_leaf = 250; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * A list of classnames to exclude from display in both the CMS and front end |
||
| 65 | * displays. ->Children() and ->AllChildren affected. |
||
| 66 | * Especially useful for big sets of pages like listings |
||
| 67 | * If you use this, and still need the classes to be editable |
||
| 68 | * then add a model admin for the class |
||
| 69 | * Note: Does not filter subclasses (non-inheriting) |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | * @config |
||
| 73 | */ |
||
| 74 | private static $hide_from_hierarchy = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * A list of classnames to exclude from display in the page tree views of the CMS, |
||
| 78 | * unlike $hide_from_hierarchy above which effects both CMS and front end. |
||
| 79 | * Especially useful for big sets of pages like listings |
||
| 80 | * If you use this, and still need the classes to be editable |
||
| 81 | * then add a model admin for the class |
||
| 82 | * Note: Does not filter subclasses (non-inheriting) |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | * @config |
||
| 86 | */ |
||
| 87 | private static $hide_from_cms_tree = array(); |
||
| 88 | |||
| 89 | public static function get_extra_config($class, $extension, $args) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Validate the owner object - check for existence of infinite loops. |
||
| 98 | * |
||
| 99 | * @param ValidationResult $validationResult |
||
| 100 | */ |
||
| 101 | public function validate(ValidationResult $validationResult) |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Get a list of this DataObject's and all it's descendants IDs. |
||
| 142 | * |
||
| 143 | * @return int[] |
||
| 144 | */ |
||
| 145 | public function getDescendantIDList() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get a list of this DataObject's and all it's descendants ID, and put them in $idList. |
||
| 154 | * |
||
| 155 | * @param array $idList Array to put results in. |
||
| 156 | * @param DataObject|Hierarchy $node |
||
| 157 | */ |
||
| 158 | protected function loadDescendantIDListInto(&$idList, $node = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the children for this DataObject filtered by canView() |
||
| 174 | * |
||
| 175 | * @return SS_List |
||
| 176 | */ |
||
| 177 | public function Children() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Return all children, including those 'not in menus'. |
||
| 194 | * |
||
| 195 | * @return DataList |
||
| 196 | */ |
||
| 197 | public function AllChildren() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return all children, including those that have been deleted but are still in live. |
||
| 204 | * - Deleted children will be marked as "DeletedFromStage" |
||
| 205 | * - Added children will be marked as "AddedToStage" |
||
| 206 | * - Modified children will be marked as "ModifiedOnStage" |
||
| 207 | * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up. |
||
| 208 | * |
||
| 209 | * @return ArrayList |
||
| 210 | */ |
||
| 211 | public function AllChildrenIncludingDeleted() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return all the children that this page had, including pages that were deleted from both stage & live. |
||
| 232 | * |
||
| 233 | * @return DataList |
||
| 234 | * @throws Exception |
||
| 235 | */ |
||
| 236 | public function AllHistoricalChildren() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Return the number of children that this page ever had, including pages that were deleted. |
||
| 253 | * |
||
| 254 | * @return int |
||
| 255 | */ |
||
| 256 | public function numHistoricalChildren() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return the number of direct children. By default, values are cached after the first invocation. Can be |
||
| 263 | * augumented by {@link augmentNumChildrenCountQuery()}. |
||
| 264 | * |
||
| 265 | * @param bool $cache Whether to retrieve values from cache |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | public function numChildren($cache = true) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function showingCMSTree() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return children in the stage site. |
||
| 302 | * |
||
| 303 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
||
| 304 | * extension is applied to {@link SiteTree}. |
||
| 305 | * @return DataList |
||
| 306 | */ |
||
| 307 | public function stageChildren($showAll = false) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Return children in the live site, if it exists. |
||
| 329 | * |
||
| 330 | * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
||
| 331 | * applicable when extension is applied to {@link SiteTree}. |
||
| 332 | * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
||
| 333 | * @return DataList |
||
| 334 | * @throws Exception |
||
| 335 | */ |
||
| 336 | public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing |
||
| 366 | * is returned. |
||
| 367 | * |
||
| 368 | * @param string $filter |
||
| 369 | * @return DataObject |
||
| 370 | */ |
||
| 371 | public function getParent($filter = null) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Return all the parents of this class in a set ordered from the closest to furtherest parent. |
||
| 386 | * |
||
| 387 | * @param bool $includeSelf |
||
| 388 | * @return ArrayList |
||
| 389 | */ |
||
| 390 | public function getAncestors($includeSelf = false) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute. |
||
| 407 | * |
||
| 408 | * @param string $separator |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getBreadcrumbs($separator = ' » ') |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Flush all Hierarchy caches: |
||
| 425 | * - Children (instance) |
||
| 426 | * - NumChildren (instance) |
||
| 427 | * - Marked (global) |
||
| 428 | * - Expanded (global) |
||
| 429 | * - TreeOpened (global) |
||
| 430 | */ |
||
| 431 | public function flushCache() |
||
| 436 | } |
||
| 437 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.