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 |
||
29 | class UserManipulator |
||
30 | { |
||
31 | /** |
||
32 | * User manager. |
||
33 | * |
||
34 | * @var UserManagerInterface |
||
35 | */ |
||
36 | private $userManager; |
||
37 | |||
38 | /** |
||
39 | * @var EventDispatcherInterface |
||
40 | */ |
||
41 | private $dispatcher; |
||
42 | |||
43 | /** |
||
44 | * @var RequestStack |
||
45 | */ |
||
46 | private $requestStack; |
||
47 | |||
48 | /** |
||
49 | * UserManipulator constructor. |
||
50 | * |
||
51 | * @param UserManagerInterface $userManager |
||
52 | * @param EventDispatcherInterface $dispatcher |
||
53 | * @param RequestStack $requestStack |
||
54 | */ |
||
55 | public function __construct(UserManagerInterface $userManager, EventDispatcherInterface $dispatcher, RequestStack $requestStack) |
||
61 | |||
62 | /** |
||
63 | * Creates a user and returns it. |
||
64 | * |
||
65 | * @param string $username |
||
66 | * @param string $password |
||
67 | * @param string $email |
||
68 | * @param bool $active |
||
69 | * @param bool $superadmin |
||
70 | * |
||
71 | * @return UserInterface |
||
72 | */ |
||
73 | public function create($username, $password, $email, $active, $superadmin) |
||
88 | |||
89 | /** |
||
90 | * Activates the given user. |
||
91 | * |
||
92 | * @param string $username |
||
93 | */ |
||
94 | View Code Duplication | public function activate($username) |
|
103 | |||
104 | /** |
||
105 | * Deactivates the given user. |
||
106 | * |
||
107 | * @param string $username |
||
108 | */ |
||
109 | View Code Duplication | public function deactivate($username) |
|
118 | |||
119 | /** |
||
120 | * Changes the password for the given user. |
||
121 | * |
||
122 | * @param string $username |
||
123 | * @param string $password |
||
124 | */ |
||
125 | View Code Duplication | public function changePassword($username, $password) |
|
134 | |||
135 | /** |
||
136 | * Promotes the given user. |
||
137 | * |
||
138 | * @param string $username |
||
139 | */ |
||
140 | View Code Duplication | public function promote($username) |
|
149 | |||
150 | /** |
||
151 | * Demotes the given user. |
||
152 | * |
||
153 | * @param string $username |
||
154 | */ |
||
155 | View Code Duplication | public function demote($username) |
|
164 | |||
165 | /** |
||
166 | * Adds role to the given user. |
||
167 | * |
||
168 | * @param string $username |
||
169 | * @param string $role |
||
170 | * |
||
171 | * @return bool true if role was added, false if user already had the role |
||
172 | */ |
||
173 | View Code Duplication | public function addRole($username, $role) |
|
185 | |||
186 | /** |
||
187 | * Removes role from the given user. |
||
188 | * |
||
189 | * @param string $username |
||
190 | * @param string $role |
||
191 | * |
||
192 | * @return bool true if role was removed, false if user didn't have the role |
||
193 | */ |
||
194 | View Code Duplication | public function removeRole($username, $role) |
|
206 | |||
207 | /** |
||
208 | * Finds a user by his username and throws an exception if we can't find it. |
||
209 | * |
||
210 | * @param string $username |
||
211 | * |
||
212 | * @throws InvalidArgumentException When user does not exist |
||
213 | * |
||
214 | * @return UserInterface |
||
215 | */ |
||
216 | private function findUserByUsernameOrThrowException($username) |
||
226 | |||
227 | /** |
||
228 | * @return Request |
||
229 | */ |
||
230 | private function getRequest() |
||
234 | } |
||
235 |
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.