Complex classes like CompositeField 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 CompositeField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class CompositeField extends FormField { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var FieldList |
||
| 18 | */ |
||
| 19 | protected $children; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Set to true when this field is a readonly field |
||
| 23 | */ |
||
| 24 | protected $readonly; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var $columnCount int Toggle different css-rendering for multiple columns |
||
| 28 | * ("onecolumn", "twocolumns", "threecolumns"). The content is determined |
||
| 29 | * by the $children-array, so wrap all items you want to have grouped in a |
||
| 30 | * column inside a CompositeField. |
||
| 31 | * Caution: Please make sure that this variable actually matches the |
||
| 32 | * count of your $children. |
||
| 33 | */ |
||
| 34 | protected $columnCount = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var String custom HTML tag to render with, e.g. to produce a <fieldset>. |
||
| 38 | */ |
||
| 39 | protected $tag = 'div'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var String Optional description for this set of fields. |
||
| 43 | * If the {@link $tag} property is set to use a 'fieldset', this will be |
||
| 44 | * rendered as a <legend> tag, otherwise its a 'title' attribute. |
||
| 45 | */ |
||
| 46 | protected $legend; |
||
| 47 | |||
| 48 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL; |
||
| 49 | |||
| 50 | public function __construct($children = null) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Merge child field data into this form |
||
| 67 | */ |
||
| 68 | public function getSchemaDataDefaults() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns all the sub-fields, suitable for <% loop FieldList %> |
||
| 88 | * |
||
| 89 | * @return FieldList |
||
| 90 | */ |
||
| 91 | public function FieldList() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Accessor method for $this->children |
||
| 97 | * |
||
| 98 | * @return FieldList |
||
| 99 | */ |
||
| 100 | public function getChildren() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Returns the name (ID) for the element. |
||
| 106 | * If the CompositeField doesn't have a name, but we still want the ID/name to be set. |
||
| 107 | * This code generates the ID from the nested children. |
||
| 108 | * |
||
| 109 | * @todo this is temporary, and should be removed when FormTemplateHelper is updated to handle ID for CompositeFields with no name |
||
| 110 | * |
||
| 111 | * @return String $name |
||
| 112 | */ |
||
| 113 | public function getName(){ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param FieldList $children |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function setChildren($children) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $tag |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function setTag($tag) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getTag() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $legend |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function setLegend($legend) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getLegend() { |
||
| 174 | |||
| 175 | public function extraClass() { |
||
| 182 | |||
| 183 | public function getAttributes() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Add all of the non-composite fields contained within this field to the |
||
| 197 | * list. |
||
| 198 | * |
||
| 199 | * Sequentialisation is used when connecting the form to its data source |
||
| 200 | * |
||
| 201 | * @param array $list |
||
| 202 | * @param bool $saveableOnly |
||
| 203 | */ |
||
| 204 | public function collateDataFields(&$list, $saveableOnly = false) { |
||
| 231 | |||
| 232 | public function setForm($form) { |
||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | public function setDisabled($disabled) { |
||
| 252 | |||
| 253 | public function setReadonly($readonly) |
||
| 261 | |||
| 262 | public function setColumnCount($columnCount) { |
||
| 266 | |||
| 267 | public function getColumnCount() { |
||
| 270 | |||
| 271 | public function isComposite() { |
||
| 274 | |||
| 275 | public function hasData() { |
||
| 278 | |||
| 279 | public function fieldByName($name) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Add a new child field to the end of the set. |
||
| 285 | * |
||
| 286 | * @param FormField |
||
| 287 | */ |
||
| 288 | public function push(FormField $field) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Add a new child field to the beginning of the set. |
||
| 294 | * |
||
| 295 | * @param FormField |
||
| 296 | */ |
||
| 297 | public function unshift(FormField $field) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @uses FieldList->insertBefore() |
||
| 303 | * |
||
| 304 | * @param string $insertBefore |
||
| 305 | * @param FormField $field |
||
| 306 | * @return false|FormField |
||
| 307 | */ |
||
| 308 | public function insertBefore($insertBefore, $field) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @uses FieldList->insertAfter() |
||
| 314 | * @param string $insertAfter |
||
| 315 | * @param FormField $field |
||
| 316 | * @return false|FormField |
||
| 317 | */ |
||
| 318 | public function insertAfter($insertAfter, $field) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Remove a field from this CompositeField by Name. |
||
| 324 | * The field could also be inside a CompositeField. |
||
| 325 | * |
||
| 326 | * @param string $fieldName The name of the field |
||
| 327 | * @param boolean $dataFieldOnly If this is true, then a field will only |
||
| 328 | * be removed if it's a data field. Dataless fields, such as tabs, will |
||
| 329 | * be left as-is. |
||
| 330 | */ |
||
| 331 | public function removeByName($fieldName, $dataFieldOnly = false) { |
||
| 334 | |||
| 335 | public function replaceField($fieldName, $newField) { |
||
| 338 | |||
| 339 | public function rootFieldList() { |
||
| 343 | |||
| 344 | public function __clone() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return a readonly version of this field. Keeps the composition but returns readonly |
||
| 351 | * versions of all the child {@link FormField} objects. |
||
| 352 | * |
||
| 353 | * @return CompositeField |
||
| 354 | */ |
||
| 355 | public function performReadonlyTransformation() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return a disabled version of this field. Keeps the composition but returns disabled |
||
| 374 | * versions of all the child {@link FormField} objects. |
||
| 375 | * |
||
| 376 | * @return CompositeField |
||
| 377 | */ |
||
| 378 | public function performDisabledTransformation() { |
||
| 397 | |||
| 398 | public function IsReadonly() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Find the numerical position of a field within |
||
| 404 | * the children collection. Doesn't work recursively. |
||
| 405 | * |
||
| 406 | * @param string|FormField |
||
| 407 | * @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't |
||
| 408 | * be found. |
||
| 409 | */ |
||
| 410 | public function fieldPosition($field) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Transform the named field into a readonly feld. |
||
| 432 | * |
||
| 433 | * @param string|FormField |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function makeFieldReadonly($field) { |
||
| 455 | |||
| 456 | public function debug() { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Validate this field |
||
| 467 | * |
||
| 468 | * @param Validator $validator |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function validate($validator) { |
||
| 479 | |||
| 480 | } |
||
| 481 |