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 |
||
| 45 | class TreeDropdownField extends FormField { |
||
| 46 | |||
| 47 | private static $url_handlers = array( |
||
| 48 | '$Action!/$ID' => '$Action' |
||
| 49 | ); |
||
| 50 | |||
| 51 | private static $allowed_actions = array( |
||
| 52 | 'tree' |
||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ignore |
||
| 57 | */ |
||
| 58 | protected $sourceObject, $keyField, $labelField, $filterCallback, |
||
|
|
|||
| 59 | $disableCallback, $searchCallback, $baseID = 0; |
||
| 60 | /** |
||
| 61 | * @var string default child method in Hierarchy->getChildrenAsUL |
||
| 62 | */ |
||
| 63 | protected $childrenMethod = 'AllChildrenIncludingDeleted'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string default child counting method in Hierarchy->getChildrenAsUL |
||
| 67 | */ |
||
| 68 | protected $numChildrenMethod = 'numChildren'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Used by field search to leave only the relevant entries |
||
| 72 | */ |
||
| 73 | protected $searchIds = null, $showSearch, $searchExpanded = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * CAVEAT: for search to work properly $labelField must be a database field, |
||
| 77 | * or you need to setSearchFunction. |
||
| 78 | * |
||
| 79 | * @param string $name the field name |
||
| 80 | * @param string $title the field label |
||
| 81 | * @param string|array $sourceObject The object-type to list in the tree. This could |
||
| 82 | * be one of the following: |
||
| 83 | * - A DataObject class name with the {@link Hierarchy} extension. |
||
| 84 | * - An array of key/value pairs, like a {@link DropdownField} source. In |
||
| 85 | * this case, the field will act like show a flat list of tree items, |
||
| 86 | * without any hierarchy. This is most useful in conjunction with |
||
| 87 | * {@link TreeMultiselectField}, for presenting a set of checkboxes in |
||
| 88 | * a compact view. Note, that all value strings must be XML encoded |
||
| 89 | * safely prior to being passed in. |
||
| 90 | * |
||
| 91 | * @param string $keyField to field on the source class to save as the |
||
| 92 | * field value (default ID). |
||
| 93 | * @param string $labelField the field name to show as the human-readable |
||
| 94 | * value on the tree (default Title). |
||
| 95 | * @param bool $showSearch enable the ability to search the tree by |
||
| 96 | * entering the text in the input field. |
||
| 97 | */ |
||
| 98 | public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID', |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set the ID of the root node of the tree. This defaults to 0 - i.e. |
||
| 120 | * displays the whole tree. |
||
| 121 | * |
||
| 122 | * @param int $ID |
||
| 123 | */ |
||
| 124 | public function setTreeBaseID($ID) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set a callback used to filter the values of the tree before |
||
| 131 | * displaying to the user. |
||
| 132 | * |
||
| 133 | * @param callback $callback |
||
| 134 | */ |
||
| 135 | public function setFilterFunction($callback) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set a callback used to disable checkboxes for some items in the tree |
||
| 146 | * |
||
| 147 | * @param callback $callback |
||
| 148 | */ |
||
| 149 | public function setDisableFunction($callback) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Set a callback used to search the hierarchy globally, even before |
||
| 160 | * applying the filter. |
||
| 161 | * |
||
| 162 | * @param callback $callback |
||
| 163 | */ |
||
| 164 | public function setSearchFunction($callback) { |
||
| 172 | |||
| 173 | public function getShowSearch() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param Boolean |
||
| 179 | */ |
||
| 180 | public function setShowSearch($bool) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in |
||
| 187 | * {@link Hierarchy}. The method specified determines the structure of the returned list. Use "ChildFolders" |
||
| 188 | * in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in |
||
| 189 | * the currently selected folder. setNumChildrenMethod() should be used as well for proper functioning. |
||
| 190 | * |
||
| 191 | * See {@link Hierarchy} for a complete list of possible methods. |
||
| 192 | */ |
||
| 193 | public function setChildrenMethod($method) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $method The parameter to numChildrenMethod to use when calling Hierarchy->getChildrenAsUL in |
||
| 200 | * {@link Hierarchy}. Should be used in conjunction with setChildrenMethod(). |
||
| 201 | * |
||
| 202 | */ |
||
| 203 | public function setNumChildrenMethod($method) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return HTMLText |
||
| 210 | */ |
||
| 211 | public function Field($properties = array()) { |
||
| 256 | |||
| 257 | public function extraClass() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the whole tree of a part of the tree via an AJAX request. |
||
| 263 | * |
||
| 264 | * @param SS_HTTPRequest $request |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function tree(SS_HTTPRequest $request) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Marking public function for the tree, which combines different filters sensibly. |
||
| 388 | * If a filter function has been set, that will be called. And if search text is set, |
||
| 389 | * filter on that too. Return true if all applicable conditions are true, false otherwise. |
||
| 390 | * @param $node |
||
| 391 | * @return unknown_type |
||
| 392 | */ |
||
| 393 | public function filterMarking($node) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Marking a specific node in the tree as disabled |
||
| 404 | * @param $node |
||
| 405 | * @return boolean |
||
| 406 | */ |
||
| 407 | public function nodeIsDisabled($node) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param String $field |
||
| 413 | */ |
||
| 414 | public function setLabelField($field) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return String |
||
| 421 | */ |
||
| 422 | public function getLabelField() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param String $field |
||
| 428 | */ |
||
| 429 | public function setKeyField($field) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return String |
||
| 436 | */ |
||
| 437 | public function getKeyField() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param String $field |
||
| 443 | */ |
||
| 444 | public function setSourceObject($class) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return String |
||
| 451 | */ |
||
| 452 | public function getSourceObject() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Populate $this->searchIds with the IDs of the pages matching the searched parameter and their parents. |
||
| 458 | * Reverse-constructs the tree starting from the leaves. Initially taken from CMSSiteTreeFilter, but modified |
||
| 459 | * with pluggable search function. |
||
| 460 | */ |
||
| 461 | protected function populateIDs() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get the object where the $keyField is equal to a certain value |
||
| 514 | * |
||
| 515 | * @param string|int $key |
||
| 516 | * @return DataObject |
||
| 517 | */ |
||
| 518 | protected function objectForKey($key) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Changes this field to the readonly field. |
||
| 526 | */ |
||
| 527 | public function performReadonlyTransformation() { |
||
| 535 | |||
| 536 | } |
||
| 537 | |||
| 565 |
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.