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 |
||
| 54 | class Manager extends PublicEmitter implements IUserManager { |
||
| 55 | /** |
||
| 56 | * @var \OCP\UserInterface[] $backends |
||
| 57 | */ |
||
| 58 | private $backends = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \OC\User\User[] $cachedUsers |
||
| 62 | */ |
||
| 63 | private $cachedUsers = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \OCP\IConfig $config |
||
| 67 | */ |
||
| 68 | private $config; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param \OCP\IConfig $config |
||
| 72 | */ |
||
| 73 | public function __construct(IConfig $config = null) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the active backends |
||
| 92 | * @return \OCP\UserInterface[] |
||
| 93 | */ |
||
| 94 | public function getBackends() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * register a user backend |
||
| 100 | * |
||
| 101 | * @param \OCP\UserInterface $backend |
||
| 102 | */ |
||
| 103 | public function registerBackend($backend) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * remove a user backend |
||
| 109 | * |
||
| 110 | * @param \OCP\UserInterface $backend |
||
| 111 | */ |
||
| 112 | public function removeBackend($backend) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * remove all user backends |
||
| 121 | */ |
||
| 122 | public function clearBackends() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * get a user by user id |
||
| 129 | * |
||
| 130 | * @param string $uid |
||
| 131 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 132 | */ |
||
| 133 | public function get($uid) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * get or construct the user object |
||
| 147 | * |
||
| 148 | * @param string $uid |
||
| 149 | * @param \OCP\UserInterface $backend |
||
| 150 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 151 | * @return \OC\User\User |
||
| 152 | */ |
||
| 153 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * check if a user exists |
||
| 177 | * |
||
| 178 | * @param string $uid |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function userExists($uid) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Check if the password is valid for the user |
||
| 188 | * |
||
| 189 | * @param string $loginName |
||
| 190 | * @param string $password |
||
| 191 | * @return mixed the User object on success, false otherwise |
||
| 192 | */ |
||
| 193 | public function checkPassword($loginName, $password) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * search by user id |
||
| 212 | * |
||
| 213 | * @param string $pattern |
||
| 214 | * @param int $limit |
||
| 215 | * @param int $offset |
||
| 216 | * @return \OC\User\User[] |
||
| 217 | */ |
||
| 218 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 238 | |||
| 239 | /** |
||
| 240 | * search by displayName |
||
| 241 | * |
||
| 242 | * @param string $pattern |
||
| 243 | * @param int $limit |
||
| 244 | * @param int $offset |
||
| 245 | * @return \OC\User\User[] |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $uid |
||
| 270 | * @param string $password |
||
| 271 | * @throws \Exception |
||
| 272 | * @return bool|\OC\User\User the created user or false |
||
| 273 | */ |
||
| 274 | public function createUser($uid, $password) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * returns how many users per backend exist (if supported by backend) |
||
| 314 | * |
||
| 315 | * @return array an array of backend class as key and count number as value |
||
| 316 | */ |
||
| 317 | public function countUsers() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * The callback is executed for each user on each backend. |
||
| 341 | * If the callback returns false no further users will be retrieved. |
||
| 342 | * |
||
| 343 | * @param \Closure $callback |
||
| 344 | * @param string $search |
||
| 345 | * @since 9.0.0 |
||
| 346 | */ |
||
| 347 | public function callForAllUsers(\Closure $callback, $search = '') { |
||
| 367 | } |
||
| 368 |
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: