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 |
||
| 12 | class CompositeField extends FormField { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var FieldList |
||
| 16 | */ |
||
| 17 | protected $children; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Set to true when this field is a readonly field |
||
| 21 | */ |
||
| 22 | protected $readonly; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var $columnCount int Toggle different css-rendering for multiple columns |
||
| 26 | * ("onecolumn", "twocolumns", "threecolumns"). The content is determined |
||
| 27 | * by the $children-array, so wrap all items you want to have grouped in a |
||
| 28 | * column inside a CompositeField. |
||
| 29 | * Caution: Please make sure that this variable actually matches the |
||
| 30 | * count of your $children. |
||
| 31 | */ |
||
| 32 | protected $columnCount = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var String custom HTML tag to render with, e.g. to produce a <fieldset>. |
||
| 36 | */ |
||
| 37 | protected $tag = 'div'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var String Optional description for this set of fields. |
||
| 41 | * If the {@link $tag} property is set to use a 'fieldset', this will be |
||
| 42 | * rendered as a <legend> tag, otherwise its a 'title' attribute. |
||
| 43 | */ |
||
| 44 | protected $legend; |
||
| 45 | |||
| 46 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL; |
||
| 47 | |||
| 48 | public function __construct($children = null) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Merge child field data into this form |
||
| 65 | */ |
||
| 66 | public function getSchemaDataDefaults() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns all the sub-fields, suitable for <% loop FieldList %> |
||
| 82 | * |
||
| 83 | * @return FieldList |
||
| 84 | */ |
||
| 85 | public function FieldList() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Accessor method for $this->children |
||
| 91 | * |
||
| 92 | * @return FieldList |
||
| 93 | */ |
||
| 94 | public function getChildren() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param FieldList $children |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function setChildren($children) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $tag |
||
| 109 | * @return $this |
||
| 110 | */ |
||
| 111 | public function setTag($tag) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getTag() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $legend |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function setLegend($legend) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getLegend() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @deprecated |
||
| 142 | */ |
||
| 143 | public function extraClasses() { |
||
| 147 | |||
| 148 | public function extraClass() { |
||
| 154 | |||
| 155 | public function getAttributes() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add all of the non-composite fields contained within this field to the |
||
| 169 | * list. |
||
| 170 | * |
||
| 171 | * Sequentialisation is used when connecting the form to its data source |
||
| 172 | * |
||
| 173 | * @param array $list |
||
| 174 | * @param bool $saveableOnly |
||
| 175 | */ |
||
| 176 | public function collateDataFields(&$list, $saveableOnly = false) { |
||
| 203 | |||
| 204 | public function setForm($form) { |
||
| 214 | |||
| 215 | public function setColumnCount($columnCount) { |
||
| 219 | |||
| 220 | public function getColumnCount() { |
||
| 223 | |||
| 224 | public function isComposite() { |
||
| 227 | |||
| 228 | public function hasData() { |
||
| 231 | |||
| 232 | public function fieldByName($name) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Add a new child field to the end of the set. |
||
| 238 | * |
||
| 239 | * @param FormField |
||
| 240 | */ |
||
| 241 | public function push(FormField $field) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Add a new child field to the beginning of the set. |
||
| 247 | * |
||
| 248 | * @param FormField |
||
| 249 | */ |
||
| 250 | public function unshift(FormField $field) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @uses FieldList->insertBefore() |
||
| 256 | * |
||
| 257 | * @param string $insertBefore |
||
| 258 | * @param FormField $field |
||
| 259 | * @return false|FormField |
||
| 260 | */ |
||
| 261 | public function insertBefore($insertBefore, $field) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @uses FieldList->insertAfter() |
||
| 267 | * @param string $insertAfter |
||
| 268 | * @param FormField $field |
||
| 269 | * @return false|FormField |
||
| 270 | */ |
||
| 271 | public function insertAfter($insertAfter, $field) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Remove a field from this CompositeField by Name. |
||
| 277 | * The field could also be inside a CompositeField. |
||
| 278 | * |
||
| 279 | * @param string $fieldName The name of the field |
||
| 280 | * @param boolean $dataFieldOnly If this is true, then a field will only |
||
| 281 | * be removed if it's a data field. Dataless fields, such as tabs, will |
||
| 282 | * be left as-is. |
||
| 283 | */ |
||
| 284 | public function removeByName($fieldName, $dataFieldOnly = false) { |
||
| 287 | |||
| 288 | public function replaceField($fieldName, $newField) { |
||
| 291 | |||
| 292 | public function rootFieldList() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return a readonly version of this field. Keeps the composition but returns readonly |
||
| 299 | * versions of all the child {@link FormField} objects. |
||
| 300 | * |
||
| 301 | * @return CompositeField |
||
| 302 | */ |
||
| 303 | public function performReadonlyTransformation() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return a disabled version of this field. Keeps the composition but returns disabled |
||
| 322 | * versions of all the child {@link FormField} objects. |
||
| 323 | * |
||
| 324 | * @return CompositeField |
||
| 325 | */ |
||
| 326 | public function performDisabledTransformation() { |
||
| 345 | |||
| 346 | public function IsReadonly() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Find the numerical position of a field within |
||
| 352 | * the children collection. Doesn't work recursively. |
||
| 353 | * |
||
| 354 | * @param string|FormField |
||
| 355 | * @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't |
||
| 356 | * be found. |
||
| 357 | */ |
||
| 358 | public function fieldPosition($field) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Transform the named field into a readonly feld. |
||
| 380 | * |
||
| 381 | * @param string|FormField |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | public function makeFieldReadonly($field) { |
||
| 403 | |||
| 404 | public function debug() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Validate this field |
||
| 415 | * |
||
| 416 | * @param Validator $validator |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function validate($validator) { |
||
| 427 | |||
| 428 | } |
||
| 429 |