Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
21 | class DataTable extends Widget { |
||
22 | |||
23 | protected $_searchField; |
||
24 | protected $_urls; |
||
25 | protected $_pagination; |
||
26 | protected $_hasCheckboxes; |
||
27 | protected $_toolbar; |
||
28 | protected $_compileParts; |
||
29 | protected $_toolbarPosition; |
||
30 | |||
31 | public function run(JsUtils $js){ |
||
40 | |||
41 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
47 | |||
48 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
86 | |||
87 | private function _generateContent($table){ |
||
106 | |||
107 | private function _generatePagination($table){ |
||
116 | |||
117 | private function _setToolbarPosition($table,$captions){ |
||
129 | |||
130 | /** |
||
131 | * Associates a $callback function after the compilation of the field at $index position |
||
132 | * The $callback function can take the following arguments : $field=>the compiled field, $index: the field position, $instance : the active instance of the object |
||
133 | * @param int $index postion of the compiled field |
||
134 | * @param callable $callback function called after the field compilation |
||
135 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
136 | */ |
||
137 | public function afterCompile($index,$callback){ |
||
141 | |||
142 | private function addToolbarRow($part,$table,$captions){ |
||
147 | |||
148 | public function getInstanceViewer() { |
||
151 | |||
152 | public function setInstanceViewer($_instanceViewer) { |
||
156 | |||
157 | public function setCaptions($captions){ |
||
161 | |||
162 | public function setFields($fields){ |
||
166 | |||
167 | public function addField($field){ |
||
171 | |||
172 | public function insertField($index,$field){ |
||
176 | |||
177 | public function insertInField($index,$field){ |
||
181 | |||
182 | public function setValueFunction($index,$callback){ |
||
186 | |||
187 | public function setIdentifierFunction($callback){ |
||
191 | |||
192 | public function getHtmlComponent(){ |
||
195 | |||
196 | public function getUrls() { |
||
199 | |||
200 | public function setUrls($urls) { |
||
204 | |||
205 | public function paginate($items_per_page=10,$page=1){ |
||
208 | |||
209 | public function getHasCheckboxes() { |
||
212 | |||
213 | public function setHasCheckboxes($_hasCheckboxes) { |
||
217 | |||
218 | public function refresh($compileParts=["tbody"]){ |
||
222 | /** |
||
223 | * @param string $caption |
||
224 | * @param callable $callback |
||
225 | * @return callable |
||
226 | */ |
||
227 | private function getFieldButtonCallable($caption,$callback=null){ |
||
230 | |||
231 | /** |
||
232 | * @param mixed $object |
||
233 | * @param callable $callback |
||
234 | * @return callable |
||
235 | */ |
||
236 | private function getCallable($object,$callback=null){ |
||
247 | |||
248 | /** |
||
249 | * @param string $caption |
||
250 | * @return HtmlButton |
||
251 | */ |
||
252 | private function getFieldButton($caption){ |
||
257 | |||
258 | /** |
||
259 | * Inserts a new Button for each row |
||
260 | * @param string $caption |
||
261 | * @param callable $callback |
||
262 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
263 | */ |
||
264 | public function addFieldButton($caption,$callback=null){ |
||
268 | |||
269 | /** |
||
270 | * Inserts a new Button for each row at col $index |
||
271 | * @param int $index |
||
272 | * @param string $caption |
||
273 | * @param callable $callback |
||
274 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
275 | */ |
||
276 | public function insertFieldButton($index,$caption,$callback=null){ |
||
280 | |||
281 | /** |
||
282 | * Inserts a new Button for each row in col at $index |
||
283 | * @param int $index |
||
284 | * @param string $caption |
||
285 | * @param callable $callback |
||
286 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
287 | */ |
||
288 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
292 | |||
293 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
298 | |||
299 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
304 | |||
305 | private function getDefaultButton($icon,$class=null){ |
||
312 | |||
313 | public function addDeleteButton($callback=null){ |
||
316 | |||
317 | public function addEditButton($callback=null){ |
||
320 | |||
321 | public function addEditDeleteButtons($callbackEdit=null,$callbackDelete=null){ |
||
327 | |||
328 | public function insertDeleteButtonIn($index,$callback=null){ |
||
331 | |||
332 | public function insertEditButtonIn($index,$callback=null){ |
||
335 | |||
336 | public function setSelectable(){ |
||
340 | |||
341 | /** |
||
342 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
343 | */ |
||
344 | public function getToolbar(){ |
||
351 | |||
352 | /** |
||
353 | * @param unknown $element |
||
354 | * @return \Ajax\common\html\HtmlDoubleElement |
||
355 | */ |
||
356 | public function addInToolbar($element){ |
||
360 | |||
361 | public function addItemInToolbar($caption,$icon=NULL){ |
||
366 | |||
367 | public function addButtonInToolbar($caption){ |
||
371 | |||
372 | public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){ |
||
377 | |||
378 | |||
379 | public function addSearchInToolbar(){ |
||
382 | |||
383 | public function getSearchField(){ |
||
390 | |||
391 | public function setSortable($colIndex=NULL) { |
||
395 | |||
396 | public function fieldAsImage($index,$size=Size::SMALL,$circular=false){ |
||
404 | |||
405 | public function fieldAsAvatar($index){ |
||
409 | |||
410 | public function fieldAsRadio($index,$name=NULL){ |
||
421 | |||
422 | View Code Duplication | public function fieldAsInput($index,$name=NULL,$type="text",$placeholder=""){ |
|
435 | |||
436 | View Code Duplication | public function fieldAsCheckbox($index,$name=NULL){ |
|
448 | |||
449 | public function fieldAsDropDown($index,$elements=[],$multiple=false,$name=NULL){ |
||
460 | |||
461 | private function _getFieldIdentifier($prefix){ |
||
464 | } |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.