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 | /** |
||
| 18 | * @var FieldList |
||
| 19 | */ |
||
| 20 | protected $children; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Set to true when this field is a readonly field |
||
| 24 | */ |
||
| 25 | protected $readonly; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var $columnCount int Toggle different css-rendering for multiple columns |
||
| 29 | * ("onecolumn", "twocolumns", "threecolumns"). The content is determined |
||
| 30 | * by the $children-array, so wrap all items you want to have grouped in a |
||
| 31 | * column inside a CompositeField. |
||
| 32 | * Caution: Please make sure that this variable actually matches the |
||
| 33 | * count of your $children. |
||
| 34 | */ |
||
| 35 | protected $columnCount = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var String custom HTML tag to render with, e.g. to produce a <fieldset>. |
||
| 39 | */ |
||
| 40 | protected $tag = 'div'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var String Optional description for this set of fields. |
||
| 44 | * If the {@link $tag} property is set to use a 'fieldset', this will be |
||
| 45 | * rendered as a <legend> tag, otherwise its a 'title' attribute. |
||
| 46 | */ |
||
| 47 | protected $legend; |
||
| 48 | |||
| 49 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL; |
||
| 50 | |||
| 51 | protected $schemaComponent = 'CompositeField'; |
||
| 52 | |||
| 53 | public function __construct($children = null) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Merge child field data into this form |
||
| 71 | */ |
||
| 72 | public function getSchemaDataDefaults() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns all the sub-fields, suitable for <% loop FieldList %> |
||
| 100 | * |
||
| 101 | * @return FieldList |
||
| 102 | */ |
||
| 103 | public function FieldList() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Accessor method for $this->children |
||
| 110 | * |
||
| 111 | * @return FieldList |
||
| 112 | */ |
||
| 113 | public function getChildren() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns the name (ID) for the element. |
||
| 120 | * If the CompositeField doesn't have a name, but we still want the ID/name to be set. |
||
| 121 | * This code generates the ID from the nested children. |
||
| 122 | * |
||
| 123 | * @todo this is temporary, and should be removed when FormTemplateHelper is updated to handle ID for CompositeFields with no name |
||
| 124 | * |
||
| 125 | * @return String $name |
||
| 126 | */ |
||
| 127 | public function getName() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param FieldList $children |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function setChildren($children) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $tag |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function setTag($tag) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getTag() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $legend |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function setLegend($legend) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getLegend() |
||
| 196 | |||
| 197 | public function extraClass() |
||
| 207 | |||
| 208 | public function getAttributes() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add all of the non-composite fields contained within this field to the |
||
| 223 | * list. |
||
| 224 | * |
||
| 225 | * Sequentialisation is used when connecting the form to its data source |
||
| 226 | * |
||
| 227 | * @param array $list |
||
| 228 | * @param bool $saveableOnly |
||
| 229 | */ |
||
| 230 | public function collateDataFields(&$list, $saveableOnly = false) |
||
| 258 | |||
| 259 | public function setForm($form) |
||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | public function setDisabled($disabled) |
||
| 281 | |||
| 282 | public function setReadonly($readonly) |
||
| 290 | |||
| 291 | public function setColumnCount($columnCount) |
||
| 296 | |||
| 297 | public function getColumnCount() |
||
| 301 | |||
| 302 | public function isComposite() |
||
| 306 | |||
| 307 | public function hasData() |
||
| 311 | |||
| 312 | public function fieldByName($name) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Add a new child field to the end of the set. |
||
| 319 | * |
||
| 320 | * @param FormField |
||
| 321 | */ |
||
| 322 | public function push(FormField $field) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Add a new child field to the beginning of the set. |
||
| 329 | * |
||
| 330 | * @param FormField |
||
| 331 | */ |
||
| 332 | public function unshift(FormField $field) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @uses FieldList->insertBefore() |
||
| 339 | * |
||
| 340 | * @param string $insertBefore |
||
| 341 | * @param FormField $field |
||
| 342 | * @return false|FormField |
||
| 343 | */ |
||
| 344 | public function insertBefore($insertBefore, $field) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @uses FieldList->insertAfter() |
||
| 351 | * @param string $insertAfter |
||
| 352 | * @param FormField $field |
||
| 353 | * @return false|FormField |
||
| 354 | */ |
||
| 355 | public function insertAfter($insertAfter, $field) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Remove a field from this CompositeField by Name. |
||
| 362 | * The field could also be inside a CompositeField. |
||
| 363 | * |
||
| 364 | * @param string $fieldName The name of the field |
||
| 365 | * @param boolean $dataFieldOnly If this is true, then a field will only |
||
| 366 | * be removed if it's a data field. Dataless fields, such as tabs, will |
||
| 367 | * be left as-is. |
||
| 368 | */ |
||
| 369 | public function removeByName($fieldName, $dataFieldOnly = false) |
||
| 373 | |||
| 374 | public function replaceField($fieldName, $newField) |
||
| 378 | |||
| 379 | public function rootFieldList() |
||
| 387 | |||
| 388 | public function __clone() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return a readonly version of this field. Keeps the composition but returns readonly |
||
| 396 | * versions of all the child {@link FormField} objects. |
||
| 397 | * |
||
| 398 | * @return CompositeField |
||
| 399 | */ |
||
| 400 | public function performReadonlyTransformation() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Return a disabled version of this field. Keeps the composition but returns disabled |
||
| 422 | * versions of all the child {@link FormField} objects. |
||
| 423 | * |
||
| 424 | * @return CompositeField |
||
| 425 | */ |
||
| 426 | public function performDisabledTransformation() |
||
| 448 | |||
| 449 | public function IsReadonly() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Find the numerical position of a field within |
||
| 456 | * the children collection. Doesn't work recursively. |
||
| 457 | * |
||
| 458 | * @param string|FormField |
||
| 459 | * @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't |
||
| 460 | * be found. |
||
| 461 | */ |
||
| 462 | public function fieldPosition($field) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Transform the named field into a readonly feld. |
||
| 485 | * |
||
| 486 | * @param string|FormField |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | public function makeFieldReadonly($field) |
||
| 509 | |||
| 510 | public function debug() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Validate this field |
||
| 522 | * |
||
| 523 | * @param Validator $validator |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | public function validate($validator) |
||
| 535 | } |
||
| 536 |