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 | public function indexAction() |
||
56 | |||
57 | /** |
||
58 | * Get a lost password email link token. |
||
59 | * |
||
60 | * @SWG\Get( |
||
61 | * path="/user/lost-password/{email}", |
||
62 | * tags={"users"}, |
||
63 | * @SWG\Parameter( |
||
64 | * name="email", |
||
65 | * in="path", |
||
66 | * type="string", |
||
67 | * description="the email of the user", |
||
68 | * required=true, |
||
69 | * default="[email protected]" |
||
70 | * ), |
||
71 | * @SWG\Response(response="200", description="Sends email link details") |
||
72 | * ) |
||
73 | * @throws Exception |
||
74 | */ |
||
75 | View Code Duplication | public function lostPasswordAction() |
|
93 | |||
94 | |||
95 | |||
96 | /** |
||
97 | * Activate from the email link token. |
||
98 | * |
||
99 | * @SWG\Get( |
||
100 | * path="/user/activate/{email}/{token}", |
||
101 | * tags={"users"}, |
||
102 | * @SWG\Response(response="200", description="Registers a new unactivated user"), |
||
103 | * @SWG\Parameter( |
||
104 | * name="email", |
||
105 | * in="path", |
||
106 | * type="string", |
||
107 | * description="the users email", |
||
108 | * required=true, |
||
109 | * default="[email protected]" |
||
110 | * ), |
||
111 | * @SWG\Parameter( |
||
112 | * name="token", |
||
113 | * in="path", |
||
114 | * type="string", |
||
115 | * description="the email link token", |
||
116 | * required=true, |
||
117 | * default="r4nd0mT0k3n" |
||
118 | * ) |
||
119 | * ) |
||
120 | * @throws Exception |
||
121 | */ |
||
122 | public function activateAction() |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Refresh the activation email link token. |
||
164 | * |
||
165 | * @SWG\Get( |
||
166 | * path="/user/activate/resend/{email}", |
||
167 | * tags={"users"}, |
||
168 | * @SWG\Parameter( |
||
169 | * name="email", |
||
170 | * in="path", |
||
171 | * type="string", |
||
172 | * description="the email of the user registering", |
||
173 | * required=true, |
||
174 | * default="[email protected]" |
||
175 | * ), |
||
176 | * @SWG\Response(response="200", description="Sends email link details") |
||
177 | * ) |
||
178 | * @throws Exception |
||
179 | */ |
||
180 | View Code Duplication | public function resendActivationAction() |
|
198 | |||
199 | /** |
||
200 | * Register as a new user. Returns an email link token. |
||
201 | * |
||
202 | * @SWG\Post( |
||
203 | * path="/user/register", |
||
204 | * tags={"users"}, |
||
205 | * @SWG\Response(response="200", description="Registers a new unactivated user"), |
||
206 | * @SWG\Parameter( |
||
207 | * name="email", |
||
208 | * in="formData", |
||
209 | * type="string", |
||
210 | * description="the users email", |
||
211 | * required=true, |
||
212 | * default="[email protected]" |
||
213 | * ), |
||
214 | * @SWG\Parameter( |
||
215 | * name="password", |
||
216 | * in="formData", |
||
217 | * type="string", |
||
218 | * description="a password for the user", |
||
219 | * required=true, |
||
220 | * default="password" |
||
221 | * ), |
||
222 | * @SWG\Parameter( |
||
223 | * name="confirm", |
||
224 | * in="formData", |
||
225 | * type="string", |
||
226 | * description="password confirmation", |
||
227 | * required=true, |
||
228 | * default="password" |
||
229 | * ) |
||
230 | * ) |
||
231 | * @throws Exception |
||
232 | */ |
||
233 | public function registerAction() |
||
260 | } |
||
261 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.