Complex classes like GridField 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 GridField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class GridField extends FormField |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private static $allowed_actions = array( |
||
| 49 | 'index', |
||
| 50 | 'gridFieldAlterAction', |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Data source. |
||
| 55 | * |
||
| 56 | * @var SS_List |
||
| 57 | */ |
||
| 58 | protected $list = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Class name of the DataObject that the GridField will display. |
||
| 62 | * |
||
| 63 | * Defaults to the value of $this->list->dataClass. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $modelClassName = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Current state of the GridField. |
||
| 71 | * |
||
| 72 | * @var GridState |
||
| 73 | */ |
||
| 74 | protected $state = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var GridFieldConfig |
||
| 78 | */ |
||
| 79 | protected $config = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Components list. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $components = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Internal dispatcher for column handlers. |
||
| 90 | * |
||
| 91 | * Keys are column names and values are GridField_ColumnProvider objects. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $columnDispatch = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Map of callbacks for custom data fields. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $customDataFields = array(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $name = ''; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $name |
||
| 111 | * @param string $title |
||
| 112 | * @param SS_List $dataList |
||
| 113 | * @param GridFieldConfig $config |
||
| 114 | */ |
||
| 115 | public function __construct($name, $title = null, SS_List $dataList = null, GridFieldConfig $config = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param HTTPRequest $request |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function index($request) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the modelClass (data object) that this field will get it column headers from. |
||
| 154 | * |
||
| 155 | * If no $displayFields has been set, the display fields will be $summary_fields. |
||
| 156 | * |
||
| 157 | * @see GridFieldDataColumns::getDisplayFields() |
||
| 158 | * |
||
| 159 | * @param string $modelClassName |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function setModelClass($modelClassName) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns a data class that is a DataObject type that this GridField should look like. |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | * |
||
| 175 | * @throws LogicException |
||
| 176 | */ |
||
| 177 | public function getModelClass() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return GridFieldConfig |
||
| 200 | */ |
||
| 201 | public function getConfig() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param GridFieldConfig $config |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setConfig(GridFieldConfig $config) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return ArrayList |
||
| 220 | */ |
||
| 221 | public function getComponents() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Cast an arbitrary value with the help of a $castingDefinition. |
||
| 228 | * |
||
| 229 | * @todo refactor this into GridFieldComponent |
||
| 230 | * |
||
| 231 | * @param mixed $value |
||
| 232 | * @param string|array $castingDefinition |
||
| 233 | * |
||
| 234 | * @return mixed |
||
| 235 | */ |
||
| 236 | public function getCastedValue($value, $castingDefinition) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the data source. |
||
| 262 | * |
||
| 263 | * @param SS_List $list |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function setList(SS_List $list) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the data source. |
||
| 276 | * |
||
| 277 | * @return SS_List |
||
| 278 | */ |
||
| 279 | public function getList() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the data source after applying every {@link GridField_DataManipulator} to it. |
||
| 286 | * |
||
| 287 | * @return SS_List |
||
| 288 | */ |
||
| 289 | public function getManipulatedList() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the current GridState_Data or the GridState. |
||
| 304 | * |
||
| 305 | * @param bool $getData |
||
| 306 | * |
||
| 307 | * @return GridState_Data|GridState |
||
| 308 | */ |
||
| 309 | public function getState($getData = true) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the whole gridfield rendered with all the attached components. |
||
| 320 | * |
||
| 321 | * @param array $properties |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function FieldHolder($properties = array()) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param int $total |
||
| 543 | * @param int $index |
||
| 544 | * @param DataObject $record |
||
| 545 | * @param array $attributes |
||
| 546 | * @param string $content |
||
| 547 | * |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | protected function newCell($total, $index, $record, $attributes, $content) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param int $total |
||
| 561 | * @param int $index |
||
| 562 | * @param DataObject $record |
||
| 563 | * @param array $attributes |
||
| 564 | * @param string $content |
||
| 565 | * |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | protected function newRow($total, $index, $record, $attributes, $content) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param int $total |
||
| 579 | * @param int $index |
||
| 580 | * @param DataObject $record |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | protected function getRowAttributes($total, $index, $record) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param int $total |
||
| 597 | * @param int $index |
||
| 598 | * @param DataObject $record |
||
| 599 | * |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | protected function newRowClasses($total, $index, $record) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param array $properties |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | public function Field($properties = array()) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * {@inheritdoc} |
||
| 637 | */ |
||
| 638 | public function getAttributes() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Get the columns of this GridField, they are provided by attached GridField_ColumnProvider. |
||
| 650 | * |
||
| 651 | * @return array |
||
| 652 | */ |
||
| 653 | public function getColumns() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the value from a column. |
||
| 668 | * |
||
| 669 | * @param DataObject $record |
||
| 670 | * @param string $column |
||
| 671 | * |
||
| 672 | * @return string |
||
| 673 | * |
||
| 674 | * @throws InvalidArgumentException |
||
| 675 | */ |
||
| 676 | public function getColumnContent($record, $column) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Add additional calculated data fields to be used on this GridField |
||
| 703 | * |
||
| 704 | * @param array $fields a map of fieldname to callback. The callback will |
||
| 705 | * be passed the record as an argument. |
||
| 706 | */ |
||
| 707 | public function addDataFields($fields) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Get the value of a named field on the given record. |
||
| 718 | * |
||
| 719 | * Use of this method ensures that any special rules around the data for this gridfield are |
||
| 720 | * followed. |
||
| 721 | * |
||
| 722 | * @param DataObject $record |
||
| 723 | * @param string $fieldName |
||
| 724 | * |
||
| 725 | * @return mixed |
||
| 726 | */ |
||
| 727 | public function getDataFieldValue($record, $fieldName) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Get extra columns attributes used as HTML attributes. |
||
| 748 | * |
||
| 749 | * @param DataObject $record |
||
| 750 | * @param string $column |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | * |
||
| 754 | * @throws LogicException |
||
| 755 | * @throws InvalidArgumentException |
||
| 756 | */ |
||
| 757 | public function getColumnAttributes($record, $column) |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Get metadata for a column. |
||
| 794 | * |
||
| 795 | * @example "array('Title'=>'Email address')" |
||
| 796 | * |
||
| 797 | * @param string $column |
||
| 798 | * |
||
| 799 | * @return array |
||
| 800 | * |
||
| 801 | * @throws LogicException |
||
| 802 | * @throws InvalidArgumentException |
||
| 803 | */ |
||
| 804 | public function getColumnMetadata($column) |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Return how many columns the grid will have. |
||
| 841 | * |
||
| 842 | * @return int |
||
| 843 | */ |
||
| 844 | public function getColumnCount() |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Build an columnDispatch that maps a GridField_ColumnProvider to a column for reference later. |
||
| 855 | */ |
||
| 856 | protected function buildColumnDispatch() |
||
| 870 | |||
| 871 | /** |
||
| 872 | * This is the action that gets executed when a GridField_AlterAction gets clicked. |
||
| 873 | * |
||
| 874 | * @param array $data |
||
| 875 | * @param Form $form |
||
| 876 | * @param HTTPRequest $request |
||
| 877 | * |
||
| 878 | * @return string |
||
| 879 | */ |
||
| 880 | public function gridFieldAlterAction($data, $form, HTTPRequest $request) |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Pass an action on the first GridField_ActionProvider that matches the $actionName. |
||
| 939 | * |
||
| 940 | * @param string $actionName |
||
| 941 | * @param mixed $arguments |
||
| 942 | * @param array $data |
||
| 943 | * |
||
| 944 | * @return mixed |
||
| 945 | * |
||
| 946 | * @throws InvalidArgumentException |
||
| 947 | */ |
||
| 948 | public function handleAlterAction($actionName, $arguments, $data) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Custom request handler that will check component handlers before proceeding to the default |
||
| 970 | * implementation. |
||
| 971 | * |
||
| 972 | * @todo copy less code from RequestHandler. |
||
| 973 | * |
||
| 974 | * @param HTTPRequest $request |
||
| 975 | * @param DataModel $model |
||
| 976 | * |
||
| 977 | * @return array|RequestHandler|HTTPResponse|string|void |
||
| 978 | * |
||
| 979 | * @throws HTTPResponse_Exception |
||
| 980 | */ |
||
| 981 | public function handleRequest(HTTPRequest $request, DataModel $model) |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * {@inheritdoc} |
||
| 1070 | */ |
||
| 1071 | public function saveInto(DataObjectInterface $record) |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * @param array $content |
||
| 1082 | * |
||
| 1083 | * @return string |
||
| 1084 | */ |
||
| 1085 | protected function getOptionalTableHeader(array $content) |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * @param array $content |
||
| 1100 | * |
||
| 1101 | * @return string |
||
| 1102 | */ |
||
| 1103 | protected function getOptionalTableBody(array $content) |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @param $content |
||
| 1118 | * |
||
| 1119 | * @return string |
||
| 1120 | */ |
||
| 1121 | protected function getOptionalTableFooter($content) |
||
| 1133 | } |
||
| 1134 |
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.