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 |
||
25 | View Code Duplication | final class UserRememberPasswordRequested implements UserEvent |
|
|
|||
26 | { |
||
27 | /** |
||
28 | * The user id. |
||
29 | * |
||
30 | * @var UserId |
||
31 | */ |
||
32 | private $userId; |
||
33 | |||
34 | /** |
||
35 | * The email. |
||
36 | * |
||
37 | * @var UserEmail |
||
38 | */ |
||
39 | private $email; |
||
40 | |||
41 | /** |
||
42 | * The occurred on. |
||
43 | * |
||
44 | * @var \DateTimeImmutable |
||
45 | */ |
||
46 | private $occurredOn; |
||
47 | |||
48 | /** |
||
49 | * The remember password token. |
||
50 | * |
||
51 | * @var UserToken |
||
52 | */ |
||
53 | private $rememberPasswordToken; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param UserId $aUserId The user id |
||
59 | * @param UserEmail $anEmail The email |
||
60 | * @param UserToken $aRememberPasswordToken The remember password token |
||
61 | */ |
||
62 | public function __construct(UserId $aUserId, UserEmail $anEmail, UserToken $aRememberPasswordToken) |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function id() |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function email() |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function occurredOn() |
||
93 | |||
94 | /** |
||
95 | * Gets the remember password token. |
||
96 | * |
||
97 | * @return UserToken |
||
98 | */ |
||
99 | public function rememberPasswordToken() |
||
103 | } |
||
104 |
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.