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 |
||
7 | class AdminQuery extends ActiveQuery |
||
8 | { |
||
9 | /** |
||
10 | * @return AdminQuery the query with conditions for users that can login applied |
||
11 | */ |
||
12 | 4 | public function canLogin() |
|
18 | |||
19 | /** |
||
20 | * @return AdminQuery the query with condition for given email applied |
||
21 | */ |
||
22 | 2 | public function email($email) |
|
26 | |||
27 | /** |
||
28 | * @return AdminQuery the query with condition for given username applied |
||
29 | */ |
||
30 | 2 | public function username($username) |
|
34 | |||
35 | /** |
||
36 | * @param string $token the password reset token |
||
37 | * @return AdminQuery the query with conditions for valid password reset token applied |
||
38 | */ |
||
39 | 2 | View Code Duplication | public function passwordResetToken($token) |
50 | |||
51 | /** |
||
52 | * @param string $token the email confirmation token |
||
53 | * @return AdminQuery the query with conditions for valid email confirmation token applied |
||
54 | */ |
||
55 | 1 | View Code Duplication | public function emailConfirmationToken($token) |
66 | } |
||
67 |
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.