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 |
||
13 | class UserModel implements |
||
14 | ConfigureInterface, |
||
15 | InjectionAwareInterface |
||
16 | { |
||
17 | use ConfigureTrait, |
||
18 | InjectionAwareTrait; |
||
19 | |||
20 | /** |
||
21 | * Variables |
||
22 | */ |
||
23 | //private $session; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Create a new User. |
||
28 | * The acronym is unique! |
||
29 | * |
||
30 | * @param object $newUser the values of the new user. |
||
31 | * |
||
32 | * $newUser = (object) [ |
||
33 | * acronym => name, |
||
34 | * password => hashing in function!, |
||
35 | * email => , |
||
36 | * created => timestamp in function, |
||
37 | * updated => null, |
||
38 | * deleted => null, |
||
39 | * active => null, |
||
40 | * admin => 0, |
||
41 | * ]; |
||
42 | * |
||
43 | * @return object $user if acronym unique, false otherwise. |
||
44 | */ |
||
45 | 4 | public function createUser($newUser) |
|
70 | |||
71 | |||
72 | /** |
||
73 | * Save a user to session. (Should maybe be called loginUser) |
||
74 | * |
||
75 | * @param object $user the values of the user. |
||
76 | * |
||
77 | * $user = (object) [ |
||
78 | * acronym => name, |
||
79 | * password => hashed, |
||
80 | * email => email, |
||
81 | * created => timestamp, |
||
82 | * updated => null/timestamp, |
||
83 | * deleted => null/timestamp, |
||
84 | * active => null/timestamp, |
||
85 | * admin => 0/1, |
||
86 | * ]; |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | 2 | public function saveToSession($user) |
|
104 | |||
105 | |||
106 | /** |
||
107 | * Get a user from the database |
||
108 | * |
||
109 | * @param string $key corresponding to a column in the table User |
||
110 | * @param mixed $value the value of the key |
||
111 | * |
||
112 | * @return object $user the user object |
||
113 | */ |
||
114 | 2 | public function getUserFromDatabase($key, $value) |
|
123 | |||
124 | |||
125 | /** |
||
126 | * Update a user in the database |
||
127 | * |
||
128 | * @param int $id the id of the user |
||
129 | * @param object $update user object with new values |
||
130 | * |
||
131 | * @return object $user the user object |
||
132 | */ |
||
133 | 1 | public function updateUserInDatabase($id, $update) |
|
153 | |||
154 | |||
155 | /** |
||
156 | * Logout user from session |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | 1 | public function logoutUser() |
|
173 | |||
174 | |||
175 | /** |
||
176 | * Delete user |
||
177 | * (Set $user->deleted to timestamp) |
||
178 | * |
||
179 | * @param int $id the id of the user to delete |
||
180 | * |
||
181 | * @return object $user the deleted useer |
||
182 | */ |
||
183 | 1 | View Code Duplication | public function deleteUser($id) |
194 | } |
||
195 |
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.