Complex classes like GridFieldAddExistingAutocompleter 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 GridFieldAddExistingAutocompleter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class GridFieldAddExistingAutocompleter implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator, GridField_URLHandler |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The HTML fragment to write this component into |
||
| 40 | */ |
||
| 41 | protected $targetFragment; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var SS_List |
||
| 45 | */ |
||
| 46 | protected $searchList; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Define column names which should be included in the search. |
||
| 50 | * By default, they're searched with a {@link StartsWithFilter}. |
||
| 51 | * To define custom filters, use the same notation as {@link DataList->filter()}, |
||
| 52 | * e.g. "Name:EndsWith". |
||
| 53 | * |
||
| 54 | * If multiple fields are provided, the filtering is performed non-exclusive. |
||
| 55 | * If no fields are provided, tries to auto-detect fields from |
||
| 56 | * {@link DataObject->searchableFields()}. |
||
| 57 | * |
||
| 58 | * The fields support "dot-notation" for relationships, e.g. |
||
| 59 | * a entry called "Team.Name" will search through the names of |
||
| 60 | * a "Team" relationship. |
||
| 61 | * |
||
| 62 | * @example |
||
| 63 | * array( |
||
| 64 | * 'Name', |
||
| 65 | * 'Email:StartsWith', |
||
| 66 | * 'Team.Name' |
||
| 67 | * ) |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $searchFields = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string SSViewer template to render the results presentation |
||
| 75 | */ |
||
| 76 | protected $resultsFormat = '$Title'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string Text shown on the search field, instructing what to search for. |
||
| 80 | */ |
||
| 81 | protected $placeholderText; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $resultsLimit = 20; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @param string $targetFragment |
||
| 91 | * @param array $searchFields Which fields on the object in the list should be searched |
||
| 92 | */ |
||
| 93 | public function __construct($targetFragment = 'before', $searchFields = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * |
||
| 101 | * @param GridField $gridField |
||
| 102 | * @return string[] - HTML |
||
| 103 | */ |
||
| 104 | public function getHTMLFragments($gridField) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * |
||
| 157 | * @param GridField $gridField |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function getActions($gridField) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Manipulate the state to add a new relation |
||
| 167 | * |
||
| 168 | * @param GridField $gridField |
||
| 169 | * @param string $actionName Action identifier, see {@link getActions()}. |
||
| 170 | * @param array $arguments Arguments relevant for this |
||
| 171 | * @param array $data All form data |
||
| 172 | */ |
||
| 173 | public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * If an object ID is set, add the object to the list |
||
| 186 | * |
||
| 187 | * @param GridField $gridField |
||
| 188 | * @param SS_List $dataList |
||
| 189 | * @return SS_List |
||
| 190 | */ |
||
| 191 | public function getManipulatedData(GridField $gridField, SS_List $dataList) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * |
||
| 207 | * @param GridField $gridField |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function getURLHandlers($gridField) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns a json array of a search results that can be used by for example Jquery.ui.autosuggestion |
||
| 219 | * |
||
| 220 | * @param GridField $gridField |
||
| 221 | * @param HTTPRequest $request |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function doSearch($gridField, $request) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $format |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | public function setResultsFormat($format) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getResultsFormat() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sets the base list instance which will be used for the autocomplete |
||
| 291 | * search. |
||
| 292 | * |
||
| 293 | * @param SS_List $list |
||
| 294 | */ |
||
| 295 | public function setSearchList(SS_List $list) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param array $fields |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function setSearchFields($fields) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function getSearchFields() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Detect searchable fields and searchable relations. |
||
| 321 | * Falls back to {@link DataObject->summaryFields()} if |
||
| 322 | * no custom search fields are defined. |
||
| 323 | * |
||
| 324 | * @param string $dataClass The class name |
||
| 325 | * @return array|null names of the searchable fields |
||
| 326 | */ |
||
| 327 | public function scaffoldSearchFields($dataClass) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $dataClass The class of the object being searched for |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function getPlaceholderText($dataClass) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string $text |
||
| 403 | * |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | public function setPlaceholderText($text) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Gets the maximum number of autocomplete results to display. |
||
| 414 | * |
||
| 415 | * @return int |
||
| 416 | */ |
||
| 417 | public function getResultsLimit() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param int $limit |
||
| 424 | * |
||
| 425 | * @return $this |
||
| 426 | */ |
||
| 427 | public function setResultsLimit($limit) |
||
| 432 | } |
||
| 433 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.