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 | protected $_sortable; |
||
40 | protected $_hiddenColumns; |
||
41 | |||
42 | |||
43 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
50 | |||
51 | public function run(JsUtils $js){ |
||
65 | |||
66 | |||
67 | |||
68 | protected function _generateBehavior($op,$params,JsUtils $js){ |
||
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
78 | */ |
||
79 | protected function getTable() { |
||
82 | |||
83 | |||
84 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
127 | |||
128 | protected function _hideColumns(){ |
||
135 | |||
136 | protected function _generateHeader(HtmlTable $table,$captions){ |
||
142 | |||
143 | |||
144 | |||
145 | protected function _generateContent($table){ |
||
160 | |||
161 | protected function _generateRow($instance,&$table,$checkedClass=null){ |
||
183 | |||
184 | protected function _generatePagination($table,$js=NULL){ |
||
197 | |||
198 | protected function _associatePaginationBehavior(HtmlMenu $menu,JsUtils $js=NULL){ |
||
203 | |||
204 | protected function _getFieldName($index){ |
||
207 | |||
208 | protected function _getFieldCaption($index){ |
||
211 | |||
212 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
227 | |||
228 | /** |
||
229 | * Associates a $callback function after the compilation of the field at $index position |
||
230 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
231 | * @param int $index postion of the compiled field |
||
232 | * @param callable $callback function called after the field compilation |
||
233 | * @return DataTable |
||
234 | */ |
||
235 | public function afterCompile($index,$callback){ |
||
239 | |||
240 | private function addToolbarRow($part,$table,$captions){ |
||
250 | |||
251 | public function getHtmlComponent(){ |
||
254 | |||
255 | public function getUrls() { |
||
258 | |||
259 | /** |
||
260 | * Sets the associative array of urls for refreshing, updating or deleting |
||
261 | * @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 |
||
262 | * @return DataTable |
||
263 | */ |
||
264 | public function setUrls($urls) { |
||
274 | |||
275 | /** |
||
276 | * Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
277 | * @param number $page the active page number |
||
278 | * @param number $total_rowcount the total number of items |
||
279 | * @param number $items_per_page The number of items per page |
||
280 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
281 | * @return DataTable |
||
282 | */ |
||
283 | public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){ |
||
287 | |||
288 | /** |
||
289 | * Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
290 | * @param number $page the active page number |
||
291 | * @param number $items_per_page The number of items per page |
||
292 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
293 | * @return DataTable |
||
294 | */ |
||
295 | public function autoPaginate($page=1,$items_per_page=10,$pages_visibles=4){ |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * @param array $compileParts |
||
304 | * @return DataTable |
||
305 | */ |
||
306 | public function refresh($compileParts=["tbody"]){ |
||
310 | |||
311 | |||
312 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
315 | |||
316 | public function getSearchField(){ |
||
323 | |||
324 | /** |
||
325 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
326 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
327 | * @param callable $callback |
||
328 | * @return DataTable |
||
329 | */ |
||
330 | public function onNewRow($callback) { |
||
334 | |||
335 | public function asForm(){ |
||
338 | |||
339 | |||
340 | |||
341 | protected function getTargetSelector() { |
||
347 | |||
348 | /** |
||
349 | * Sets the response element selector for Edit and Delete request with ajax |
||
350 | * @param string $_targetSelector |
||
351 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
352 | */ |
||
353 | public function setTargetSelector($_targetSelector) { |
||
357 | |||
358 | public function getRefreshSelector() { |
||
363 | |||
364 | /** |
||
365 | * @param string $_refreshSelector |
||
366 | * @return DataTable |
||
367 | */ |
||
368 | public function setRefreshSelector($_refreshSelector) { |
||
372 | |||
373 | /** |
||
374 | * {@inheritDoc} |
||
375 | * @see \Ajax\common\Widget::show() |
||
376 | */ |
||
377 | public function show($modelInstance){ |
||
384 | |||
385 | public function getRowClass() { |
||
388 | |||
389 | /** |
||
390 | * Sets the default row class (tr class) |
||
391 | * @param string $_rowClass |
||
392 | * @return DataTable |
||
393 | */ |
||
394 | public function setRowClass($_rowClass) { |
||
398 | |||
399 | /** |
||
400 | * Sets the message displayed when there is no record |
||
401 | * @param mixed $_emptyMessage |
||
402 | * @return DataTable |
||
403 | */ |
||
404 | public function setEmptyMessage($_emptyMessage) { |
||
408 | |||
409 | public function setSortable($colIndex=NULL) { |
||
413 | |||
414 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
418 | |||
419 | public function hideColumn($colIndex){ |
||
425 | |||
426 | } |
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: