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 |
||
26 | class ContactPhoneController extends RestController implements ClassResourceInterface |
||
27 | { |
||
28 | /** |
||
29 | * REST GET list |
||
30 | * |
||
31 | * @ApiDoc( |
||
32 | * description="Get all phones items", |
||
33 | * resource=true |
||
34 | * ) |
||
35 | * @AclAncestor("orocrm_contact_view") |
||
36 | * @param int $contactId |
||
37 | * @return Response |
||
38 | */ |
||
39 | View Code Duplication | public function cgetAction($contactId) |
|
57 | |||
58 | /** |
||
59 | * REST GET primary phone |
||
60 | * |
||
61 | * @param string $contactId |
||
62 | * |
||
63 | * @ApiDoc( |
||
64 | * description="Get contact primary phone", |
||
65 | * resource=true |
||
66 | * ) |
||
67 | * @AclAncestor("orocrm_contact_view") |
||
68 | * @return Response |
||
69 | */ |
||
70 | View Code Duplication | public function getPrimaryAction($contactId) |
|
85 | |||
86 | /** |
||
87 | * Create entity ContactPhone |
||
88 | * oro_api_post_contact_phone |
||
89 | * |
||
90 | * @return Response |
||
91 | * |
||
92 | * @ApiDoc( |
||
93 | * description="Create entity", |
||
94 | * resource=true, |
||
95 | * requirements = { |
||
96 | * {"name"="id", "dataType"="integer"}, |
||
97 | * } |
||
98 | * ) |
||
99 | */ |
||
100 | public function postAction() |
||
106 | |||
107 | /** |
||
108 | * Delete entity ContactPhone |
||
109 | * oro_api_delete_contact_phone |
||
110 | * |
||
111 | * @param int $id |
||
112 | * |
||
113 | * @ApiDoc( |
||
114 | * description="Delete ContactPhone" |
||
115 | * ) |
||
116 | * |
||
117 | * @return Response |
||
118 | */ |
||
119 | View Code Duplication | public function deleteAction($id) |
|
129 | |||
130 | protected function getContactManager() |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function getManager() |
||
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | protected function getPreparedItem($entity, $resultFields = []) |
||
155 | |||
156 | /** |
||
157 | * @return ApiFormHandler |
||
158 | */ |
||
159 | public function getFormHandler() |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function getForm() |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function getDeleteHandler() |
||
179 | } |
||
180 |
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.