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 |
||
11 | class UsersService extends Base |
||
12 | { |
||
13 | /** |
||
14 | * Constructor of the class. |
||
15 | * |
||
16 | * @param object $database |
||
17 | */ |
||
18 | public function __construct(\PDO $database) |
||
22 | |||
23 | /** |
||
24 | * Check if the user exists. |
||
25 | * |
||
26 | * @param int $userId |
||
27 | * @return object $user |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | public function checkUser($userId) |
||
43 | |||
44 | /** |
||
45 | * Get all users. |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function getUsers() |
||
58 | |||
59 | /** |
||
60 | * Get one user by id. |
||
61 | * |
||
62 | * @param int $userId |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getUser($userId) |
||
71 | |||
72 | /** |
||
73 | * Search users by name. |
||
74 | * |
||
75 | * @param string $str |
||
76 | * @return array |
||
77 | * @throws \Exception |
||
78 | */ |
||
79 | View Code Duplication | public function searchUsers($str) |
|
94 | |||
95 | /** |
||
96 | * Create a user. |
||
97 | * |
||
98 | * @param array $input |
||
99 | * @return array |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | public function createUser($input) |
||
117 | |||
118 | /** |
||
119 | * Update a user. |
||
120 | * |
||
121 | * @param array $input |
||
122 | * @param int $userId |
||
123 | * @return array |
||
124 | * @throws \Exception |
||
125 | */ |
||
126 | public function updateUser($input, $userId) |
||
142 | |||
143 | /** |
||
144 | * Delete a user. |
||
145 | * |
||
146 | * @param int $userId |
||
147 | * @return array |
||
148 | */ |
||
149 | public function deleteUser($userId) |
||
160 | } |
||
161 |