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:
| 1 | <?php |
||
| 49 | class Manager extends PublicEmitter implements IUserManager { |
||
| 50 | /** |
||
| 51 | * @var \OCP\UserInterface [] $backends |
||
| 52 | */ |
||
| 53 | private $backends = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \OC\User\User[] $cachedUsers |
||
| 57 | */ |
||
| 58 | private $cachedUsers = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \OCP\IConfig $config |
||
| 62 | */ |
||
| 63 | private $config; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param \OCP\IConfig $config |
||
| 67 | */ |
||
| 68 | 816 | public function __construct(IConfig $config = null) { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Get the active backends |
||
| 87 | * @return \OCP\UserInterface[] |
||
| 88 | */ |
||
| 89 | 1 | public function getBackends() { |
|
| 92 | |||
| 93 | /** |
||
| 94 | * register a user backend |
||
| 95 | * |
||
| 96 | * @param \OCP\UserInterface $backend |
||
| 97 | */ |
||
| 98 | 744 | public function registerBackend($backend) { |
|
| 101 | |||
| 102 | /** |
||
| 103 | * remove a user backend |
||
| 104 | * |
||
| 105 | * @param \OCP\UserInterface $backend |
||
| 106 | */ |
||
| 107 | 211 | public function removeBackend($backend) { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * remove all user backends |
||
| 116 | */ |
||
| 117 | 435 | public function clearBackends() { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * get a user by user id |
||
| 124 | * |
||
| 125 | * @param string $uid |
||
| 126 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 127 | */ |
||
| 128 | 1281 | public function get($uid) { |
|
| 139 | |||
| 140 | /** |
||
| 141 | * get or construct the user object |
||
| 142 | * |
||
| 143 | * @param string $uid |
||
| 144 | * @param \OCP\UserInterface $backend |
||
| 145 | * @return \OC\User\User |
||
| 146 | */ |
||
| 147 | 1105 | protected function getUserObject($uid, $backend) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * check if a user exists |
||
| 157 | * |
||
| 158 | * @param string $uid |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | 1245 | public function userExists($uid) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Check if the password is valid for the user |
||
| 168 | * |
||
| 169 | * @param string $loginname |
||
| 170 | * @param string $password |
||
| 171 | * @return mixed the User object on success, false otherwise |
||
| 172 | */ |
||
| 173 | 269 | public function checkPassword($loginname, $password) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * search by user id |
||
| 192 | * |
||
| 193 | * @param string $pattern |
||
| 194 | * @param int $limit |
||
| 195 | * @param int $offset |
||
| 196 | * @return \OC\User\User[] |
||
| 197 | */ |
||
| 198 | 21 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
| 218 | |||
| 219 | /** |
||
| 220 | * search by displayName |
||
| 221 | * |
||
| 222 | * @param string $pattern |
||
| 223 | * @param int $limit |
||
| 224 | * @param int $offset |
||
| 225 | * @return \OC\User\User[] |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 228 | $users = array(); |
||
| 229 | foreach ($this->backends as $backend) { |
||
| 230 | $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
||
| 231 | if (is_array($backendUsers)) { |
||
| 232 | foreach ($backendUsers as $uid => $displayName) { |
||
| 233 | $users[] = $this->getUserObject($uid, $backend); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | usort($users, function ($a, $b) { |
||
| 239 | /** |
||
| 240 | * @var \OC\User\User $a |
||
| 241 | * @var \OC\User\User $b |
||
| 242 | */ |
||
| 243 | return strcmp($a->getDisplayName(), $b->getDisplayName()); |
||
| 244 | }); |
||
| 245 | return $users; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $uid |
||
| 250 | * @param string $password |
||
| 251 | * @throws \Exception |
||
| 252 | * @return bool|\OC\User\User the created user or false |
||
| 253 | */ |
||
| 254 | 613 | public function createUser($uid, $password) { |
|
| 287 | |||
| 288 | /** |
||
| 289 | * returns how many users per backend exist (if supported by backend) |
||
| 290 | * |
||
| 291 | * @return array an array of backend class as key and count number as value |
||
| 292 | */ |
||
| 293 | 3 | public function countUsers() { |
|
| 314 | } |
||
| 315 |
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.