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 |
||
| 56 | class Manager extends PublicEmitter implements IUserManager { |
||
| 57 | /** |
||
| 58 | * @var \OCP\UserInterface[] $backends |
||
| 59 | */ |
||
| 60 | private $backends = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \OC\User\User[] $cachedUsers |
||
| 64 | */ |
||
| 65 | private $cachedUsers = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \OCP\IConfig $config |
||
| 69 | */ |
||
| 70 | private $config; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param \OCP\IConfig $config |
||
| 74 | */ |
||
| 75 | public function __construct(IConfig $config = null) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the active backends |
||
| 94 | * @return \OCP\UserInterface[] |
||
| 95 | */ |
||
| 96 | public function getBackends() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * register a user backend |
||
| 102 | * |
||
| 103 | * @param \OCP\UserInterface $backend |
||
| 104 | */ |
||
| 105 | public function registerBackend($backend) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * remove a user backend |
||
| 111 | * |
||
| 112 | * @param \OCP\UserInterface $backend |
||
| 113 | */ |
||
| 114 | public function removeBackend($backend) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * remove all user backends |
||
| 123 | */ |
||
| 124 | public function clearBackends() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * get a user by user id |
||
| 131 | * |
||
| 132 | * @param string $uid |
||
| 133 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 134 | */ |
||
| 135 | public function get($uid) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * get or construct the user object |
||
| 149 | * |
||
| 150 | * @param string $uid |
||
| 151 | * @param \OCP\UserInterface $backend |
||
| 152 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 153 | * @return \OC\User\User |
||
| 154 | */ |
||
| 155 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * check if a user exists |
||
| 179 | * |
||
| 180 | * @param string $uid |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function userExists($uid) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Check if the password is valid for the user |
||
| 190 | * |
||
| 191 | * @param string $loginName |
||
| 192 | * @param string $password |
||
| 193 | * @return mixed the User object on success, false otherwise |
||
| 194 | */ |
||
| 195 | public function checkPassword($loginName, $password) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * search by user id |
||
| 214 | * |
||
| 215 | * @param string $pattern |
||
| 216 | * @param int $limit |
||
| 217 | * @param int $offset |
||
| 218 | * @return \OC\User\User[] |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * search by displayName |
||
| 243 | * |
||
| 244 | * @param string $pattern |
||
| 245 | * @param int $limit |
||
| 246 | * @param int $offset |
||
| 247 | * @return \OC\User\User[] |
||
| 248 | */ |
||
| 249 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $uid |
||
| 272 | * @param string $password |
||
| 273 | * @throws \Exception |
||
| 274 | * @return bool|\OC\User\User the created user or false |
||
| 275 | */ |
||
| 276 | public function createUser($uid, $password) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * returns how many users per backend exist (if supported by backend) |
||
| 316 | * |
||
| 317 | * @return array an array of backend class as key and count number as value |
||
| 318 | */ |
||
| 319 | public function countUsers() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * The callback is executed for each user on each backend. |
||
| 343 | * If the callback returns false no further users will be retrieved. |
||
| 344 | * |
||
| 345 | * @param \Closure $callback |
||
| 346 | * @param string $search |
||
| 347 | * @since 9.0.0 |
||
| 348 | */ |
||
| 349 | public function callForAllUsers(\Closure $callback, $search = '') { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $email |
||
| 372 | * @return IUser[] |
||
| 373 | * @since 9.1.0 |
||
| 374 | */ |
||
| 375 | public function getByEmail($email) { |
||
| 382 | } |
||
| 383 |
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: