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 OC_User_Database 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 OC_User_Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class OC_User_Database extends OC_User_Backend implements \OCP\IUserBackend { |
||
| 57 | /** @var CappedMemoryCache */ |
||
| 58 | private $cache; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * OC_User_Database constructor. |
||
| 62 | */ |
||
| 63 | public function __construct() { |
||
| 66 | 103 | ||
| 67 | 103 | /** |
|
| 68 | 103 | * Create a new user |
|
| 69 | 103 | * @param string $uid The username of the user to create |
|
| 70 | * @param string $password The password of the new user |
||
| 71 | 103 | * @return bool |
|
| 72 | * |
||
| 73 | * Creates a new user. Basic checking of username is done in OC_User |
||
| 74 | * itself, not in its subclasses. |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function createUser($uid, $password) { |
|
| 86 | 101 | ||
| 87 | 101 | /** |
|
| 88 | * delete a user |
||
| 89 | 101 | * @param string $uid The username of the user to delete |
|
| 90 | 73 | * @return bool |
|
| 91 | 73 | * |
|
| 92 | * Deletes a user |
||
| 93 | 101 | */ |
|
| 94 | View Code Duplication | public function deleteUser($uid) { |
|
| 105 | 3 | ||
| 106 | 3 | /** |
|
| 107 | 3 | * Set password |
|
| 108 | * @param string $uid The username |
||
| 109 | 3 | * @param string $password The new password |
|
| 110 | * @return bool |
||
| 111 | * |
||
| 112 | * Change the password of a user |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function setPassword($uid, $password) { |
|
| 124 | 3 | ||
| 125 | 3 | /** |
|
| 126 | 3 | * Set display name |
|
| 127 | 3 | * @param string $uid The username |
|
| 128 | * @param string $displayName The new display name |
||
| 129 | 3 | * @return bool |
|
| 130 | * |
||
| 131 | * Change the display name of a user |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function setDisplayName($uid, $displayName) { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * get display name of the user |
||
| 147 | * @param string $uid user ID of the user |
||
| 148 | * @return string display name |
||
| 149 | */ |
||
| 150 | public function getDisplayName($uid) { |
||
| 154 | 1 | ||
| 155 | 1 | /** |
|
| 156 | 1 | * Get a list of all display names and user ids. |
|
| 157 | 1 | * |
|
| 158 | 1 | * @param string $search |
|
| 159 | * @param string|null $limit |
||
| 160 | 1 | * @param string|null $offset |
|
| 161 | 1 | * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 162 | */ |
||
| 163 | 1 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 183 | 3 | ||
| 184 | 3 | /** |
|
| 185 | 3 | * Check if the password is correct |
|
| 186 | * @param string $uid The username |
||
| 187 | 3 | * @param string $password The password |
|
| 188 | 3 | * @return string |
|
| 189 | 2 | * |
|
| 190 | 2 | * Check if the password is correct without logging in the user |
|
| 191 | 2 | * returns the user id or false |
|
| 192 | 2 | */ |
|
| 193 | public function checkPassword($uid, $password) { |
||
| 212 | |||
| 213 | 142 | /** |
|
| 214 | * Load an user in the cache |
||
| 215 | * @param string $uid the username |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | 142 | private function loadUser($uid) { |
|
| 236 | 3 | ||
| 237 | 3 | /** |
|
| 238 | 3 | * Get a list of all users |
|
| 239 | 2 | * |
|
| 240 | 2 | * @param string $search |
|
| 241 | 2 | * @param null|int $limit |
|
| 242 | * @param null|int $offset |
||
| 243 | 3 | * @return string[] an array of all uids |
|
| 244 | 3 | */ |
|
| 245 | 3 | View Code Duplication | public function getUsers($search = '', $limit = null, $offset = null) { |
| 261 | |||
| 262 | /** |
||
| 263 | * check if a user exists |
||
| 264 | * @param string $uid the username |
||
| 265 | * @return boolean |
||
| 266 | */ |
||
| 267 | 99 | public function userExists($uid) { |
|
| 271 | |||
| 272 | 44 | /** |
|
| 273 | * get the user's home directory |
||
| 274 | * @param string $uid the username |
||
| 275 | * @return string|false |
||
| 276 | */ |
||
| 277 | public function getHome($uid) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function hasUserListings() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * counts the users in the database |
||
| 294 | * |
||
| 295 | * @return int|bool |
||
| 296 | */ |
||
| 297 | public function countUsers() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * returns the username for the given login name in the correct casing |
||
| 309 | * |
||
| 310 | * @param string $loginName |
||
| 311 | * @return string|false |
||
| 312 | */ |
||
| 313 | public function loginName2UserName($loginName) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Backend name to be shown in user management |
||
| 323 | * @return string the name of the backend to be shown |
||
| 324 | */ |
||
| 325 | public function getBackendName(){ |
||
| 328 | |||
| 329 | public static function preLoginNameUsedAsUserName($param) { |
||
| 347 | } |
||
| 348 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.