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 |
||
22 | class UserModel extends Model |
||
23 | { |
||
24 | /** |
||
25 | * SQL table name. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | const TABLE = 'users'; |
||
30 | |||
31 | /** |
||
32 | * Id used for everybody (filtering). |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | const EVERYBODY_ID = -1; |
||
37 | |||
38 | /** |
||
39 | * Return true if the user exists. |
||
40 | * |
||
41 | * @param int $user_id User id |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | public function exists($user_id) |
||
49 | |||
50 | /** |
||
51 | * Return true if the user is active. |
||
52 | * |
||
53 | * @param int $user_id User id |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function isActive($user_id) |
||
61 | |||
62 | /** |
||
63 | * Get query to fetch all users. |
||
64 | * |
||
65 | * @return \PicoDb\Table |
||
66 | */ |
||
67 | public function getQuery() |
||
71 | |||
72 | /** |
||
73 | * Return true is the given user id is administrator. |
||
74 | * |
||
75 | * @param int $user_id User id |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function isAdmin($user_id) |
||
88 | |||
89 | /** |
||
90 | * Get a specific user by id. |
||
91 | * |
||
92 | * @param int $user_id User id |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public function getById($user_id) |
||
100 | |||
101 | /** |
||
102 | * Get a specific user by the Google id. |
||
103 | * |
||
104 | * @param string $column |
||
105 | * @param string $id |
||
106 | * |
||
107 | * @return array|bool |
||
108 | */ |
||
109 | public function getByExternalId($column, $id) |
||
117 | |||
118 | /** |
||
119 | * Get a specific user by the username. |
||
120 | * |
||
121 | * @param string $username Username |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getByUsername($username) |
||
129 | |||
130 | /** |
||
131 | * Get user_id by username. |
||
132 | * |
||
133 | * @param string $username Username |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | public function getIdByUsername($username) |
||
141 | |||
142 | /** |
||
143 | * Get a specific user by the email address. |
||
144 | * |
||
145 | * @param string $email Email |
||
146 | * |
||
147 | * @return array|bool |
||
148 | */ |
||
149 | public function getByEmail($email) |
||
157 | |||
158 | /** |
||
159 | * Fetch user by using the token. |
||
160 | * |
||
161 | * @param string $token Token |
||
162 | * |
||
163 | * @return array|bool |
||
164 | */ |
||
165 | public function getByToken($token) |
||
173 | |||
174 | /** |
||
175 | * Get all users. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getAll() |
||
183 | |||
184 | /** |
||
185 | * Get the number of users. |
||
186 | * |
||
187 | * @return int |
||
188 | */ |
||
189 | public function count() |
||
193 | |||
194 | /** |
||
195 | * List all users (key-value pairs with id/username). |
||
196 | * |
||
197 | * @param bool $prepend Prepend "All users" |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function getActiveUsersList($prepend = false) |
||
212 | |||
213 | /** |
||
214 | * Common method to prepare a user list. |
||
215 | * |
||
216 | * @param array $users Users list (from database) |
||
217 | * |
||
218 | * @return array Formated list |
||
219 | */ |
||
220 | public function prepareList(array $users) |
||
232 | |||
233 | /** |
||
234 | * Prepare values before an update or a create. |
||
235 | * |
||
236 | * @param array $values Form values |
||
237 | */ |
||
238 | public function prepare(array &$values) |
||
253 | |||
254 | /** |
||
255 | * Add a new user in the database. |
||
256 | * |
||
257 | * @param array $values Form values |
||
258 | * |
||
259 | * @return bool|int |
||
260 | */ |
||
261 | public function create(array $values) |
||
267 | |||
268 | /** |
||
269 | * Modify a new user. |
||
270 | * |
||
271 | * @param array $values Form values |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function update(array $values) |
||
283 | |||
284 | /** |
||
285 | * Disable a specific user. |
||
286 | * |
||
287 | * @param int $user_id |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function disable($user_id) |
||
295 | |||
296 | /** |
||
297 | * Enable a specific user. |
||
298 | * |
||
299 | * @param int $user_id |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function enable($user_id) |
||
307 | |||
308 | /** |
||
309 | * Remove a specific user. |
||
310 | * |
||
311 | * @param int $user_id User id |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function remove($user_id) |
||
353 | |||
354 | /** |
||
355 | * Enable public access for a user. |
||
356 | * |
||
357 | * @param int $user_id User id |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function enablePublicAccess($user_id) |
||
368 | |||
369 | /** |
||
370 | * Disable public access for a user. |
||
371 | * |
||
372 | * @param int $user_id User id |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | public function disablePublicAccess($user_id) |
||
383 | } |
||
384 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.