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 |
||
18 | class EmailController extends Controller |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Lists all Email entities. |
||
23 | * |
||
24 | * @Route("/{page}", name="email-spool", defaults={"page" = 1}, requirements={"page" = "\d+"}) |
||
25 | * @Method("GET") |
||
26 | * @Template() |
||
27 | */ |
||
28 | public function indexAction($page) |
||
44 | |||
45 | /** |
||
46 | * Finds and displays a Email entity. |
||
47 | * |
||
48 | * @Route("/{id}/show", name="email-spool_show") |
||
49 | * @Method("GET") |
||
50 | * @Template() |
||
51 | */ |
||
52 | public function showAction($id) |
||
66 | |||
67 | /** |
||
68 | * Retry to send an email |
||
69 | * |
||
70 | * @Route("/{id}/retry", name="email-spool_retry") |
||
71 | * @Method("GET") |
||
72 | */ |
||
73 | View Code Duplication | public function retryAction($id) |
|
91 | |||
92 | /** |
||
93 | * Resend an email |
||
94 | * |
||
95 | * @Route("/{id}/resend", name="email-spool_resend") |
||
96 | * @Method("GET") |
||
97 | */ |
||
98 | View Code Duplication | public function resendAction($id) |
|
116 | |||
117 | /** |
||
118 | * Cancel an email sending |
||
119 | * |
||
120 | * @Route("/{id}/cancel", name="email-spool_cancel") |
||
121 | * @Method("GET") |
||
122 | */ |
||
123 | View Code Duplication | public function cancelAction($id) |
|
140 | |||
141 | /** |
||
142 | * Deletes a Email entity. |
||
143 | * |
||
144 | * @Route("/{id}/delete", name="email-spool_delete") |
||
145 | * @Method("GET") |
||
146 | */ |
||
147 | public function deleteAction($id) |
||
163 | |||
164 | } |
||
165 |
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.