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 UserMountCache 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 UserMountCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class UserMountCache implements IUserMountCache { |
||
| 47 | /** |
||
| 48 | * @var IDBConnection |
||
| 49 | */ |
||
| 50 | private $connection; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var IUserManager |
||
| 54 | */ |
||
| 55 | private $userManager; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Cached mount info. |
||
| 59 | * Map of $userId to ICachedMountInfo. |
||
| 60 | * |
||
| 61 | * @var ICache |
||
| 62 | **/ |
||
| 63 | private $mountsForUsers; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ILogger |
||
| 67 | */ |
||
| 68 | private $logger; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ICache |
||
| 72 | */ |
||
| 73 | private $cacheInfoCache; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * UserMountCache constructor. |
||
| 77 | * |
||
| 78 | * @param IDBConnection $connection |
||
| 79 | * @param IUserManager $userManager |
||
| 80 | * @param ILogger $logger |
||
| 81 | */ |
||
| 82 | public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
||
| 89 | |||
| 90 | public function registerMounts(IUser $user, array $mounts) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param ICachedMountInfo[] $newMounts |
||
| 135 | * @param ICachedMountInfo[] $cachedMounts |
||
| 136 | * @return ICachedMountInfo[] |
||
| 137 | */ |
||
| 138 | private function findChangedMounts(array $newMounts, array $cachedMounts) { |
||
| 156 | |||
| 157 | private function addToCache(ICachedMountInfo $mount) { |
||
| 171 | |||
| 172 | private function updateCachedMount(ICachedMountInfo $mount) { |
||
| 184 | |||
| 185 | View Code Duplication | private function removeFromCache(ICachedMountInfo $mount) { |
|
| 193 | |||
| 194 | private function dbRowToMountInfo(array $row) { |
||
| 195 | $user = $this->userManager->get($row['user_id']); |
||
| 196 | if (is_null($user)) { |
||
| 197 | return null; |
||
| 198 | } |
||
| 199 | $mount_id = $row['mount_id']; |
||
| 200 | if (!is_null($mount_id)) { |
||
| 201 | $mount_id = (int) $mount_id; |
||
| 202 | } |
||
| 203 | return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path']) ? $row['path'] : ''); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param IUser $user |
||
| 208 | * @return ICachedMountInfo[] |
||
| 209 | */ |
||
| 210 | public function getMountsForUser(IUser $user) { |
||
| 211 | if (!isset($this->mountsForUsers[$user->getUID()])) { |
||
| 212 | $builder = $this->connection->getQueryBuilder(); |
||
| 213 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
||
| 214 | ->from('mounts', 'm') |
||
| 215 | ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
||
| 216 | ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
||
| 217 | |||
| 218 | $rows = $query->execute()->fetchAll(); |
||
| 219 | |||
| 220 | $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
||
| 221 | } |
||
| 222 | return $this->mountsForUsers[$user->getUID()]; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param int $numericStorageId |
||
| 227 | * @param string|null $user limit the results to a single user |
||
| 228 | * @return CachedMountInfo[] |
||
| 229 | */ |
||
| 230 | public function getMountsForStorageId($numericStorageId, $user = null) { |
||
| 231 | $builder = $this->connection->getQueryBuilder(); |
||
| 232 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
||
| 233 | ->from('mounts', 'm') |
||
| 234 | ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
||
| 235 | ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
||
| 236 | |||
| 237 | if ($user) { |
||
|
|
|||
| 238 | $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user))); |
||
| 239 | } |
||
| 240 | |||
| 241 | $rows = $query->execute()->fetchAll(); |
||
| 242 | |||
| 243 | return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param int $rootFileId |
||
| 248 | * @return CachedMountInfo[] |
||
| 249 | */ |
||
| 250 | public function getMountsForRootId($rootFileId) { |
||
| 251 | $builder = $this->connection->getQueryBuilder(); |
||
| 252 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
||
| 253 | ->from('mounts', 'm') |
||
| 254 | ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
||
| 255 | ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
||
| 256 | |||
| 257 | $rows = $query->execute()->fetchAll(); |
||
| 258 | |||
| 259 | return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param $fileId |
||
| 264 | * @return array |
||
| 265 | * @throws \OCP\Files\NotFoundException |
||
| 266 | */ |
||
| 267 | private function getCacheInfoFromFileId($fileId) { |
||
| 268 | if (!isset($this->cacheInfoCache[$fileId])) { |
||
| 269 | $builder = $this->connection->getQueryBuilder(); |
||
| 270 | $query = $builder->select('storage', 'path', 'mimetype') |
||
| 271 | ->from('filecache') |
||
| 272 | ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
||
| 273 | |||
| 274 | $row = $query->execute()->fetch(); |
||
| 275 | if (is_array($row)) { |
||
| 276 | $this->cacheInfoCache[$fileId] = [ |
||
| 277 | (int)$row['storage'], |
||
| 278 | $row['path'], |
||
| 279 | (int)$row['mimetype'] |
||
| 280 | ]; |
||
| 281 | } else { |
||
| 282 | throw new NotFoundException('File with id "' . $fileId . '" not found'); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | return $this->cacheInfoCache[$fileId]; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param int $fileId |
||
| 290 | * @param string|null $user optionally restrict the results to a single user |
||
| 291 | * @return ICachedMountFileInfo[] |
||
| 292 | * @since 9.0.0 |
||
| 293 | */ |
||
| 294 | public function getMountsForFileId($fileId, $user = null) { |
||
| 295 | try { |
||
| 296 | list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
||
| 297 | } catch (NotFoundException $e) { |
||
| 298 | return []; |
||
| 299 | } |
||
| 300 | $mountsForStorage = $this->getMountsForStorageId($storageId, $user); |
||
| 301 | |||
| 302 | // filter mounts that are from the same storage but a different directory |
||
| 303 | $filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
||
| 304 | if ($fileId === $mount->getRootId()) { |
||
| 305 | return true; |
||
| 306 | } |
||
| 307 | $internalMountPath = $mount->getRootInternalPath(); |
||
| 308 | |||
| 309 | return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
||
| 310 | }); |
||
| 311 | |||
| 312 | return array_map(function (ICachedMountInfo $mount) use ($internalPath) { |
||
| 313 | return new CachedMountFileInfo( |
||
| 314 | $mount->getUser(), |
||
| 315 | $mount->getStorageId(), |
||
| 316 | $mount->getRootId(), |
||
| 317 | $mount->getMountPoint(), |
||
| 318 | $mount->getMountId(), |
||
| 319 | $mount->getRootInternalPath(), |
||
| 320 | $internalPath |
||
| 321 | ); |
||
| 322 | }, $filteredMounts); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Remove all cached mounts for a user |
||
| 327 | * |
||
| 328 | * @param IUser $user |
||
| 329 | */ |
||
| 330 | View Code Duplication | public function removeUserMounts(IUser $user) { |
|
| 337 | |||
| 338 | View Code Duplication | public function removeUserStorageMount($storageId, $userId) { |
|
| 346 | |||
| 347 | View Code Duplication | public function remoteStorageMounts($storageId) { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $users |
||
| 357 | * @return array |
||
| 358 | * @suppress SqlInjectionChecker |
||
| 359 | */ |
||
| 360 | public function getUsedSpaceForUsers(array $users) { |
||
| 393 | } |
||
| 394 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: