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; |
||
27 | protected $_searchField; |
||
28 | protected $_urls; |
||
29 | protected $_pagination; |
||
30 | protected $_hasCheckboxes; |
||
31 | protected $_compileParts; |
||
32 | protected $_hasDelete=false; |
||
33 | protected $_hasEdit=false; |
||
34 | protected $_visibleHover=false; |
||
35 | protected $_targetSelector; |
||
36 | |||
37 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
42 | |||
43 | public function run(JsUtils $js){ |
||
60 | |||
61 | protected function _generateBehavior($op,JsUtils $js){ |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
69 | */ |
||
70 | protected function getTable() { |
||
73 | |||
74 | |||
75 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
114 | |||
115 | private function _generateMainCheckbox(&$captions){ |
||
121 | |||
122 | protected function _generateContent($table){ |
||
145 | |||
146 | private function _generatePagination($table){ |
||
156 | |||
157 | protected function _getFieldName($index){ |
||
160 | |||
161 | protected function _getFieldCaption($index){ |
||
164 | |||
165 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
180 | |||
181 | /** |
||
182 | * Associates a $callback function after the compilation of the field at $index position |
||
183 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
184 | * @param int $index postion of the compiled field |
||
185 | * @param callable $callback function called after the field compilation |
||
186 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
187 | */ |
||
188 | public function afterCompile($index,$callback){ |
||
192 | |||
193 | private function addToolbarRow($part,$table,$captions){ |
||
203 | |||
204 | public function getHtmlComponent(){ |
||
207 | |||
208 | public function getUrls() { |
||
211 | |||
212 | /** |
||
213 | * Sets the associative array of urls for refreshing, updating or deleting |
||
214 | * @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 |
||
215 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
216 | */ |
||
217 | public function setUrls($urls) { |
||
227 | |||
228 | public function paginate($items_per_page=10,$page=1){ |
||
231 | |||
232 | public function getHasCheckboxes() { |
||
235 | |||
236 | public function setHasCheckboxes($_hasCheckboxes) { |
||
240 | |||
241 | public function refresh($compileParts=["tbody"]){ |
||
245 | /** |
||
246 | * @param string $caption |
||
247 | * @param callable $callback |
||
248 | * @param boolean $visibleHover |
||
249 | * @return callable |
||
250 | */ |
||
251 | private function getFieldButtonCallable($caption,$visibleHover=true,$callback=null){ |
||
254 | |||
255 | /** |
||
256 | * @param callable $thisCallback |
||
257 | * @param array $parameters |
||
258 | * @param callable $callback |
||
259 | * @return callable |
||
260 | */ |
||
261 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
280 | |||
281 | /** |
||
282 | * @param string $caption |
||
283 | * @return HtmlButton |
||
284 | */ |
||
285 | private function getFieldButton($caption,$visibleHover=true){ |
||
291 | |||
292 | /** |
||
293 | * Inserts a new Button for each row |
||
294 | * @param string $caption |
||
295 | * @param callable $callback |
||
296 | * @param boolean $visibleHover |
||
297 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
298 | */ |
||
299 | public function addFieldButton($caption,$visibleHover=true,$callback=null){ |
||
303 | |||
304 | /** |
||
305 | * Inserts a new Button for each row at col $index |
||
306 | * @param int $index |
||
307 | * @param string $caption |
||
308 | * @param callable $callback |
||
309 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
310 | */ |
||
311 | public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
315 | |||
316 | /** |
||
317 | * Inserts a new Button for each row in col at $index |
||
318 | * @param int $index |
||
319 | * @param string $caption |
||
320 | * @param callable $callback |
||
321 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
322 | */ |
||
323 | public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
327 | |||
328 | private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null){ |
||
332 | |||
333 | private function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null){ |
||
337 | |||
338 | private function getDefaultButton($icon,$class=null,$visibleHover=true){ |
||
345 | |||
346 | public function addDeleteButton($visibleHover=true,$generateBehavior=true,$callback=null){ |
||
350 | |||
351 | public function addEditButton($visibleHover=true,$generateBehavior=true,$callback=null){ |
||
355 | |||
356 | public function addEditDeleteButtons($visibleHover=true,$generateBehavior=true,$callbackEdit=null,$callbackDelete=null){ |
||
362 | |||
363 | public function insertDeleteButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){ |
||
367 | |||
368 | public function insertEditButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){ |
||
372 | |||
373 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
376 | |||
377 | public function getSearchField(){ |
||
384 | |||
385 | /** |
||
386 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
387 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
388 | * @param callable $callback |
||
389 | * @return DataTable |
||
390 | */ |
||
391 | public function onNewRow($callback) { |
||
395 | |||
396 | public function asForm(){ |
||
399 | |||
400 | /** |
||
401 | * Creates a submit button at $index position |
||
402 | * @param int $index |
||
403 | * @param string $cssStyle |
||
404 | * @param string $url |
||
405 | * @param string $responseElement |
||
406 | * @param array $attributes |
||
407 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
408 | */ |
||
409 | public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
||
418 | |||
419 | protected function _visibleOver($element){ |
||
423 | |||
424 | protected function getTargetSelector() { |
||
430 | |||
431 | /** |
||
432 | * Sets the response element selector for Edit and Delete request with ajax |
||
433 | * @param string $_targetSelector |
||
434 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
435 | */ |
||
436 | public function setTargetSelector($_targetSelector) { |
||
440 | |||
441 | } |
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: