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 User implements UserInterface, PasswordReset |
||
8 | { |
||
9 | /** @var string */ |
||
10 | private $username; |
||
11 | |||
12 | /** @var string */ |
||
13 | private $password; |
||
14 | |||
15 | /** @var string */ |
||
16 | private $displayName; |
||
17 | |||
18 | /** @var */ |
||
19 | private $passwordResetToken; |
||
20 | |||
21 | /** @var */ |
||
22 | private $email; |
||
23 | |||
24 | /** |
||
25 | * @param string $username |
||
26 | * @param string $password |
||
27 | * @param string $displayName |
||
28 | */ |
||
29 | View Code Duplication | public function __construct($username, $password, $displayName, $email) |
|
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | public function getRoles() |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | public function getPassword() |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | public function getSalt() |
||
60 | |||
61 | /** |
||
62 | * {@inheritDoc} |
||
63 | */ |
||
64 | public function getUsername() |
||
68 | |||
69 | /** |
||
70 | * {@inheritDoc} |
||
71 | */ |
||
72 | public function eraseCredentials() |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | public function getDisplayName() |
||
84 | |||
85 | /** |
||
86 | * {@inheritDoc} |
||
87 | */ |
||
88 | public function __toString() |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function clearPasswordResetToken() |
||
102 | |||
103 | /** |
||
104 | * @return self |
||
105 | */ |
||
106 | public function generatePasswordResetToken() |
||
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getPasswordResetToken() |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getEmail() |
||
128 | |||
129 | /** |
||
130 | * @param $email |
||
131 | * |
||
132 | * @return self |
||
133 | */ |
||
134 | public function setEmail($email) |
||
140 | |||
141 | /** |
||
142 | * @param $password |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | public function setPassword($password) |
||
152 | } |
||
153 |
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.