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 Root 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 Root, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class Root extends Folder implements IRootFolder { |
||
| 62 | /** @var Manager */ |
||
| 63 | private $mountManager; |
||
| 64 | /** @var PublicEmitter */ |
||
| 65 | private $emitter; |
||
| 66 | /** @var null|\OC\User\User */ |
||
| 67 | private $user; |
||
| 68 | /** @var CappedMemoryCache */ |
||
| 69 | private $userFolderCache; |
||
| 70 | /** @var IUserMountCache */ |
||
| 71 | private $userMountCache; |
||
| 72 | /** @var ILogger */ |
||
| 73 | private $logger; |
||
| 74 | /** @var IUserManager */ |
||
| 75 | private $userManager; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param \OC\Files\Mount\Manager $manager |
||
| 79 | * @param \OC\Files\View $view |
||
| 80 | * @param \OC\User\User|null $user |
||
| 81 | * @param IUserMountCache $userMountCache |
||
| 82 | * @param ILogger $logger |
||
| 83 | * @param IUserManager $userManager |
||
| 84 | */ |
||
| 85 | public function __construct($manager, |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the user for which the filesystem is setup |
||
| 103 | * |
||
| 104 | * @return \OC\User\User |
||
| 105 | */ |
||
| 106 | public function getUser() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $scope |
||
| 112 | * @param string $method |
||
| 113 | * @param callable $callback |
||
| 114 | */ |
||
| 115 | public function listen($scope, $method, callable $callback) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $scope optional |
||
| 121 | * @param string $method optional |
||
| 122 | * @param callable $callback optional |
||
| 123 | */ |
||
| 124 | public function removeListener($scope = null, $method = null, callable $callback = null) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $scope |
||
| 130 | * @param string $method |
||
| 131 | * @param Node[] $arguments |
||
| 132 | */ |
||
| 133 | public function emit($scope, $method, $arguments = array()) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param \OC\Files\Storage\Storage $storage |
||
| 139 | * @param string $mountPoint |
||
| 140 | * @param array $arguments |
||
| 141 | */ |
||
| 142 | public function mount($storage, $mountPoint, $arguments = array()) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $mountPoint |
||
| 149 | * @return \OC\Files\Mount\MountPoint |
||
| 150 | */ |
||
| 151 | public function getMount($mountPoint) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $mountPoint |
||
| 157 | * @return \OC\Files\Mount\MountPoint[] |
||
| 158 | */ |
||
| 159 | public function getMountsIn($mountPoint) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $storageId |
||
| 165 | * @return \OC\Files\Mount\MountPoint[] |
||
| 166 | */ |
||
| 167 | public function getMountByStorageId($storageId) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param int $numericId |
||
| 173 | * @return MountPoint[] |
||
| 174 | */ |
||
| 175 | public function getMountByNumericStorageId($numericId) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param \OC\Files\Mount\MountPoint $mount |
||
| 181 | */ |
||
| 182 | public function unMount($mount) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $path |
||
| 188 | * @throws \OCP\Files\NotFoundException |
||
| 189 | * @throws \OCP\Files\NotPermittedException |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function get($path) { |
||
| 206 | |||
| 207 | //most operations can't be done on the root |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $targetPath |
||
| 211 | * @throws \OCP\Files\NotPermittedException |
||
| 212 | * @return \OC\Files\Node\Node |
||
| 213 | */ |
||
| 214 | public function rename($targetPath) { |
||
| 217 | |||
| 218 | public function delete() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $targetPath |
||
| 224 | * @throws \OCP\Files\NotPermittedException |
||
| 225 | * @return \OC\Files\Node\Node |
||
| 226 | */ |
||
| 227 | public function copy($targetPath) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param int $mtime |
||
| 233 | * @throws \OCP\Files\NotPermittedException |
||
| 234 | */ |
||
| 235 | public function touch($mtime = null) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return \OC\Files\Storage\Storage |
||
| 241 | * @throws \OCP\Files\NotFoundException |
||
| 242 | */ |
||
| 243 | public function getStorage() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getPath() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getInternalPath() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public function getId() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function stat() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return int |
||
| 277 | */ |
||
| 278 | public function getMTime() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return int |
||
| 284 | */ |
||
| 285 | public function getSize() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getEtag() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return int |
||
| 298 | */ |
||
| 299 | public function getPermissions() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function isReadable() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function isUpdateable() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function isDeletable() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function isShareable() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return Node |
||
| 333 | * @throws \OCP\Files\NotFoundException |
||
| 334 | */ |
||
| 335 | public function getParent() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getName() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns a view to user's files folder |
||
| 348 | * |
||
| 349 | * @param String $userId user ID |
||
| 350 | * @return \OCP\Files\Folder |
||
| 351 | * @throws \OC\User\NoUserException |
||
| 352 | */ |
||
| 353 | public function getUserFolder($userId) { |
||
| 389 | |||
| 390 | public function clearCache() { |
||
| 393 | |||
| 394 | public function getUserMountCache() { |
||
| 397 | } |
||
| 398 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.