Complex classes like GridFieldRecordHighlighter 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 GridFieldRecordHighlighter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class GridFieldRecordHighlighter implements GridField_ColumnProvider |
||
| 11 | { |
||
| 12 | |||
| 13 | protected $alerts; |
||
| 14 | |||
| 15 | function __construct($alerts) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Add extra fields to the column list |
||
| 22 | * |
||
| 23 | * @param GridField $gridField |
||
| 24 | * @param array - List reference of all column names. |
||
| 25 | */ |
||
| 26 | public function augmentColumns($gridField, &$columns) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * List of handled columns |
||
| 34 | * |
||
| 35 | * @param GridField $gridField |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function getColumnsHandled($gridField) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set titles for the column header |
||
| 46 | * |
||
| 47 | * @param GridField $gridField |
||
| 48 | * @param string $columnName |
||
| 49 | * @return array - Map of arbitrary metadata identifiers to their values. |
||
| 50 | */ |
||
| 51 | public function getColumnMetadata($gridField, $columnName) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return a formfield for the extra field column or an edit button for the actions column |
||
| 59 | * |
||
| 60 | * @param GridField $gridField |
||
| 61 | * @param DataObject $record - Record displayed in this row |
||
| 62 | * @param string $columnName |
||
| 63 | * @return string - HTML for the column. Return NULL to skip. |
||
| 64 | */ |
||
| 65 | public function getColumnContent($gridField, $record, $columnName) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Generate HTML attributes for each individual cells as selectors for CSS and JS |
||
| 90 | * |
||
| 91 | * @param GridField $gridField |
||
| 92 | * @param DataObject $record displayed in this row |
||
| 93 | * @param string $columnName |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public function getColumnAttributes($gridField, $record, $columnName) |
||
| 111 | |||
| 112 | function getAlerts($record) |
||
| 144 | } |
||
| 145 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.