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:
Complex classes like Database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
78 | class Database extends ABackend |
||
79 | implements ICreateUserBackend, |
||
80 | ISetPasswordBackend, |
||
81 | ISetDisplayNameBackend, |
||
82 | IGetDisplayNameBackend, |
||
83 | ICheckPasswordBackend, |
||
84 | IGetHomeBackend, |
||
85 | ICountUsersBackend { |
||
86 | /** @var CappedMemoryCache */ |
||
87 | private $cache; |
||
88 | |||
89 | /** @var EventDispatcher */ |
||
90 | private $eventDispatcher; |
||
91 | |||
92 | /** @var IDBConnection */ |
||
93 | private $dbConn; |
||
94 | |||
95 | /** @var string */ |
||
96 | private $table; |
||
97 | |||
98 | /** |
||
99 | * \OC\User\Database constructor. |
||
100 | * |
||
101 | * @param EventDispatcher $eventDispatcher |
||
102 | * @param string $table |
||
103 | */ |
||
104 | public function __construct($eventDispatcher = null, $table = 'users') { |
||
109 | |||
110 | /** |
||
111 | * FIXME: This function should not be required! |
||
112 | */ |
||
113 | private function fixDI() { |
||
118 | |||
119 | /** |
||
120 | * Create a new user |
||
121 | * |
||
122 | * @param string $uid The username of the user to create |
||
123 | * @param string $password The password of the new user |
||
124 | * @return bool |
||
125 | * |
||
126 | * Creates a new user. Basic checking of username is done in OC_User |
||
127 | * itself, not in its subclasses. |
||
128 | */ |
||
129 | public function createUser(string $uid, string $password): bool { |
||
154 | |||
155 | /** |
||
156 | * delete a user |
||
157 | * |
||
158 | * @param string $uid The username of the user to delete |
||
159 | * @return bool |
||
160 | * |
||
161 | * Deletes a user |
||
162 | */ |
||
163 | public function deleteUser($uid) { |
||
178 | |||
179 | private function updatePassword(string $uid, string $passwordHash): bool { |
||
188 | |||
189 | /** |
||
190 | * Set password |
||
191 | * |
||
192 | * @param string $uid The username |
||
193 | * @param string $password The new password |
||
194 | * @return bool |
||
195 | * |
||
196 | * Change the password of a user |
||
197 | */ |
||
198 | public function setPassword(string $uid, string $password): bool { |
||
213 | |||
214 | /** |
||
215 | * Set display name |
||
216 | * |
||
217 | * @param string $uid The username |
||
218 | * @param string $displayName The new display name |
||
219 | * @return bool |
||
220 | * |
||
221 | * Change the display name of a user |
||
222 | */ |
||
223 | public function setDisplayName(string $uid, string $displayName): bool { |
||
240 | |||
241 | /** |
||
242 | * get display name of the user |
||
243 | * |
||
244 | * @param string $uid user ID of the user |
||
245 | * @return string display name |
||
246 | */ |
||
247 | public function getDisplayName($uid): string { |
||
252 | |||
253 | /** |
||
254 | * Get a list of all display names and user ids. |
||
255 | * |
||
256 | * @param string $search |
||
257 | * @param string|null $limit |
||
258 | * @param string|null $offset |
||
259 | * @return array an array of all displayNames (value) and the corresponding uids (key) |
||
260 | */ |
||
261 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
||
290 | |||
291 | /** |
||
292 | * Check if the password is correct |
||
293 | * |
||
294 | * @param string $uid The username |
||
295 | * @param string $password The password |
||
296 | * @return string |
||
297 | * |
||
298 | * Check if the password is correct without logging in the user |
||
299 | * returns the user id or false |
||
300 | */ |
||
301 | public function checkPassword(string $uid, string $password) { |
||
330 | |||
331 | /** |
||
332 | * Load an user in the cache |
||
333 | * |
||
334 | * @param string $uid the username |
||
335 | * @return boolean true if user was found, false otherwise |
||
336 | */ |
||
337 | private function loadUser($uid) { |
||
373 | |||
374 | /** |
||
375 | * Get a list of all users |
||
376 | * |
||
377 | * @param string $search |
||
378 | * @param null|int $limit |
||
379 | * @param null|int $offset |
||
380 | * @return string[] an array of all uids |
||
381 | */ |
||
382 | public function getUsers($search = '', $limit = null, $offset = null) { |
||
390 | |||
391 | /** |
||
392 | * check if a user exists |
||
393 | * |
||
394 | * @param string $uid the username |
||
395 | * @return boolean |
||
396 | */ |
||
397 | public function userExists($uid) { |
||
401 | |||
402 | /** |
||
403 | * get the user's home directory |
||
404 | * |
||
405 | * @param string $uid the username |
||
406 | * @return string|false |
||
407 | */ |
||
408 | public function getHome(string $uid) { |
||
415 | |||
416 | /** |
||
417 | * @return bool |
||
418 | */ |
||
419 | public function hasUserListings() { |
||
422 | |||
423 | /** |
||
424 | * counts the users in the database |
||
425 | * |
||
426 | * @return int|bool |
||
427 | */ |
||
428 | public function countUsers() { |
||
438 | |||
439 | /** |
||
440 | * returns the username for the given login name in the correct casing |
||
441 | * |
||
442 | * @param string $loginName |
||
443 | * @return string|false |
||
444 | */ |
||
445 | public function loginName2UserName($loginName) { |
||
452 | |||
453 | /** |
||
454 | * Backend name to be shown in user management |
||
455 | * |
||
456 | * @return string the name of the backend to be shown |
||
457 | */ |
||
458 | public function getBackendName() { |
||
461 | |||
462 | public static function preLoginNameUsedAsUserName($param) { |
||
480 | } |
||
481 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.