Complex classes like FieldList 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 FieldList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class FieldList extends ArrayList { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Cached flat representation of all fields in this set, |
||
| 14 | * including fields nested in {@link CompositeFields}. |
||
| 15 | * |
||
| 16 | * @uses self::collateDataFields() |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $sequentialSet; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $sequentialSaveableSet; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @todo Documentation |
||
| 28 | */ |
||
| 29 | protected $containerField; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array Ordered list of regular expressions, |
||
| 33 | * see {@link setTabPathRewrites()}. |
||
| 34 | */ |
||
| 35 | protected $tabPathRewrites = array(); |
||
| 36 | |||
| 37 | public function __construct($items = array()) { |
||
| 48 | |||
| 49 | public function __clone() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Return a sequential set of all fields that have data. This excludes wrapper composite fields |
||
| 58 | * as well as heading / help text fields. |
||
| 59 | */ |
||
| 60 | public function dataFields() { |
||
| 64 | |||
| 65 | public function saveableFields() { |
||
| 69 | |||
| 70 | protected function flushFieldsCache() { |
||
| 74 | |||
| 75 | protected function collateDataFields(&$list, $saveableOnly = false) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Add an extra field to a tab within this FieldList. |
||
| 103 | * This is most commonly used when overloading getCMSFields() |
||
| 104 | * |
||
| 105 | * @param string $tabName The name of the tab or tabset. Subtabs can be referred to as TabSet.Tab |
||
| 106 | * or TabSet.Tab.Subtab. This function will create any missing tabs. |
||
| 107 | * @param FormField $field The {@link FormField} object to add to the end of that tab. |
||
| 108 | * @param string $insertBefore The name of the field to insert before. Optional. |
||
| 109 | */ |
||
| 110 | public function addFieldToTab($tabName, $field, $insertBefore = null) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Add a number of extra fields to a tab within this FieldList. |
||
| 124 | * This is most commonly used when overloading getCMSFields() |
||
| 125 | * |
||
| 126 | * @param string $tabName The name of the tab or tabset. Subtabs can be referred to as TabSet.Tab |
||
| 127 | * or TabSet.Tab.Subtab. |
||
| 128 | * This function will create any missing tabs. |
||
| 129 | * @param array $fields An array of {@link FormField} objects. |
||
| 130 | */ |
||
| 131 | public function addFieldsToTab($tabName, $fields, $insertBefore = null) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Remove the given field from the given tab in the field. |
||
| 153 | * |
||
| 154 | * @param string $tabName The name of the tab |
||
| 155 | * @param string $fieldName The name of the field |
||
| 156 | */ |
||
| 157 | public function removeFieldFromTab($tabName, $fieldName) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Removes a number of fields from a Tab/TabSet within this FieldList. |
||
| 167 | * |
||
| 168 | * @param string $tabName The name of the Tab or TabSet field |
||
| 169 | * @param array $fields A list of fields, e.g. array('Name', 'Email') |
||
| 170 | */ |
||
| 171 | public function removeFieldsFromTab($tabName, $fields) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Remove a field or fields from this FieldList by Name. |
||
| 183 | * The field could also be inside a CompositeField. |
||
| 184 | * |
||
| 185 | * @param string|array $fieldName The name of, or an array with the field(s) or tab(s) |
||
| 186 | * @param boolean $dataFieldOnly If this is true, then a field will only |
||
| 187 | * be removed if it's a data field. Dataless fields, such as tabs, will |
||
| 188 | * be left as-is. |
||
| 189 | */ |
||
| 190 | public function removeByName($fieldName, $dataFieldOnly = false) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Replace a single field with another. Ignores dataless fields such as Tabs and TabSets |
||
| 221 | * |
||
| 222 | * @param string $fieldName The name of the field to replace |
||
| 223 | * @param FormField $newField The field object to replace with |
||
| 224 | * @return boolean TRUE field was successfully replaced |
||
| 225 | * FALSE field wasn't found, nothing changed |
||
| 226 | */ |
||
| 227 | public function replaceField($fieldName, $newField) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Rename the title of a particular field name in this set. |
||
| 245 | * |
||
| 246 | * @param string $fieldName Name of field to rename title of |
||
| 247 | * @param string $newFieldTitle New title of field |
||
| 248 | * @return boolean |
||
| 249 | */ |
||
| 250 | public function renameField($fieldName, $newFieldTitle) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | public function hasTabSet() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns the specified tab object, creating it if necessary. |
||
| 274 | * |
||
| 275 | * @todo Support recursive creation of TabSets |
||
| 276 | * |
||
| 277 | * @param string $tabName The tab to return, in the form "Tab.Subtab.Subsubtab". |
||
| 278 | * Caution: Does not recursively create TabSet instances, you need to make sure everything |
||
| 279 | * up until the last tab in the chain exists. |
||
| 280 | * @param string $title Natural language title of the tab. If {@link $tabName} is passed in dot notation, |
||
| 281 | * the title parameter will only apply to the innermost referenced tab. |
||
| 282 | * The title is only changed if the tab doesn't exist already. |
||
| 283 | * @return Tab The found or newly created Tab instance |
||
| 284 | */ |
||
| 285 | public function findOrMakeTab($tabName, $title = null) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns a named field. |
||
| 322 | * You can use dot syntax to get fields from child composite fields |
||
| 323 | * |
||
| 324 | * @todo Implement similarly to dataFieldByName() to support nested sets - or merge with dataFields() |
||
| 325 | * |
||
| 326 | * @param string $name |
||
| 327 | * @return FormField |
||
| 328 | */ |
||
| 329 | public function fieldByName($name) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns a named field in a sequential set. |
||
| 353 | * Use this if you're using nested FormFields. |
||
| 354 | * |
||
| 355 | * @param string $name The name of the field to return |
||
| 356 | * @return FormField instance |
||
| 357 | */ |
||
| 358 | public function dataFieldByName($name) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Inserts a field before a particular field in a FieldList. |
||
| 368 | * |
||
| 369 | * @param string $name Name of the field to insert before |
||
| 370 | * @param FormField $item The form field to insert |
||
| 371 | * @return FormField|false |
||
| 372 | */ |
||
| 373 | public function insertBefore($name, $item) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Inserts a field after a particular field in a FieldList. |
||
| 398 | * |
||
| 399 | * @param string $name Name of the field to insert after |
||
| 400 | * @param FormField $item The form field to insert |
||
| 401 | * @return FormField|false |
||
| 402 | */ |
||
| 403 | public function insertAfter($name, $item) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Push a single field onto the end of this FieldList instance. |
||
| 428 | * |
||
| 429 | * @param FormField $item The FormField to add |
||
| 430 | */ |
||
| 431 | public function push($item) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Push a single field onto the beginning of this FieldList instance. |
||
| 440 | * |
||
| 441 | * @param FormField $item The FormField to add |
||
| 442 | */ |
||
| 443 | public function unshift($item) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Handler method called before the FieldList is going to be manipulated. |
||
| 452 | */ |
||
| 453 | protected function onBeforeInsert($item) { |
||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * Set the Form instance for this FieldList. |
||
| 464 | * |
||
| 465 | * @param Form $form The form to set this FieldList to |
||
| 466 | */ |
||
| 467 | public function setForm($form) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Load the given data into this form. |
||
| 477 | * |
||
| 478 | * @param data An map of data to load into the FieldList |
||
| 479 | */ |
||
| 480 | public function setValues($data) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Return all <input type="hidden"> fields |
||
| 490 | * in a form - including fields nested in {@link CompositeFields}. |
||
| 491 | * Useful when doing custom field layouts. |
||
| 492 | * |
||
| 493 | * @return FieldList |
||
| 494 | */ |
||
| 495 | public function HiddenFields() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Return all fields except for the hidden fields. |
||
| 508 | * Useful when making your own simplified form layouts. |
||
| 509 | */ |
||
| 510 | public function VisibleFields() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Transform this FieldList with a given tranform method, |
||
| 522 | * e.g. $this->transform(new ReadonlyTransformation()) |
||
| 523 | * |
||
| 524 | * @return FieldList |
||
| 525 | */ |
||
| 526 | public function transform($trans) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns the root field set that this belongs to |
||
| 537 | */ |
||
| 538 | public function rootFieldList() { |
||
| 542 | |||
| 543 | public function setContainerField($field) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Transforms this FieldList instance to readonly. |
||
| 550 | * |
||
| 551 | * @return FieldList |
||
| 552 | */ |
||
| 553 | public function makeReadonly() { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Transform the named field into a readonly feld. |
||
| 559 | * |
||
| 560 | * @param string|FormField |
||
| 561 | */ |
||
| 562 | public function makeFieldReadonly($field) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Change the order of fields in this FieldList by specifying an ordered list of field names. |
||
| 570 | * This works well in conjunction with SilverStripe's scaffolding functions: take the scaffold, and |
||
| 571 | * shuffle the fields around to the order that you want. |
||
| 572 | * |
||
| 573 | * Please note that any tabs or other dataless fields will be clobbered by this operation. |
||
| 574 | * |
||
| 575 | * @param array $fieldNames Field names can be given as an array, or just as a list of arguments. |
||
| 576 | */ |
||
| 577 | public function changeFieldOrder($fieldNames) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Find the numerical position of a field within |
||
| 607 | * the children collection. Doesn't work recursively. |
||
| 608 | * |
||
| 609 | * @param string|FormField |
||
| 610 | * @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't |
||
| 611 | * be found. |
||
| 612 | */ |
||
| 613 | public function fieldPosition($field) { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Ordered list of regular expressions |
||
| 627 | * matching a tab path, to their rewrite rule (through preg_replace()). |
||
| 628 | * Mainly used for backwards compatibility. |
||
| 629 | * Ensure that more specific rules are placed earlier in the list, |
||
| 630 | * and that tabs with children are accounted for in the rule sets. |
||
| 631 | * |
||
| 632 | * Example: |
||
| 633 | * $fields->setTabPathRewriting(array( |
||
| 634 | * // Rewrite specific innermost tab |
||
| 635 | * '/^Root\.Content\.Main$/' => 'Root.Content', |
||
| 636 | * // Rewrite all innermost tabs |
||
| 637 | * '/^Root\.Content\.([^.]+)$/' => 'Root.\\1', |
||
| 638 | * )); |
||
| 639 | * |
||
| 640 | * @param array $rewrites |
||
| 641 | */ |
||
| 642 | public function setTabPathRewrites($rewrites) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @return array |
||
| 648 | */ |
||
| 649 | public function getTabPathRewrites() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Support function for backwards compatibility purposes. |
||
| 655 | * Caution: Volatile API, might be removed in 3.1 or later. |
||
| 656 | * |
||
| 657 | * @param String $tabname Path to a tab, e.g. "Root.Content.Main" |
||
| 658 | * @return String Rewritten path, based on {@link tabPathRewrites} |
||
| 659 | */ |
||
| 660 | protected function rewriteTabPath($name) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Default template rendering of a FieldList will concatenate all FieldHolder values. |
||
| 682 | */ |
||
| 683 | public function forTemplate() { |
||
| 690 | } |
||
| 691 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.