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 |
||
8 | class UserMailer extends Mailer |
||
9 | { |
||
10 | /** |
||
11 | * Register Email. |
||
12 | * |
||
13 | * @param User $user The user information. |
||
14 | * @param array $viewVars The variables to pass to the view. |
||
15 | * |
||
16 | * @return void |
||
17 | */ |
||
18 | View Code Duplication | public function register(User $user, $viewVars = []) |
|
27 | |||
28 | /** |
||
29 | * Login Email. Used when someone fail to login and his group is a staff group. |
||
30 | * |
||
31 | * @param array $viewVars The variables to pass to the view. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function login($viewVars = []) |
||
44 | |||
45 | /** |
||
46 | * ForgotPassword Email. |
||
47 | * |
||
48 | * @param User $user The user information. |
||
49 | * @param array $viewVars The variables to pass to the view. |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | View Code Duplication | public function forgotPassword(User $user, $viewVars = []) |
|
62 | } |
||
63 |
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.