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 Jail 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 Jail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Jail extends Wrapper { |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $rootPath; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param array $arguments ['storage' => $storage, 'mask' => $root] |
||
| 46 | * |
||
| 47 | * $storage: The storage that will be wrapper |
||
| 48 | * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage |
||
| 49 | */ |
||
| 50 | public function __construct($arguments) { |
||
| 54 | |||
| 55 | public function getUnjailedPath($path) { |
||
| 62 | |||
| 63 | public function getJailedPath($path) { |
||
| 73 | |||
| 74 | public function getId() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * see http://php.net/manual/en/function.mkdir.php |
||
| 80 | * |
||
| 81 | * @param string $path |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function mkdir($path) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * see http://php.net/manual/en/function.rmdir.php |
||
| 90 | * |
||
| 91 | * @param string $path |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public function rmdir($path) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * see http://php.net/manual/en/function.opendir.php |
||
| 100 | * |
||
| 101 | * @param string $path |
||
| 102 | * @return resource |
||
| 103 | */ |
||
| 104 | public function opendir($path) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * see http://php.net/manual/en/function.is_dir.php |
||
| 110 | * |
||
| 111 | * @param string $path |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function is_dir($path) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * see http://php.net/manual/en/function.is_file.php |
||
| 120 | * |
||
| 121 | * @param string $path |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function is_file($path) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * see http://php.net/manual/en/function.stat.php |
||
| 130 | * only the following keys are required in the result: size and mtime |
||
| 131 | * |
||
| 132 | * @param string $path |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function stat($path) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * see http://php.net/manual/en/function.filetype.php |
||
| 141 | * |
||
| 142 | * @param string $path |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function filetype($path) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * see http://php.net/manual/en/function.filesize.php |
||
| 151 | * The result for filesize when called on a folder is required to be 0 |
||
| 152 | * |
||
| 153 | * @param string $path |
||
| 154 | * @return int |
||
| 155 | */ |
||
| 156 | public function filesize($path) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * check if a file can be created in $path |
||
| 162 | * |
||
| 163 | * @param string $path |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function isCreatable($path) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * check if a file can be read |
||
| 172 | * |
||
| 173 | * @param string $path |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function isReadable($path) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * check if a file can be written to |
||
| 182 | * |
||
| 183 | * @param string $path |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isUpdatable($path) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * check if a file can be deleted |
||
| 192 | * |
||
| 193 | * @param string $path |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function isDeletable($path) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * check if a file can be shared |
||
| 202 | * |
||
| 203 | * @param string $path |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isSharable($path) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * get the full permissions of a path. |
||
| 212 | * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php |
||
| 213 | * |
||
| 214 | * @param string $path |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | public function getPermissions($path) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * see http://php.net/manual/en/function.file_exists.php |
||
| 223 | * |
||
| 224 | * @param string $path |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function file_exists($path) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * see http://php.net/manual/en/function.filemtime.php |
||
| 233 | * |
||
| 234 | * @param string $path |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function filemtime($path) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * see http://php.net/manual/en/function.file_get_contents.php |
||
| 243 | * |
||
| 244 | * @param string $path |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function file_get_contents($path) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * see http://php.net/manual/en/function.file_put_contents.php |
||
| 253 | * |
||
| 254 | * @param string $path |
||
| 255 | * @param string $data |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function file_put_contents($path, $data) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * see http://php.net/manual/en/function.unlink.php |
||
| 264 | * |
||
| 265 | * @param string $path |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function unlink($path) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * see http://php.net/manual/en/function.rename.php |
||
| 274 | * |
||
| 275 | * @param string $path1 |
||
| 276 | * @param string $path2 |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function rename($path1, $path2) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * see http://php.net/manual/en/function.copy.php |
||
| 285 | * |
||
| 286 | * @param string $path1 |
||
| 287 | * @param string $path2 |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function copy($path1, $path2) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * see http://php.net/manual/en/function.fopen.php |
||
| 296 | * |
||
| 297 | * @param string $path |
||
| 298 | * @param string $mode |
||
| 299 | * @return resource |
||
| 300 | */ |
||
| 301 | public function fopen($path, $mode) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * get the mimetype for a file or folder |
||
| 307 | * The mimetype for a folder is required to be "httpd/unix-directory" |
||
| 308 | * |
||
| 309 | * @param string $path |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getMimeType($path) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * see http://php.net/manual/en/function.hash.php |
||
| 318 | * |
||
| 319 | * @param string $type |
||
| 320 | * @param string $path |
||
| 321 | * @param bool $raw |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function hash($type, $path, $raw = false) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * see http://php.net/manual/en/function.free_space.php |
||
| 330 | * |
||
| 331 | * @param string $path |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | public function free_space($path) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * search for occurrences of $query in file names |
||
| 340 | * |
||
| 341 | * @param string $query |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | public function search($query) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * see http://php.net/manual/en/function.touch.php |
||
| 350 | * If the backend does not support the operation, false should be returned |
||
| 351 | * |
||
| 352 | * @param string $path |
||
| 353 | * @param int $mtime |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function touch($path, $mtime = null) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * get the path to a local version of the file. |
||
| 362 | * The local version of the file can be temporary and doesn't have to be persistent across requests |
||
| 363 | * |
||
| 364 | * @param string $path |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getLocalFile($path) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * check if a file or folder has been updated since $time |
||
| 373 | * |
||
| 374 | * @param string $path |
||
| 375 | * @param int $time |
||
| 376 | * @return bool |
||
| 377 | * |
||
| 378 | * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed. |
||
| 379 | * returning true for other changes in the folder is optional |
||
| 380 | */ |
||
| 381 | public function hasUpdated($path, $time) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * get a cache instance for the storage |
||
| 387 | * |
||
| 388 | * @param string $path |
||
| 389 | * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache |
||
| 390 | * @return \OC\Files\Cache\Cache |
||
| 391 | */ |
||
| 392 | public function getCache($path = '', $storage = null) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * get the user id of the owner of a file or folder |
||
| 402 | * |
||
| 403 | * @param string $path |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getOwner($path) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * get a watcher instance for the cache |
||
| 412 | * |
||
| 413 | * @param string $path |
||
| 414 | * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher |
||
| 415 | * @return \OC\Files\Cache\Watcher |
||
| 416 | */ |
||
| 417 | public function getWatcher($path = '', $storage = null) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * get the ETag for a file or folder |
||
| 426 | * |
||
| 427 | * @param string $path |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getETag($path) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $path |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | public function getMetaData($path) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param string $path |
||
| 444 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 445 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 446 | * @throws \OCP\Lock\LockedException |
||
| 447 | */ |
||
| 448 | public function acquireLock($path, $type, ILockingProvider $provider) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $path |
||
| 454 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 455 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 456 | */ |
||
| 457 | public function releaseLock($path, $type, ILockingProvider $provider) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $path |
||
| 463 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 464 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 465 | */ |
||
| 466 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Resolve the path for the source of the share |
||
| 472 | * |
||
| 473 | * @param string $path |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function resolvePath($path) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param IStorage $sourceStorage |
||
| 482 | * @param string $sourceInternalPath |
||
| 483 | * @param string $targetInternalPath |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param IStorage $sourceStorage |
||
| 495 | * @param string $sourceInternalPath |
||
| 496 | * @param string $targetInternalPath |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 505 | |||
| 506 | View Code Duplication | public function getPropagator($storage = null) { |
|
| 517 | } |
||
| 518 |