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 |
||
13 | class UserController extends BaseController |
||
14 | { |
||
15 | /** @var UserService */ |
||
16 | private $userService; |
||
17 | |||
18 | public function init() |
||
23 | |||
24 | /** |
||
25 | * Fetch user details by ID. |
||
26 | * |
||
27 | * @SWG\Get( |
||
28 | * path="/user/{id}", |
||
29 | * tags={"users"}, |
||
30 | * @SWG\Parameter( |
||
31 | * name="id", |
||
32 | * in="path", |
||
33 | * type="integer", |
||
34 | * description="the type of response", |
||
35 | * required=false, |
||
36 | * default=1 |
||
37 | * ), |
||
38 | * @SWG\Response(response="200", description="Sends user details") |
||
39 | * ) |
||
40 | * |
||
41 | */ |
||
42 | View Code Duplication | public function indexAction() |
|
56 | |||
57 | /** |
||
58 | * Register as a new user. Gives an email link token. |
||
59 | * |
||
60 | * @SWG\Post( |
||
61 | * path="/user/register", |
||
62 | * tags={"users"}, |
||
63 | * @SWG\Response(response="200", description="Registers a new unactivated user"), |
||
64 | * @SWG\Parameter( |
||
65 | * name="email", |
||
66 | * in="formData", |
||
67 | * type="string", |
||
68 | * description="the users email", |
||
69 | * required=true, |
||
70 | * default="[email protected]" |
||
71 | * ), |
||
72 | * @SWG\Parameter( |
||
73 | * name="password", |
||
74 | * in="formData", |
||
75 | * type="string", |
||
76 | * description="a password for the user", |
||
77 | * required=true, |
||
78 | * default="password" |
||
79 | * ), |
||
80 | * @SWG\Parameter( |
||
81 | * name="confirm", |
||
82 | * in="formData", |
||
83 | * type="string", |
||
84 | * description="password confirmation", |
||
85 | * required=true, |
||
86 | * default="password" |
||
87 | * ) |
||
88 | * ) |
||
89 | * @throws Exception |
||
90 | */ |
||
91 | public function registerAction() |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Get a new activation email link. |
||
122 | * |
||
123 | * @SWG\Get( |
||
124 | * path="/user/activate/resend/{email}", |
||
125 | * tags={"users"}, |
||
126 | * @SWG\Parameter( |
||
127 | * name="email", |
||
128 | * in="path", |
||
129 | * type="string", |
||
130 | * description="the email of the user registering", |
||
131 | * required=true, |
||
132 | * default="[email protected]" |
||
133 | * ), |
||
134 | * @SWG\Response(response="200", description="Sends email link details") |
||
135 | * ) |
||
136 | * @throws Exception |
||
137 | */ |
||
138 | public function resendActivationAction() |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * Activate from the email link. |
||
161 | * |
||
162 | * @SWG\Get( |
||
163 | * path="/user/activate/{email}/{token}", |
||
164 | * tags={"users"}, |
||
165 | * @SWG\Response(response="200", description="Registers a new unactivated user"), |
||
166 | * @SWG\Parameter( |
||
167 | * name="email", |
||
168 | * in="path", |
||
169 | * type="string", |
||
170 | * description="the users email", |
||
171 | * required=true, |
||
172 | * default="[email protected]" |
||
173 | * ), |
||
174 | * @SWG\Parameter( |
||
175 | * name="token", |
||
176 | * in="path", |
||
177 | * type="string", |
||
178 | * description="the email link token", |
||
179 | * required=true, |
||
180 | * default="r4nd0mT0k3n" |
||
181 | * ) |
||
182 | * ) |
||
183 | * @throws Exception |
||
184 | */ |
||
185 | public function activateAction() |
||
224 | } |
||
225 |
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.