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 |
||
21 | class AdminUserService |
||
22 | { |
||
23 | /** |
||
24 | * @var Bcrypt |
||
25 | */ |
||
26 | private $crypt; |
||
27 | |||
28 | /** |
||
29 | * @var AdminUsersMapper |
||
30 | */ |
||
31 | private $adminUsersMapper; |
||
32 | |||
33 | /** |
||
34 | * @var AdminUserFilter |
||
35 | */ |
||
36 | private $adminUserFilter; |
||
37 | |||
38 | /** |
||
39 | * @var Upload |
||
40 | */ |
||
41 | private $upload; |
||
42 | |||
43 | /** |
||
44 | * AdminUserService constructor. |
||
45 | * |
||
46 | * @param Bcrypt $crypt bcrypt password encryption method |
||
47 | * @param AdminUsersMapper $adminUsersMapper mapper for admin us |
||
48 | * @param AdminUserFilter $adminUserFilter |
||
49 | * @param Upload $upload |
||
50 | */ |
||
51 | public function __construct( |
||
62 | |||
63 | /** |
||
64 | * Performs user login or throws exception if credentials are not valid. |
||
65 | * |
||
66 | * @param string $email user email |
||
67 | * @param string $password user password |
||
68 | * |
||
69 | * @throws \Exception if user does not exist or password is not valid |
||
70 | * |
||
71 | * @return array|\ArrayObject|null |
||
72 | */ |
||
73 | public function loginUser($email, $password) |
||
93 | |||
94 | /** |
||
95 | * Return pagination object to paginate results on view. |
||
96 | * |
||
97 | * @param int $page Current page set to pagination to display |
||
98 | * @param int $limit Limit set to pagination |
||
99 | * @param string $userId UUID from DB |
||
100 | * |
||
101 | * @return Paginator |
||
102 | */ |
||
103 | View Code Duplication | public function getPagination($page, $limit, $userId) |
|
114 | |||
115 | /** |
||
116 | * Return one user for given UUID. |
||
117 | * |
||
118 | * @param string $userId UUID from DB |
||
119 | * |
||
120 | * @return array|\ArrayObject|null |
||
121 | */ |
||
122 | public function getUser($userId) |
||
126 | |||
127 | public function registerNewUser($data) |
||
147 | |||
148 | /** |
||
149 | * Refactor it. |
||
150 | */ |
||
151 | public function updateUser($data, $userId) |
||
196 | |||
197 | /** |
||
198 | * Delete user by given UUID. |
||
199 | * |
||
200 | * @param $userId |
||
201 | * |
||
202 | * @throws \Exception |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function delete($userId) |
||
217 | |||
218 | /** |
||
219 | * Fetch random X users to show on homepage. |
||
220 | * |
||
221 | * @param int $limit |
||
222 | * |
||
223 | * @return null|\Zend\Db\ResultSet\ResultSetInterface |
||
224 | */ |
||
225 | public function getForWeb($limit = 10) |
||
229 | |||
230 | /** |
||
231 | * Return all Admin User for select box. |
||
232 | * |
||
233 | * @return \Zend\Db\ResultSet\ResultSet |
||
234 | */ |
||
235 | public function getAll() |
||
239 | } |
||
240 |
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.