Complex classes like DataTable 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 DataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DataTable extends Widget { |
||
26 | use TableTrait,DataTableFieldAsTrait,HasCheckboxesTrait; |
||
27 | protected $_searchField; |
||
28 | protected $_urls; |
||
29 | protected $_pagination; |
||
30 | protected $_compileParts; |
||
31 | protected $_deleteBehavior; |
||
32 | protected $_editBehavior; |
||
33 | protected $_visibleHover=false; |
||
34 | protected $_targetSelector; |
||
35 | protected $_refreshSelector; |
||
36 | protected $_emptyMessage; |
||
37 | protected $_json; |
||
38 | protected $_rowClass=""; |
||
39 | |||
40 | |||
41 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
48 | |||
49 | public function run(JsUtils $js){ |
||
63 | |||
64 | |||
65 | |||
66 | protected function _generateBehavior($op,$params,JsUtils $js){ |
||
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
76 | */ |
||
77 | protected function getTable() { |
||
80 | |||
81 | |||
82 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
123 | |||
124 | |||
125 | |||
126 | protected function _generateContent($table){ |
||
141 | |||
142 | protected function _generateRow($instance,&$table,$checkedClass=null){ |
||
161 | |||
162 | protected function _generatePagination($table,$js=NULL){ |
||
175 | |||
176 | protected function _associatePaginationBehavior(HtmlMenu $menu,JsUtils $js=NULL){ |
||
181 | |||
182 | protected function _getFieldName($index){ |
||
185 | |||
186 | protected function _getFieldCaption($index){ |
||
189 | |||
190 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
205 | |||
206 | /** |
||
207 | * Associates a $callback function after the compilation of the field at $index position |
||
208 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
209 | * @param int $index postion of the compiled field |
||
210 | * @param callable $callback function called after the field compilation |
||
211 | * @return DataTable |
||
212 | */ |
||
213 | public function afterCompile($index,$callback){ |
||
217 | |||
218 | private function addToolbarRow($part,$table,$captions){ |
||
228 | |||
229 | public function getHtmlComponent(){ |
||
232 | |||
233 | public function getUrls() { |
||
236 | |||
237 | /** |
||
238 | * Sets the associative array of urls for refreshing, updating or deleting |
||
239 | * @param string|array $urls associative array with keys refresh: for refreshing with search field or pagination, edit : for updating a row, delete: for deleting a row |
||
240 | * @return DataTable |
||
241 | */ |
||
242 | public function setUrls($urls) { |
||
252 | |||
253 | /** |
||
254 | * Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
255 | * @param number $page the active page number |
||
256 | * @param number $total_rowcount the total number of items |
||
257 | * @param number $items_per_page The number of items per page |
||
258 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
259 | * @return DataTable |
||
260 | */ |
||
261 | public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){ |
||
265 | |||
266 | /** |
||
267 | * Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
268 | * @param number $page the active page number |
||
269 | * @param number $items_per_page The number of items per page |
||
270 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
271 | * @return DataTable |
||
272 | */ |
||
273 | public function autoPaginate($page=1,$items_per_page=10,$pages_visibles=4){ |
||
277 | |||
278 | |||
279 | |||
280 | /** |
||
281 | * @param array $compileParts |
||
282 | * @return DataTable |
||
283 | */ |
||
284 | public function refresh($compileParts=["tbody"]){ |
||
288 | |||
289 | |||
290 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
293 | |||
294 | public function getSearchField(){ |
||
301 | |||
302 | /** |
||
303 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
304 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
305 | * @param callable $callback |
||
306 | * @return DataTable |
||
307 | */ |
||
308 | public function onNewRow($callback) { |
||
312 | |||
313 | public function asForm(){ |
||
316 | |||
317 | |||
318 | |||
319 | protected function getTargetSelector() { |
||
325 | |||
326 | /** |
||
327 | * Sets the response element selector for Edit and Delete request with ajax |
||
328 | * @param string $_targetSelector |
||
329 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
330 | */ |
||
331 | public function setTargetSelector($_targetSelector) { |
||
335 | |||
336 | public function getRefreshSelector() { |
||
341 | |||
342 | public function setRefreshSelector($_refreshSelector) { |
||
346 | |||
347 | public function show($modelInstance){ |
||
354 | |||
355 | public function getRowClass() { |
||
358 | |||
359 | /** |
||
360 | * Sets the default row class (tr class) |
||
361 | * @param string $_rowClass |
||
362 | * @return DataTable |
||
363 | */ |
||
364 | public function setRowClass($_rowClass) { |
||
368 | |||
369 | /** |
||
370 | * Sets the message displayed when there is no record |
||
371 | * @param mixed $_emptyMessage |
||
372 | * @return DataTable |
||
373 | */ |
||
374 | public function setEmptyMessage($_emptyMessage) { |
||
378 | |||
379 | |||
380 | |||
381 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: