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 |
||
16 | class DataTable extends Widget { |
||
17 | |||
18 | protected $_searchField; |
||
19 | protected $_searchUrl; |
||
20 | protected $_pagination; |
||
21 | protected $_hasCheckboxes=true; |
||
22 | protected $_toolbar; |
||
23 | protected $_compileParts; |
||
24 | protected $_toolbarPosition; |
||
25 | |||
26 | public function run(JsUtils $js){ |
||
35 | |||
36 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
42 | |||
43 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
114 | |||
115 | private function addToolbarRow($part,$table,$captions){ |
||
120 | |||
121 | public function getInstanceViewer() { |
||
124 | |||
125 | public function setInstanceViewer($_instanceViewer) { |
||
129 | |||
130 | public function setCaptions($captions){ |
||
134 | |||
135 | public function setFields($fields){ |
||
139 | |||
140 | public function addField($field){ |
||
144 | |||
145 | public function insertField($index,$field){ |
||
149 | |||
150 | public function insertInField($index,$field){ |
||
154 | |||
155 | public function setValueFunction($index,$callback){ |
||
159 | |||
160 | public function setCkValueFunction($callback){ |
||
164 | |||
165 | public function getHtmlComponent(){ |
||
168 | |||
169 | public function getSearchUrl() { |
||
172 | |||
173 | public function setSearchUrl($_searchUrl) { |
||
177 | |||
178 | public function paginate($items_per_page=10,$page=1){ |
||
181 | |||
182 | public function getHasCheckboxes() { |
||
185 | |||
186 | public function setHasCheckboxes($_hasCheckboxes) { |
||
190 | |||
191 | public function refresh($compileParts=["tbody"]){ |
||
195 | /** |
||
196 | * @param string $caption |
||
197 | * @param callable $callback |
||
198 | * @return callable |
||
199 | */ |
||
200 | private function getFieldButtonCallable($caption,$callback=null){ |
||
203 | |||
204 | /** |
||
205 | * @param mixed $object |
||
206 | * @param callable $callback |
||
207 | * @return callable |
||
208 | */ |
||
209 | private function getCallable($object,$callback=null){ |
||
220 | |||
221 | /** |
||
222 | * @param string $caption |
||
223 | * @return HtmlButton |
||
224 | */ |
||
225 | private function getFieldButton($caption){ |
||
230 | |||
231 | /** |
||
232 | * Inserts a new Button for each row |
||
233 | * @param string $caption |
||
234 | * @param callable $callback |
||
235 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
236 | */ |
||
237 | public function addFieldButton($caption,$callback=null){ |
||
241 | |||
242 | /** |
||
243 | * Inserts a new Button for each row at col $index |
||
244 | * @param int $index |
||
245 | * @param string $caption |
||
246 | * @param callable $callback |
||
247 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
248 | */ |
||
249 | public function insertFieldButton($index,$caption,$callback=null){ |
||
253 | |||
254 | /** |
||
255 | * Inserts a new Button for each row in col at $index |
||
256 | * @param int $index |
||
257 | * @param string $caption |
||
258 | * @param callable $callback |
||
259 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
260 | */ |
||
261 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
265 | |||
266 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
271 | |||
272 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
277 | |||
278 | private function getDefaultButton($icon,$class=null){ |
||
285 | |||
286 | public function addDeleteButton($callback=null){ |
||
289 | |||
290 | public function addEditButton($callback=null){ |
||
293 | |||
294 | public function insertDeleteButtonIn($index,$callback=null){ |
||
297 | |||
298 | public function insertEditButtonIn($index,$callback=null){ |
||
301 | |||
302 | public function setSelectable(){ |
||
306 | |||
307 | /** |
||
308 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
309 | */ |
||
310 | public function getToolbar(){ |
||
317 | |||
318 | /** |
||
319 | * @param unknown $element |
||
320 | * @return \Ajax\common\html\HtmlDoubleElement |
||
321 | */ |
||
322 | public function addInToolbar($element){ |
||
326 | |||
327 | public function addItemInToolbar($caption,$icon=NULL){ |
||
332 | |||
333 | public function addButtonInToolbar($caption){ |
||
337 | |||
338 | public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){ |
||
343 | |||
344 | |||
345 | public function addSearchInToolbar(){ |
||
348 | |||
349 | public function getSearchField(){ |
||
356 | |||
357 | public function setSortable($colIndex=NULL) { |
||
361 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.