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 |
||
| 56 | class TreeDropdownField extends FormField |
||
| 57 | { |
||
| 58 | protected $schemaDataType = self::SCHEMA_DATA_TYPE_SINGLESELECT; |
||
| 59 | |||
| 60 | protected $schemaComponent = 'TreeDropdownField'; |
||
| 61 | |||
| 62 | private static $url_handlers = array( |
||
| 63 | '$Action!/$ID' => '$Action' |
||
| 64 | ); |
||
| 65 | |||
| 66 | private static $allowed_actions = array( |
||
| 67 | 'tree' |
||
| 68 | ); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Class name for underlying object |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $sourceObject = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Name of key field on underlying object |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $keyField = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Name of lavel field on underlying object |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $labelField = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Callback for filtering records |
||
| 93 | * |
||
| 94 | * @var callable |
||
| 95 | */ |
||
| 96 | protected $filterCallback = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Callback for marking record as disabled |
||
| 100 | * |
||
| 101 | * @var callable |
||
| 102 | */ |
||
| 103 | protected $disableCallback = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Callback for searching records. This callback takes the following arguments: |
||
| 107 | * - sourceObject Object class to search |
||
| 108 | * - labelField Label field |
||
| 109 | * - search Search text |
||
| 110 | * |
||
| 111 | * @var callable |
||
| 112 | */ |
||
| 113 | protected $searchCallback = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Filter for base record |
||
| 117 | * |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | protected $baseID = 0; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Default child method in Hierarchy->getChildrenAsUL |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | protected $childrenMethod = 'AllChildrenIncludingDeleted'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Default child counting method in Hierarchy->getChildrenAsUL |
||
| 131 | * |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | protected $numChildrenMethod = 'numChildren'; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Current string value for search text to filter on |
||
| 138 | * |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | protected $search = null; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * List of ids in current search result (keys are ids, values are true) |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $searchIds = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Determine if search should be shown |
||
| 152 | * |
||
| 153 | * @var bool |
||
| 154 | */ |
||
| 155 | protected $showSearch = false; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * List of ids which have their search expanded (keys are ids, values are true) |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | protected $searchExpanded = []; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * CAVEAT: for search to work properly $labelField must be a database field, |
||
| 166 | * or you need to setSearchFunction. |
||
| 167 | * |
||
| 168 | * @param string $name the field name |
||
| 169 | * @param string $title the field label |
||
| 170 | * @param string $sourceObject A DataObject class name with the {@link Hierarchy} extension. |
||
| 171 | * @param string $keyField to field on the source class to save as the |
||
| 172 | * field value (default ID). |
||
| 173 | * @param string $labelField the field name to show as the human-readable |
||
| 174 | * value on the tree (default Title). |
||
| 175 | * @param bool $showSearch enable the ability to search the tree by |
||
| 176 | * entering the text in the input field. |
||
| 177 | */ |
||
| 178 | public function __construct( |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set the ID of the root node of the tree. This defaults to 0 - i.e. |
||
| 210 | * displays the whole tree. |
||
| 211 | * |
||
| 212 | * @param int $ID |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function setTreeBaseID($ID) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set a callback used to filter the values of the tree before |
||
| 223 | * displaying to the user. |
||
| 224 | * |
||
| 225 | * @param callback $callback |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function setFilterFunction($callback) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set a callback used to disable checkboxes for some items in the tree |
||
| 240 | * |
||
| 241 | * @param callback $callback |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function setDisableFunction($callback) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set a callback used to search the hierarchy globally, even before |
||
| 256 | * applying the filter. |
||
| 257 | * |
||
| 258 | * @param callback $callback |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function setSearchFunction($callback) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Check if search is shown |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function getShowSearch() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param bool $bool |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function setShowSearch($bool) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in |
||
| 293 | * {@link Hierarchy}. The method specified determines the structure of the returned list. Use "ChildFolders" |
||
| 294 | * in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in |
||
| 295 | * the currently selected folder. setNumChildrenMethod() should be used as well for proper functioning. |
||
| 296 | * |
||
| 297 | * See {@link Hierarchy} for a complete list of possible methods. |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function setChildrenMethod($method) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $method The parameter to numChildrenMethod to use when calling Hierarchy->getChildrenAsUL in |
||
| 308 | * {@link Hierarchy}. Should be used in conjunction with setChildrenMethod(). |
||
| 309 | * |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | public function setNumChildrenMethod($method) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param array $properties |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function Field($properties = array()) |
||
| 350 | |||
| 351 | public function extraClass() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the whole tree of a part of the tree via an AJAX request. |
||
| 358 | * |
||
| 359 | * @param HTTPRequest $request |
||
| 360 | * @return HTTPResponse |
||
| 361 | * @throws Exception |
||
| 362 | */ |
||
| 363 | public function tree(HTTPRequest $request) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Marking public function for the tree, which combines different filters sensibly. |
||
| 459 | * If a filter function has been set, that will be called. And if search text is set, |
||
| 460 | * filter on that too. Return true if all applicable conditions are true, false otherwise. |
||
| 461 | * |
||
| 462 | * @param DataObject $node |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | public function filterMarking($node) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Marking a specific node in the tree as disabled |
||
| 480 | * @param $node |
||
| 481 | * @return boolean |
||
| 482 | */ |
||
| 483 | public function nodeIsDisabled($node) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param string $field |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | public function setLabelField($field) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return String |
||
| 500 | */ |
||
| 501 | public function getLabelField() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $field |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | public function setKeyField($field) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return String |
||
| 518 | */ |
||
| 519 | public function getKeyField() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param string $class |
||
| 526 | * @return $this |
||
| 527 | */ |
||
| 528 | public function setSourceObject($class) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @return String |
||
| 536 | */ |
||
| 537 | public function getSourceObject() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Populate $this->searchIds with the IDs of the pages matching the searched parameter and their parents. |
||
| 544 | * Reverse-constructs the tree starting from the leaves. Initially taken from CMSSiteTreeFilter, but modified |
||
| 545 | * with pluggable search function. |
||
| 546 | */ |
||
| 547 | protected function populateIDs() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Get the object where the $keyField is equal to a certain value |
||
| 605 | * |
||
| 606 | * @param string|int $key |
||
| 607 | * @return DataObject |
||
| 608 | */ |
||
| 609 | protected function objectForKey($key) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Changes this field to the readonly field. |
||
| 618 | */ |
||
| 619 | public function performReadonlyTransformation() |
||
| 628 | |||
| 629 | public function getSchemaStateDefaults() |
||
| 647 | |||
| 648 | public function getSchemaDataDefaults() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | protected function getEmptyTitle() |
||
| 668 | } |
||
| 669 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: