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 User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class User implements IUser { |
||
51 | use EventEmitterTrait, EmptyStringTrait; |
||
52 | |||
53 | /** @var Account */ |
||
54 | private $account; |
||
55 | |||
56 | /** @var Emitter|Manager $emitter */ |
||
57 | private $emitter; |
||
58 | |||
59 | /** @var \OCP\IConfig $config */ |
||
60 | private $config; |
||
61 | |||
62 | /** @var IAvatarManager */ |
||
63 | private $avatarManager; |
||
64 | |||
65 | /** @var IURLGenerator */ |
||
66 | private $urlGenerator; |
||
67 | |||
68 | /** @var EventDispatcher */ |
||
69 | private $eventDispatcher; |
||
70 | |||
71 | /** @var AccountMapper */ |
||
72 | private $mapper; |
||
73 | |||
74 | /** @var \OC\Group\Manager */ |
||
75 | private $groupManager; |
||
76 | |||
77 | /** @var Session */ |
||
78 | private $userSession; |
||
79 | |||
80 | /** |
||
81 | * User constructor. |
||
82 | * |
||
83 | * @param Account $account |
||
84 | * @param AccountMapper $mapper |
||
85 | * @param null $emitter |
||
86 | * @param IConfig|null $config |
||
87 | * @param null $urlGenerator |
||
88 | * @param EventDispatcher|null $eventDispatcher |
||
89 | * @param \OC\Group\Manager|null $groupManager |
||
90 | * @param Session|null $userSession |
||
91 | */ |
||
92 | public function __construct(Account $account, AccountMapper $mapper, $emitter = null, IConfig $config = null, |
||
117 | |||
118 | /** |
||
119 | * get the user id |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getUID() { |
||
126 | |||
127 | /** |
||
128 | * get the display name for the user, if no specific display name is set it will fallback to the user id |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getDisplayName() { |
||
139 | |||
140 | /** |
||
141 | * set the displayname for the user |
||
142 | * |
||
143 | * @param string $displayName |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function setDisplayName($displayName) { |
||
166 | |||
167 | /** |
||
168 | * set the email address of the user |
||
169 | * |
||
170 | * @param string|null $mailAddress |
||
171 | * @return void |
||
172 | * @since 9.0.0 |
||
173 | */ |
||
174 | public function setEMailAddress($mailAddress) { |
||
183 | |||
184 | /** |
||
185 | * returns the timestamp of the user's last login or 0 if the user did never |
||
186 | * login |
||
187 | * |
||
188 | * @return int |
||
189 | */ |
||
190 | public function getLastLogin() { |
||
193 | |||
194 | /** |
||
195 | * updates the timestamp of the most recent login of this user |
||
196 | */ |
||
197 | public function updateLastLoginTimestamp() { |
||
203 | |||
204 | /** |
||
205 | * Delete the user |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function delete() { |
||
254 | |||
255 | /** |
||
256 | * Set the user's password |
||
257 | * |
||
258 | * @param string $password |
||
259 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
260 | * @return bool |
||
261 | * @throws \InvalidArgumentException |
||
262 | */ |
||
263 | public function setPassword($password, $recoveryPassword = null) { |
||
295 | |||
296 | /** |
||
297 | * get the users home folder to mount |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | public function getHome() { |
||
304 | |||
305 | /** |
||
306 | * Get the name of the backend class the user is connected with |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getBackendClassName() { |
||
317 | |||
318 | /** |
||
319 | * check if the backend allows the user to change his avatar on Personal page |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function canChangeAvatar() { |
||
333 | |||
334 | /** |
||
335 | * check if the backend supports changing passwords |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | public function canChangePassword() { |
||
346 | |||
347 | /** |
||
348 | * check if the backend supports changing display names |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function canChangeDisplayName() { |
||
370 | |||
371 | /** |
||
372 | * check if the user is enabled |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | public function isEnabled() { |
||
379 | |||
380 | /** |
||
381 | * set the enabled status for the user |
||
382 | * |
||
383 | * @param bool $enabled |
||
384 | */ |
||
385 | public function setEnabled($enabled) { |
||
397 | |||
398 | /** |
||
399 | * get the users email address |
||
400 | * |
||
401 | * @return string|null |
||
402 | * @since 9.0.0 |
||
403 | */ |
||
404 | public function getEMailAddress() { |
||
407 | |||
408 | /** |
||
409 | * get the users' quota |
||
410 | * |
||
411 | * @return string |
||
412 | * @since 9.0.0 |
||
413 | */ |
||
414 | public function getQuota() { |
||
421 | |||
422 | /** |
||
423 | * set the users' quota |
||
424 | * |
||
425 | * @param string $quota |
||
426 | * @return void |
||
427 | * @since 9.0.0 |
||
428 | */ |
||
429 | public function setQuota($quota) { |
||
438 | |||
439 | /** |
||
440 | * get the avatar image if it exists |
||
441 | * |
||
442 | * @param int $size |
||
443 | * @return IImage|null |
||
444 | * @since 9.0.0 |
||
445 | */ |
||
446 | public function getAvatarImage($size) { |
||
460 | |||
461 | /** |
||
462 | * get the federation cloud id |
||
463 | * |
||
464 | * @return string |
||
465 | * @since 9.0.0 |
||
466 | */ |
||
467 | public function getCloudId() { |
||
472 | |||
473 | /** |
||
474 | * @param string $url |
||
475 | * @return string |
||
476 | */ |
||
477 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
486 | |||
487 | public function triggerChange($feature, $value = null) { |
||
492 | |||
493 | /** |
||
494 | * @return string[] |
||
495 | * @since 10.0.1 |
||
496 | */ |
||
497 | public function getSearchTerms() { |
||
504 | |||
505 | /** |
||
506 | * @param string[] $terms |
||
507 | * @since 10.0.1 |
||
508 | */ |
||
509 | public function setSearchTerms(array $terms) { |
||
516 | } |
||
517 |
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: