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 TableController 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 TableController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class TableController extends AbstractActionController |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * |
||
25 | * @var ResultSet |
||
26 | */ |
||
27 | protected $resultSet; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @var |
||
32 | */ |
||
33 | protected $customerTable; |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var \Zend\Db\Adapter\Adapter |
||
38 | */ |
||
39 | protected $dbAdapter; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var ModuleOptions |
||
44 | */ |
||
45 | protected $moduleOptions; |
||
46 | |||
47 | |||
48 | |||
49 | |||
50 | /** |
||
51 | * @var \Doctrine\ORM\EntityManager |
||
52 | */ |
||
53 | protected $em; |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | * @return \Doctrine\ORM\EntityManager |
||
58 | */ |
||
59 | public function getEntityManager() |
||
66 | |||
67 | public function changesAction() |
||
71 | |||
72 | public function bootstrapAction() |
||
75 | |||
76 | /** |
||
77 | * ********* Additional Params ******************* |
||
78 | * *********************************** |
||
79 | */ |
||
80 | public function additionalParamsAction() |
||
92 | |||
93 | /** |
||
94 | * ********* Javascript Events ******************* |
||
95 | * *********************************** |
||
96 | */ |
||
97 | public function javascriptEventsAction() |
||
109 | |||
110 | |||
111 | /** |
||
112 | * ********* Array Adapter ******************* |
||
113 | * ***************************************** |
||
114 | */ |
||
115 | public function arrayAction() |
||
118 | |||
119 | public function ajaxArrayAction() |
||
139 | |||
140 | |||
141 | /** |
||
142 | * ********* Doctrine Adapter ******************* |
||
143 | * ***************************************** |
||
144 | */ |
||
145 | public function doctrineAction() |
||
180 | |||
181 | |||
182 | /** |
||
183 | * ********* Callable ******************* |
||
184 | * ***************************************** |
||
185 | */ |
||
186 | public function callableAction() |
||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * ********* Column filtering ******************* |
||
203 | * *********************************** |
||
204 | */ |
||
205 | public function columnFilteringAction() |
||
217 | |||
218 | |||
219 | /** |
||
220 | * ********* Editable tabble ******************* |
||
221 | * *********************************** |
||
222 | */ |
||
223 | public function editableAction() |
||
235 | |||
236 | public function updateRowAction() |
||
245 | |||
246 | |||
247 | /** |
||
248 | * ********* Export To CSV ******************* |
||
249 | * ***************************************** |
||
250 | */ |
||
251 | public function csvExportAction() |
||
263 | |||
264 | /** |
||
265 | * ********* SEPARATABLE ******************* |
||
266 | * ***************************************** |
||
267 | */ |
||
268 | public function separatableAction() |
||
283 | |||
284 | /** |
||
285 | * ********* Variable Row Attr ******************* |
||
286 | * ***************************************** |
||
287 | */ |
||
288 | public function variableAttrRowAction() |
||
292 | |||
293 | /** |
||
294 | * ********* New plugin condition ******************* |
||
295 | * ***************************************** |
||
296 | */ |
||
297 | public function newPluginConditionAction() |
||
310 | |||
311 | |||
312 | |||
313 | /** |
||
314 | * ********* Base ******************* |
||
315 | * *********************************** |
||
316 | */ |
||
317 | public function baseAction() |
||
329 | |||
330 | |||
331 | /** |
||
332 | * ********* Mapper ******************* |
||
333 | * *********************************** |
||
334 | */ |
||
335 | public function mapperAction() |
||
339 | |||
340 | View Code Duplication | public function ajaxMapperAction() |
|
350 | |||
351 | /** |
||
352 | * ********* Link ******************* |
||
353 | * *********************************** |
||
354 | */ |
||
355 | public function linkAction() |
||
359 | |||
360 | View Code Duplication | public function ajaxLinkAction() |
|
369 | |||
370 | |||
371 | /** |
||
372 | * ********* Template ******************* |
||
373 | * *********************************** |
||
374 | */ |
||
375 | public function templateAction() |
||
379 | |||
380 | View Code Duplication | public function ajaxTemplateAction() |
|
389 | |||
390 | |||
391 | /** |
||
392 | * ********* Attr ******************* |
||
393 | * *********************************** |
||
394 | */ |
||
395 | public function attrAction() |
||
399 | |||
400 | View Code Duplication | public function ajaxAttrAction() |
|
409 | |||
410 | |||
411 | /** |
||
412 | * ********* Condition ******************* |
||
413 | * *********************************** |
||
414 | */ |
||
415 | public function conditionAction() |
||
419 | |||
420 | View Code Duplication | public function ajaxConditionAction() |
|
429 | |||
430 | |||
431 | /** |
||
432 | * ********* Mix ******************* |
||
433 | * *********************************** |
||
434 | */ |
||
435 | public function mixAction() |
||
439 | |||
440 | View Code Duplication | public function ajaxMixAction() |
|
449 | |||
450 | /** |
||
451 | * ********* Advance ******************* |
||
452 | * *********************************** |
||
453 | */ |
||
454 | public function advanceAction() |
||
458 | |||
459 | View Code Duplication | public function ajaxAdvanceAction() |
|
469 | |||
470 | |||
471 | |||
472 | |||
473 | |||
474 | |||
475 | |||
476 | /************************************************ */ |
||
477 | /*************** EXAMPLE DATA TABLE ************* */ |
||
478 | |||
479 | public function dataTableAction() |
||
492 | |||
493 | public function ajaxDataTableAction() |
||
507 | |||
508 | |||
509 | public function htmlResponse($html) |
||
516 | |||
517 | public function jsonResponse($data) |
||
528 | |||
529 | |||
530 | /** |
||
531 | * |
||
532 | * @return \ZfTable\Example\Model\CustomerTable |
||
533 | * |
||
534 | */ |
||
535 | public function getCustomerTable() |
||
543 | |||
544 | /**********************My codes*********************/ |
||
545 | |||
546 | |||
547 | |||
548 | public function institutionRequestsAction() |
||
551 | |||
552 | View Code Duplication | public function ajaxInstitutionRequestsAction() |
|
560 | |||
561 | /** |
||
562 | * @return \Zend\Db\Sql\Select Type |
||
563 | */ |
||
564 | private function getInstitutionRequests() |
||
568 | |||
569 | |||
570 | |||
571 | /** |
||
572 | * |
||
573 | * @return \Zend\Db\Adapter\Adapter |
||
574 | */ |
||
575 | public function getDbAdapter() |
||
583 | |||
584 | /** |
||
585 | * |
||
586 | * @return \Zend\Db\Sql\Select |
||
587 | */ |
||
588 | private function getSource() |
||
592 | } |
||
593 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.