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