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 |
||
60 | class Manager extends PublicEmitter implements IUserManager { |
||
61 | /** |
||
62 | * @var \OCP\UserInterface[] $backends |
||
63 | */ |
||
64 | private $backends = array(); |
||
65 | |||
66 | /** |
||
67 | * @var \OC\User\User[] $cachedUsers |
||
68 | */ |
||
69 | private $cachedUsers = array(); |
||
70 | |||
71 | /** |
||
72 | * @var \OCP\IConfig $config |
||
73 | */ |
||
74 | private $config; |
||
75 | |||
76 | /** |
||
77 | * @param \OCP\IConfig $config |
||
78 | */ |
||
79 | public function __construct(IConfig $config) { |
||
80 | $this->config = $config; |
||
81 | $cachedUsers = &$this->cachedUsers; |
||
82 | $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
||
83 | /** @var \OC\User\User $user */ |
||
84 | unset($cachedUsers[$user->getUID()]); |
||
85 | }); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get the active backends |
||
90 | * @return \OCP\UserInterface[] |
||
91 | */ |
||
92 | public function getBackends() { |
||
93 | return $this->backends; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * register a user backend |
||
98 | * |
||
99 | * @param \OCP\UserInterface $backend |
||
100 | */ |
||
101 | public function registerBackend($backend) { |
||
102 | $this->backends[] = $backend; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * remove a user backend |
||
107 | * |
||
108 | * @param \OCP\UserInterface $backend |
||
109 | */ |
||
110 | public function removeBackend($backend) { |
||
111 | $this->cachedUsers = array(); |
||
112 | if (($i = array_search($backend, $this->backends)) !== false) { |
||
113 | unset($this->backends[$i]); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * remove all user backends |
||
119 | */ |
||
120 | public function clearBackends() { |
||
121 | $this->cachedUsers = array(); |
||
122 | $this->backends = array(); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * get a user by user id |
||
127 | * |
||
128 | * @param string $uid |
||
129 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
130 | */ |
||
131 | public function get($uid) { |
||
132 | if (is_null($uid) || $uid === '' || $uid === false) { |
||
133 | return null; |
||
134 | } |
||
135 | if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
||
136 | return $this->cachedUsers[$uid]; |
||
137 | } |
||
138 | foreach ($this->backends as $backend) { |
||
139 | if ($backend->userExists($uid)) { |
||
140 | return $this->getUserObject($uid, $backend); |
||
141 | } |
||
142 | } |
||
143 | return null; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * get or construct the user object |
||
148 | * |
||
149 | * @param string $uid |
||
150 | * @param \OCP\UserInterface $backend |
||
151 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
152 | * @return \OC\User\User |
||
153 | */ |
||
154 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
165 | |||
166 | /** |
||
167 | * check if a user exists |
||
168 | * |
||
169 | * @param string $uid |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function userExists($uid) { |
||
176 | |||
177 | /** |
||
178 | * Check if the password is valid for the user |
||
179 | * |
||
180 | * @param string $loginName |
||
181 | * @param string $password |
||
182 | * @return mixed the User object on success, false otherwise |
||
183 | */ |
||
184 | public function checkPassword($loginName, $password) { |
||
193 | |||
194 | /** |
||
195 | * Check if the password is valid for the user |
||
196 | * |
||
197 | * @internal |
||
198 | * @param string $loginName |
||
199 | * @param string $password |
||
200 | * @return mixed the User object on success, false otherwise |
||
201 | */ |
||
202 | public function checkPasswordNoLogging($loginName, $password) { |
||
217 | |||
218 | /** |
||
219 | * search by user id |
||
220 | * |
||
221 | * @param string $pattern |
||
222 | * @param int $limit |
||
223 | * @param int $offset |
||
224 | * @return \OC\User\User[] |
||
225 | */ |
||
226 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
246 | |||
247 | /** |
||
248 | * search by displayName |
||
249 | * |
||
250 | * @param string $pattern |
||
251 | * @param int $limit |
||
252 | * @param int $offset |
||
253 | * @return \OC\User\User[] |
||
254 | */ |
||
255 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
275 | |||
276 | /** |
||
277 | * @param string $uid |
||
278 | * @param string $password |
||
279 | * @throws \InvalidArgumentException |
||
280 | * @return bool|IUser the created user or false |
||
281 | */ |
||
282 | public function createUser($uid, $password) { |
||
304 | |||
305 | /** |
||
306 | * @param string $uid |
||
307 | * @param string $password |
||
308 | * @param UserInterface $backend |
||
309 | * @return IUser|null |
||
310 | * @throws \InvalidArgumentException |
||
311 | */ |
||
312 | public function createUserFromBackend($uid, $password, UserInterface $backend) { |
||
354 | |||
355 | /** |
||
356 | * returns how many users per backend exist (if supported by backend) |
||
357 | * |
||
358 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
359 | * entry in the preferences table will be affected |
||
360 | * @return array|int an array of backend class as key and count number as value |
||
361 | * if $hasLoggedIn is true only an int is returned |
||
362 | */ |
||
363 | public function countUsers($hasLoggedIn = false) { |
||
387 | |||
388 | /** |
||
389 | * returns how many users per backend exist in the requested groups (if supported by backend) |
||
390 | * |
||
391 | * @param IGroup[] $groups an array of gid to search in |
||
392 | * @return array|int an array of backend class as key and count number as value |
||
393 | * if $hasLoggedIn is true only an int is returned |
||
394 | */ |
||
395 | public function countUsersOfGroups(array $groups) { |
||
405 | |||
406 | /** |
||
407 | * The callback is executed for each user on each backend. |
||
408 | * If the callback returns false no further users will be retrieved. |
||
409 | * |
||
410 | * @param \Closure $callback |
||
411 | * @param string $search |
||
412 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
413 | * in the preferences table will be affected |
||
414 | * @since 9.0.0 |
||
415 | */ |
||
416 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
440 | |||
441 | /** |
||
442 | * returns how many users are disabled |
||
443 | * |
||
444 | * @return int |
||
445 | * @since 12.0.0 |
||
446 | */ |
||
447 | public function countDisabledUsers(): int { |
||
468 | |||
469 | /** |
||
470 | * returns how many users are disabled in the requested groups |
||
471 | * |
||
472 | * @param array $groups groupids to search |
||
473 | * @return int |
||
474 | * @since 14.0.0 |
||
475 | */ |
||
476 | View Code Duplication | public function countDisabledUsersOfGroups(array $groups): int { |
|
498 | |||
499 | /** |
||
500 | * returns how many users have logged in once |
||
501 | * |
||
502 | * @return int |
||
503 | * @since 11.0.0 |
||
504 | */ |
||
505 | public function countSeenUsers() { |
||
520 | |||
521 | /** |
||
522 | * @param \Closure $callback |
||
523 | * @since 11.0.0 |
||
524 | */ |
||
525 | public function callForSeenUsers(\Closure $callback) { |
||
545 | |||
546 | /** |
||
547 | * Getting all userIds that have a listLogin value requires checking the |
||
548 | * value in php because on oracle you cannot use a clob in a where clause, |
||
549 | * preventing us from doing a not null or length(value) > 0 check. |
||
550 | * |
||
551 | * @param int $limit |
||
552 | * @param int $offset |
||
553 | * @return string[] with user ids |
||
554 | */ |
||
555 | private function getSeenUserIds($limit = null, $offset = null) { |
||
585 | |||
586 | /** |
||
587 | * @param string $email |
||
588 | * @return IUser[] |
||
589 | * @since 9.1.0 |
||
590 | */ |
||
591 | public function getByEmail($email) { |
||
598 | } |
||
599 |
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: