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:
1 | <?php |
||
16 | class Comments extends AdminController |
||
17 | { |
||
18 | const VERSION = '1.0.0'; |
||
19 | const ITEM_PER_PAGE = 10; |
||
20 | |||
21 | const TYPE_COMMENT = 'comment'; |
||
22 | const TYPE_ANSWER = 'answer'; |
||
23 | |||
24 | public $type = 'widget'; |
||
25 | |||
26 | // heavy actions import |
||
27 | use Comments\ActionIndex { |
||
28 | index as actionIndex; |
||
29 | } |
||
30 | |||
31 | use Comments\ActionEdit { |
||
32 | edit as actionEdit; |
||
33 | } |
||
34 | |||
35 | use Comments\ActionDelete { |
||
36 | delete as actionDelete; |
||
37 | } |
||
38 | |||
39 | use Comments\ActionPublish { |
||
40 | publish as actionPublish; |
||
41 | } |
||
42 | |||
43 | use Comments\ActionAnswerList { |
||
44 | answerList as actionAnswerlist; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * List comment - read comment and list answers |
||
49 | * @param int $id |
||
50 | * @return string |
||
51 | * @throws NotFoundException |
||
52 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
53 | */ |
||
54 | public function actionRead($id) |
||
67 | |||
68 | /** |
||
69 | * Comment widget global settings |
||
70 | * @return string |
||
71 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
72 | */ |
||
73 | View Code Duplication | public function actionSettings() |
|
94 | } |
||
95 |
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.