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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class Manager extends PublicEmitter implements IUserManager { |
||
58 | /** |
||
59 | * @var \OCP\UserInterface[] $backends |
||
60 | */ |
||
61 | private $backends = array(); |
||
62 | |||
63 | /** |
||
64 | * @var \OC\User\User[] $cachedUsers |
||
65 | */ |
||
66 | private $cachedUsers = array(); |
||
67 | |||
68 | /** |
||
69 | * @var \OCP\IConfig $config |
||
70 | */ |
||
71 | private $config; |
||
72 | |||
73 | /** |
||
74 | * @param \OCP\IConfig $config |
||
75 | */ |
||
76 | public function __construct(IConfig $config) { |
||
84 | |||
85 | /** |
||
86 | * Get the active backends |
||
87 | * @return \OCP\UserInterface[] |
||
88 | */ |
||
89 | public function getBackends() { |
||
92 | |||
93 | /** |
||
94 | * register a user backend |
||
95 | * |
||
96 | * @param \OCP\UserInterface $backend |
||
97 | */ |
||
98 | public function registerBackend($backend) { |
||
101 | |||
102 | /** |
||
103 | * remove a user backend |
||
104 | * |
||
105 | * @param \OCP\UserInterface $backend |
||
106 | */ |
||
107 | public function removeBackend($backend) { |
||
113 | |||
114 | /** |
||
115 | * remove all user backends |
||
116 | */ |
||
117 | public function clearBackends() { |
||
121 | |||
122 | /** |
||
123 | * get a user by user id |
||
124 | * |
||
125 | * @param string $uid |
||
126 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
127 | */ |
||
128 | public function get($uid) { |
||
139 | |||
140 | /** |
||
141 | * get or construct the user object |
||
142 | * |
||
143 | * @param string $uid |
||
144 | * @param \OCP\UserInterface $backend |
||
145 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
146 | * @return \OC\User\User |
||
147 | */ |
||
148 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
169 | |||
170 | /** |
||
171 | * check if a user exists |
||
172 | * |
||
173 | * @param string $uid |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function userExists($uid) { |
||
180 | |||
181 | /** |
||
182 | * Check if the password is valid for the user |
||
183 | * |
||
184 | * @param string $loginName |
||
185 | * @param string $password |
||
186 | * @return mixed the User object on success, false otherwise |
||
187 | */ |
||
188 | public function checkPassword($loginName, $password) { |
||
197 | |||
198 | /** |
||
199 | * Check if the password is valid for the user |
||
200 | * |
||
201 | * @internal |
||
202 | * @param string $loginName |
||
203 | * @param string $password |
||
204 | * @return mixed the User object on success, false otherwise |
||
205 | */ |
||
206 | public function checkPasswordNoLogging($loginName, $password) { |
||
207 | $loginName = str_replace("\0", '', $loginName); |
||
208 | $password = str_replace("\0", '', $password); |
||
209 | |||
210 | View Code Duplication | foreach ($this->backends as $backend) { |
|
211 | if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
||
212 | $uid = $backend->checkPassword($loginName, $password); |
||
213 | if ($uid !== false) { |
||
214 | return $this->getUserObject($uid, $backend); |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | |||
219 | return false; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * search by user id |
||
224 | * |
||
225 | * @param string $pattern |
||
226 | * @param int $limit |
||
227 | * @param int $offset |
||
228 | * @return \OC\User\User[] |
||
229 | */ |
||
230 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
250 | |||
251 | /** |
||
252 | * search by displayName |
||
253 | * |
||
254 | * @param string $pattern |
||
255 | * @param int $limit |
||
256 | * @param int $offset |
||
257 | * @return \OC\User\User[] |
||
258 | */ |
||
259 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
279 | |||
280 | /** |
||
281 | * @param string $uid |
||
282 | * @param string $password |
||
283 | * @throws \InvalidArgumentException |
||
284 | * @return bool|IUser the created user or false |
||
285 | */ |
||
286 | public function createUser($uid, $password) { |
||
308 | |||
309 | /** |
||
310 | * @param string $uid |
||
311 | * @param string $password |
||
312 | * @param UserInterface $backend |
||
313 | * @return IUser|null |
||
314 | * @throws \InvalidArgumentException |
||
315 | */ |
||
316 | public function createUserFromBackend($uid, $password, UserInterface $backend) { |
||
355 | |||
356 | /** |
||
357 | * returns how many users per backend exist (if supported by backend) |
||
358 | * |
||
359 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
360 | * entry in the preferences table will be affected |
||
361 | * @return array|int an array of backend class as key and count number as value |
||
362 | * if $hasLoggedIn is true only an int is returned |
||
363 | */ |
||
364 | public function countUsers($hasLoggedIn = false) { |
||
388 | |||
389 | /** |
||
390 | * The callback is executed for each user on each backend. |
||
391 | * If the callback returns false no further users will be retrieved. |
||
392 | * |
||
393 | * @param \Closure $callback |
||
394 | * @param string $search |
||
395 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
396 | * in the preferences table will be affected |
||
397 | * @since 9.0.0 |
||
398 | */ |
||
399 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
423 | |||
424 | /** |
||
425 | * returns how many users have logged in once |
||
426 | * |
||
427 | * @return int |
||
428 | * @since 12.0.0 |
||
429 | */ |
||
430 | View Code Duplication | public function countDisabledUsers() { |
|
445 | |||
446 | /** |
||
447 | * returns how many users have logged in once |
||
448 | * |
||
449 | * @return int |
||
450 | * @since 11.0.0 |
||
451 | */ |
||
452 | View Code Duplication | public function countSeenUsers() { |
|
467 | |||
468 | /** |
||
469 | * @param \Closure $callback |
||
470 | * @since 11.0.0 |
||
471 | */ |
||
472 | public function callForSeenUsers(\Closure $callback) { |
||
491 | |||
492 | /** |
||
493 | * Getting all userIds that have a listLogin value requires checking the |
||
494 | * value in php because on oracle you cannot use a clob in a where clause, |
||
495 | * preventing us from doing a not null or length(value) > 0 check. |
||
496 | * |
||
497 | * @param int $limit |
||
498 | * @param int $offset |
||
499 | * @return string[] with user ids |
||
500 | */ |
||
501 | private function getSeenUserIds($limit = null, $offset = null) { |
||
531 | |||
532 | /** |
||
533 | * @param string $email |
||
534 | * @return IUser[] |
||
535 | * @since 9.1.0 |
||
536 | */ |
||
537 | public function getByEmail($email) { |
||
544 | } |
||
545 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: