Complex classes like TreeDropdownField 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 TreeDropdownField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class TreeDropdownField extends FormField { |
||
| 44 | |||
| 45 | private static $url_handlers = array( |
||
| 46 | '$Action!/$ID' => '$Action' |
||
| 47 | ); |
||
| 48 | |||
| 49 | private static $allowed_actions = array( |
||
| 50 | 'tree' |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @config |
||
| 55 | * @var int |
||
| 56 | * @see {@link Hierarchy::$node_threshold_total}. |
||
| 57 | */ |
||
| 58 | private static $node_threshold_total = 30; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ignore |
||
| 62 | */ |
||
| 63 | protected $sourceObject, $keyField, $labelField, $filterCallback, |
||
|
|
|||
| 64 | $disableCallback, $searchCallback, $baseID = 0; |
||
| 65 | /** |
||
| 66 | * @var string default child method in Hierarchy->getChildrenAsUL |
||
| 67 | */ |
||
| 68 | protected $childrenMethod = 'AllChildrenIncludingDeleted'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string default child counting method in Hierarchy->getChildrenAsUL |
||
| 72 | */ |
||
| 73 | protected $numChildrenMethod = 'numChildren'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Used by field search to leave only the relevant entries |
||
| 77 | */ |
||
| 78 | protected $searchIds = null, $showSearch, $searchExpanded = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * CAVEAT: for search to work properly $labelField must be a database field, |
||
| 82 | * or you need to setSearchFunction. |
||
| 83 | * |
||
| 84 | * @param string $name the field name |
||
| 85 | * @param string $title the field label |
||
| 86 | * @param string|array $sourceObject The object-type to list in the tree. This could |
||
| 87 | * be one of the following: |
||
| 88 | * - A DataObject class name with the {@link Hierarchy} extension. |
||
| 89 | * - An array of key/value pairs, like a {@link DropdownField} source. In |
||
| 90 | * this case, the field will act like show a flat list of tree items, |
||
| 91 | * without any hierarchy. This is most useful in conjunction with |
||
| 92 | * {@link TreeMultiselectField}, for presenting a set of checkboxes in |
||
| 93 | * a compact view. Note, that all value strings must be XML encoded |
||
| 94 | * safely prior to being passed in. |
||
| 95 | * |
||
| 96 | * @param string $keyField to field on the source class to save as the |
||
| 97 | * field value (default ID). |
||
| 98 | * @param string $labelField the field name to show as the human-readable |
||
| 99 | * value on the tree (default Title). |
||
| 100 | * @param bool $showSearch enable the ability to search the tree by |
||
| 101 | * entering the text in the input field. |
||
| 102 | */ |
||
| 103 | public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID', |
||
| 104 | $labelField = 'TreeTitle', $showSearch = true |
||
| 105 | ) { |
||
| 106 | |||
| 107 | $this->sourceObject = $sourceObject; |
||
| 108 | $this->keyField = $keyField; |
||
| 109 | $this->labelField = $labelField; |
||
| 110 | $this->showSearch = $showSearch; |
||
| 111 | |||
| 112 | //Extra settings for Folders |
||
| 113 | if ($sourceObject == 'Folder') { |
||
| 114 | $this->childrenMethod = 'ChildFolders'; |
||
| 115 | $this->numChildrenMethod = 'numChildFolders'; |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->addExtraClass('single'); |
||
| 119 | |||
| 120 | parent::__construct($name, $title); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set the ID of the root node of the tree. This defaults to 0 - i.e. |
||
| 125 | * displays the whole tree. |
||
| 126 | * |
||
| 127 | * @param int $ID |
||
| 128 | */ |
||
| 129 | public function setTreeBaseID($ID) { |
||
| 130 | $this->baseID = (int) $ID; |
||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set a callback used to filter the values of the tree before |
||
| 136 | * displaying to the user. |
||
| 137 | * |
||
| 138 | * @param callback $callback |
||
| 139 | */ |
||
| 140 | public function setFilterFunction($callback) { |
||
| 141 | if(!is_callable($callback, true)) { |
||
| 142 | throw new InvalidArgumentException('TreeDropdownField->setFilterCallback(): not passed a valid callback'); |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->filterCallback = $callback; |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set a callback used to disable checkboxes for some items in the tree |
||
| 151 | * |
||
| 152 | * @param callback $callback |
||
| 153 | */ |
||
| 154 | public function setDisableFunction($callback) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set a callback used to search the hierarchy globally, even before |
||
| 165 | * applying the filter. |
||
| 166 | * |
||
| 167 | * @param callback $callback |
||
| 168 | */ |
||
| 169 | public function setSearchFunction($callback) { |
||
| 177 | |||
| 178 | public function getShowSearch() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param Boolean |
||
| 184 | */ |
||
| 185 | public function setShowSearch($bool) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in |
||
| 192 | * {@link Hierarchy}. The method specified determines the structure of the returned list. Use "ChildFolders" |
||
| 193 | * in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in |
||
| 194 | * the currently selected folder. setNumChildrenMethod() should be used as well for proper functioning. |
||
| 195 | * |
||
| 196 | * See {@link Hierarchy} for a complete list of possible methods. |
||
| 197 | */ |
||
| 198 | public function setChildrenMethod($method) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $method The parameter to numChildrenMethod to use when calling Hierarchy->getChildrenAsUL in |
||
| 205 | * {@link Hierarchy}. Should be used in conjunction with setChildrenMethod(). |
||
| 206 | * |
||
| 207 | */ |
||
| 208 | public function setNumChildrenMethod($method) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return HTMLText |
||
| 215 | */ |
||
| 216 | public function Field($properties = array()) { |
||
| 260 | |||
| 261 | public function extraClass() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the whole tree of a part of the tree via an AJAX request. |
||
| 267 | * |
||
| 268 | * @param SS_HTTPRequest $request |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function tree(SS_HTTPRequest $request) { |
||
| 272 | // Array sourceObject is an explicit list of values - construct a "flat tree" |
||
| 273 | if(is_array($this->sourceObject)) { |
||
| 274 | $output = "<ul class=\"tree\">\n"; |
||
| 275 | foreach($this->sourceObject as $k => $v) { |
||
| 276 | $output .= '<li id="selector-' . $this->name . '-' . $k . '"><a>' . $v . '</a>'; |
||
| 277 | } |
||
| 278 | $output .= "</ul>"; |
||
| 279 | return $output; |
||
| 280 | } |
||
| 281 | |||
| 282 | // Regular source specification |
||
| 283 | $isSubTree = false; |
||
| 284 | |||
| 285 | $this->search = $request->requestVar('search'); |
||
| 286 | $ID = (is_numeric($request->latestparam('ID'))) |
||
| 287 | ? (int)$request->latestparam('ID') |
||
| 288 | : (int)$request->requestVar('ID'); |
||
| 289 | |||
| 290 | if($ID && !$request->requestVar('forceFullTree')) { |
||
| 291 | $obj = DataObject::get_by_id($this->sourceObject, $ID); |
||
| 292 | $isSubTree = true; |
||
| 293 | if(!$obj) { |
||
| 294 | throw new Exception ( |
||
| 295 | "TreeDropdownField->tree(): the object #$ID of type $this->sourceObject could not be found" |
||
| 296 | ); |
||
| 297 | } |
||
| 298 | } else { |
||
| 299 | if($this->baseID) { |
||
| 300 | $obj = DataObject::get_by_id($this->sourceObject, $this->baseID); |
||
| 301 | } |
||
| 302 | |||
| 303 | if(!$this->baseID || !$obj) $obj = singleton($this->sourceObject); |
||
| 304 | } |
||
| 305 | |||
| 306 | // pre-process the tree - search needs to operate globally, not locally as marking filter does |
||
| 307 | if ( $this->search != "" ) |
||
| 308 | $this->populateIDs(); |
||
| 309 | |||
| 310 | if ($this->filterCallback || $this->search != "" ) |
||
| 311 | $obj->setMarkingFilterFunction(array($this, "filterMarking")); |
||
| 312 | |||
| 313 | $obj->markPartialTree($nodeCountThreshold = 30, $context = null, |
||
| 314 | $this->childrenMethod, $this->numChildrenMethod); |
||
| 315 | |||
| 316 | // allow to pass values to be selected within the ajax request |
||
| 317 | if( isset($_REQUEST['forceValue']) || $this->value ) { |
||
| 318 | $forceValue = ( isset($_REQUEST['forceValue']) ? $_REQUEST['forceValue'] : $this->value); |
||
| 319 | if(($values = preg_split('/,\s*/', $forceValue)) && count($values)) foreach($values as $value) { |
||
| 320 | if(!$value || $value == 'unchanged') continue; |
||
| 321 | |||
| 322 | $obj->markToExpose($this->objectForKey($value)); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | $self = $this; |
||
| 327 | $titleFn = function(&$child) use(&$self) { |
||
| 328 | $keyField = $self->keyField; |
||
| 329 | $labelField = $self->labelField; |
||
| 330 | return sprintf( |
||
| 331 | '<li id="selector-%s-%s" data-id="%s" class="class-%s %s %s"><a rel="%d">%s</a>', |
||
| 332 | Convert::raw2xml($self->getName()), |
||
| 333 | Convert::raw2xml($child->$keyField), |
||
| 334 | Convert::raw2xml($child->$keyField), |
||
| 335 | Convert::raw2xml($child->class), |
||
| 336 | Convert::raw2xml($child->markingClasses($self->numChildrenMethod)), |
||
| 337 | ($self->nodeIsDisabled($child)) ? 'disabled' : '', |
||
| 338 | (int)$child->ID, |
||
| 339 | $child->obj($labelField)->forTemplate() |
||
| 340 | ); |
||
| 341 | }; |
||
| 342 | |||
| 343 | // Limit the amount of nodes shown for performance reasons. |
||
| 344 | // Skip the check if we're filtering the tree, since its not clear how many children will |
||
| 345 | // match the filter criteria until they're queried (and matched up with previously marked nodes). |
||
| 346 | $nodeThresholdLeaf = Config::inst()->get('Hierarchy', 'node_threshold_leaf'); |
||
| 347 | if($nodeThresholdLeaf && !$this->filterCallback && !$this->search) { |
||
| 348 | $className = $this->sourceObject; |
||
| 349 | $nodeCountCallback = function($parent, $numChildren) use($className, $nodeThresholdLeaf) { |
||
| 350 | if($className == 'SiteTree' && $parent->ID && $numChildren > $nodeThresholdLeaf) { |
||
| 351 | return sprintf( |
||
| 352 | '<ul><li><span class="item">%s</span></li></ul>', |
||
| 353 | _t('LeftAndMain.TooManyPages', 'Too many pages') |
||
| 354 | ); |
||
| 355 | } |
||
| 356 | }; |
||
| 357 | } else { |
||
| 358 | $nodeCountCallback = null; |
||
| 359 | } |
||
| 360 | |||
| 361 | if($isSubTree) { |
||
| 362 | $html = $obj->getChildrenAsUL( |
||
| 363 | "", |
||
| 364 | $titleFn, |
||
| 365 | null, |
||
| 366 | true, |
||
| 367 | $this->childrenMethod, |
||
| 368 | $this->numChildrenMethod, |
||
| 369 | true, // root call |
||
| 370 | null, |
||
| 371 | $nodeCountCallback |
||
| 372 | ); |
||
| 373 | return substr(trim($html), 4, -5); |
||
| 374 | } else { |
||
| 375 | $html = $obj->getChildrenAsUL( |
||
| 376 | 'class="tree"', |
||
| 377 | $titleFn, |
||
| 378 | null, |
||
| 379 | true, |
||
| 380 | $this->childrenMethod, |
||
| 381 | $this->numChildrenMethod, |
||
| 382 | true, // root call |
||
| 383 | null, |
||
| 384 | $nodeCountCallback |
||
| 385 | ); |
||
| 386 | return $html; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Marking public function for the tree, which combines different filters sensibly. |
||
| 392 | * If a filter function has been set, that will be called. And if search text is set, |
||
| 393 | * filter on that too. Return true if all applicable conditions are true, false otherwise. |
||
| 394 | * @param DataObject $node |
||
| 395 | * @return boolean |
||
| 396 | */ |
||
| 397 | public function filterMarking($node) { |
||
| 398 | if ($this->filterCallback && !call_user_func($this->filterCallback, $node)) return false; |
||
| 399 | if ($this->search != "") { |
||
| 400 | return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false; |
||
| 401 | } |
||
| 402 | |||
| 403 | return true; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Marking a specific node in the tree as disabled |
||
| 408 | * @param $node |
||
| 409 | * @return boolean |
||
| 410 | */ |
||
| 411 | public function nodeIsDisabled($node) { |
||
| 412 | return ($this->disableCallback && call_user_func($this->disableCallback, $node)); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $field |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | public function setLabelField($field) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getLabelField() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $field |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | public function setKeyField($field) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function getKeyField() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $field |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function setSourceObject($class) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function getSourceObject() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Populate $this->searchIds with the IDs of the pages matching the searched parameter and their parents. |
||
| 465 | * Reverse-constructs the tree starting from the leaves. Initially taken from CMSSiteTreeFilter, but modified |
||
| 466 | * with pluggable search function. |
||
| 467 | */ |
||
| 468 | protected function populateIDs() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get the object where the $keyField is equal to a certain value |
||
| 521 | * |
||
| 522 | * @param string|int $key |
||
| 523 | * @return DataObject |
||
| 524 | */ |
||
| 525 | protected function objectForKey($key) { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Changes this field to the readonly field. |
||
| 533 | */ |
||
| 534 | public function performReadonlyTransformation() { |
||
| 542 | |||
| 543 | } |
||
| 544 | |||
| 572 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.