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 |
||
23 | View Code Duplication | class WithConfirmationSignUpUserCommand |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * The user id. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $id; |
||
31 | |||
32 | /** |
||
33 | * The user email. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $email; |
||
38 | |||
39 | /** |
||
40 | * The plain password. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | private $plainPassword; |
||
45 | |||
46 | /** |
||
47 | * Array which contains the roles. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $roles; |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * @param string $anEmail The email |
||
57 | * @param string $aPlainPassword The plain password |
||
58 | * @param array $roles Array which contains the roles |
||
59 | * @param string|null $anId User id, it can be null |
||
60 | */ |
||
61 | public function __construct($anEmail, $aPlainPassword, array $roles, $anId = null) |
||
68 | |||
69 | /** |
||
70 | * Gets the id. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function id() |
||
78 | |||
79 | /** |
||
80 | * Gets the email. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function email() |
||
88 | |||
89 | /** |
||
90 | * Gets the user plain password. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function password() |
||
98 | |||
99 | /** |
||
100 | * Gets the roles. |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | public function roles() |
||
108 | } |
||
109 |
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.