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 |
||
| 55 | class Manager extends PublicEmitter implements IUserManager { |
||
| 56 | /** |
||
| 57 | * @var \OCP\UserInterface[] $backends |
||
| 58 | */ |
||
| 59 | private $backends = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \OC\User\User[] $cachedUsers |
||
| 63 | */ |
||
| 64 | private $cachedUsers = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \OCP\IConfig $config |
||
| 68 | */ |
||
| 69 | private $config; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param \OCP\IConfig $config |
||
| 73 | */ |
||
| 74 | public function __construct(IConfig $config = null) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the active backends |
||
| 93 | * @return \OCP\UserInterface[] |
||
| 94 | */ |
||
| 95 | public function getBackends() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * register a user backend |
||
| 101 | * |
||
| 102 | * @param \OCP\UserInterface $backend |
||
| 103 | */ |
||
| 104 | public function registerBackend($backend) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * remove a user backend |
||
| 110 | * |
||
| 111 | * @param \OCP\UserInterface $backend |
||
| 112 | */ |
||
| 113 | public function removeBackend($backend) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * remove all user backends |
||
| 122 | */ |
||
| 123 | public function clearBackends() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * get a user by user id |
||
| 130 | * |
||
| 131 | * @param string $uid |
||
| 132 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 133 | */ |
||
| 134 | public function get($uid) { |
||
| 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) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * search by user id |
||
| 203 | * |
||
| 204 | * @param string $pattern |
||
| 205 | * @param int $limit |
||
| 206 | * @param int $offset |
||
| 207 | * @return \OC\User\User[] |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * search by displayName |
||
| 232 | * |
||
| 233 | * @param string $pattern |
||
| 234 | * @param int $limit |
||
| 235 | * @param int $offset |
||
| 236 | * @return \OC\User\User[] |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $uid |
||
| 261 | * @param string $password |
||
| 262 | * @throws \Exception |
||
| 263 | * @return bool|\OC\User\User the created user or false |
||
| 264 | */ |
||
| 265 | public function createUser($uid, $password) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * returns how many users per backend exist (if supported by backend) |
||
| 305 | * |
||
| 306 | * @return array an array of backend class as key and count number as value |
||
| 307 | */ |
||
| 308 | public function countUsers() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * The callback is executed for each user on each backend. |
||
| 332 | * If the callback returns false no further users will be retrieved. |
||
| 333 | * |
||
| 334 | * @param \Closure $callback |
||
| 335 | * @param string $search |
||
| 336 | * @since 9.0.0 |
||
| 337 | */ |
||
| 338 | public function callForAllUsers(\Closure $callback, $search = '') { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $email |
||
| 361 | * @return IUser[] |
||
| 362 | * @since 9.1.0 |
||
| 363 | */ |
||
| 364 | public function getByEmail($email) { |
||
| 371 | } |
||
| 372 |
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: