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 $_visibleHover=false; |
||
33 | |||
34 | public function run(JsUtils $js){ |
||
47 | |||
48 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
56 | */ |
||
57 | protected function getTable() { |
||
60 | |||
61 | |||
62 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
99 | |||
100 | private function _generateMainCheckbox(&$captions){ |
||
106 | |||
107 | protected function _generateContent($table){ |
||
130 | |||
131 | private function _generatePagination($table){ |
||
140 | |||
141 | protected function _getFieldName($index){ |
||
144 | |||
145 | protected function _getFieldCaption($index){ |
||
148 | |||
149 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
164 | |||
165 | /** |
||
166 | * Associates a $callback function after the compilation of the field at $index position |
||
167 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
168 | * @param int $index postion of the compiled field |
||
169 | * @param callable $callback function called after the field compilation |
||
170 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
171 | */ |
||
172 | public function afterCompile($index,$callback){ |
||
176 | |||
177 | private function addToolbarRow($part,$table,$captions){ |
||
187 | |||
188 | public function getHtmlComponent(){ |
||
191 | |||
192 | public function getUrls() { |
||
195 | |||
196 | public function setUrls($urls) { |
||
200 | |||
201 | public function paginate($items_per_page=10,$page=1){ |
||
204 | |||
205 | public function getHasCheckboxes() { |
||
208 | |||
209 | public function setHasCheckboxes($_hasCheckboxes) { |
||
213 | |||
214 | public function refresh($compileParts=["tbody"]){ |
||
218 | /** |
||
219 | * @param string $caption |
||
220 | * @param callable $callback |
||
221 | * @param boolean $visibleHover |
||
222 | * @return callable |
||
223 | */ |
||
224 | private function getFieldButtonCallable($caption,$visibleHover=true,$callback=null){ |
||
227 | |||
228 | /** |
||
229 | * @param callable $thisCallback |
||
230 | * @param array $parameters |
||
231 | * @param callable $callback |
||
232 | * @return callable |
||
233 | */ |
||
234 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
253 | |||
254 | /** |
||
255 | * @param string $caption |
||
256 | * @return HtmlButton |
||
257 | */ |
||
258 | private function getFieldButton($caption,$visibleHover=true){ |
||
264 | |||
265 | /** |
||
266 | * Inserts a new Button for each row |
||
267 | * @param string $caption |
||
268 | * @param callable $callback |
||
269 | * @param boolean $visibleHover |
||
270 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
271 | */ |
||
272 | public function addFieldButton($caption,$visibleHover=true,$callback=null){ |
||
276 | |||
277 | /** |
||
278 | * Inserts a new Button for each row at col $index |
||
279 | * @param int $index |
||
280 | * @param string $caption |
||
281 | * @param callable $callback |
||
282 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
283 | */ |
||
284 | public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
288 | |||
289 | /** |
||
290 | * Inserts a new Button for each row in col at $index |
||
291 | * @param int $index |
||
292 | * @param string $caption |
||
293 | * @param callable $callback |
||
294 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
295 | */ |
||
296 | public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
300 | |||
301 | private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null){ |
||
305 | |||
306 | private function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null){ |
||
310 | |||
311 | private function getDefaultButton($icon,$class=null,$visibleHover=true){ |
||
318 | |||
319 | public function addDeleteButton($visibleHover=true,$callback=null){ |
||
322 | |||
323 | public function addEditButton($visibleHover=true,$callback=null){ |
||
326 | |||
327 | public function addEditDeleteButtons($visibleHover=true,$callbackEdit=null,$callbackDelete=null){ |
||
333 | |||
334 | public function insertDeleteButtonIn($index,$visibleHover=true,$callback=null){ |
||
337 | |||
338 | public function insertEditButtonIn($index,$visibleHover=true,$callback=null){ |
||
341 | |||
342 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
345 | |||
346 | public function getSearchField(){ |
||
353 | |||
354 | /** |
||
355 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
356 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
357 | * @param callable $callback |
||
358 | * @return DataTable |
||
359 | */ |
||
360 | public function onNewRow($callback) { |
||
364 | |||
365 | public function asForm(){ |
||
368 | |||
369 | public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
||
376 | |||
377 | protected function _visibleOver($element){ |
||
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: