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 |
||
| 36 | class Jail extends Wrapper { |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $rootPath; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array $arguments ['storage' => $storage, 'mask' => $root] |
||
| 44 | * |
||
| 45 | * $storage: The storage that will be wrapper |
||
| 46 | * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage |
||
| 47 | */ |
||
| 48 | public function __construct($arguments) { |
||
| 52 | |||
| 53 | public function getUnjailedPath($path) { |
||
| 60 | |||
| 61 | public function getId() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * see http://php.net/manual/en/function.mkdir.php |
||
| 67 | * |
||
| 68 | * @param string $path |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public function mkdir($path) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * see http://php.net/manual/en/function.rmdir.php |
||
| 77 | * |
||
| 78 | * @param string $path |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function rmdir($path) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * see http://php.net/manual/en/function.opendir.php |
||
| 87 | * |
||
| 88 | * @param string $path |
||
| 89 | * @return resource |
||
| 90 | */ |
||
| 91 | public function opendir($path) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * see http://php.net/manual/en/function.is_dir.php |
||
| 97 | * |
||
| 98 | * @param string $path |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function is_dir($path) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * see http://php.net/manual/en/function.is_file.php |
||
| 107 | * |
||
| 108 | * @param string $path |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function is_file($path) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * see http://php.net/manual/en/function.stat.php |
||
| 117 | * only the following keys are required in the result: size and mtime |
||
| 118 | * |
||
| 119 | * @param string $path |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function stat($path) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * see http://php.net/manual/en/function.filetype.php |
||
| 128 | * |
||
| 129 | * @param string $path |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function filetype($path) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * see http://php.net/manual/en/function.filesize.php |
||
| 138 | * The result for filesize when called on a folder is required to be 0 |
||
| 139 | * |
||
| 140 | * @param string $path |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function filesize($path) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * check if a file can be created in $path |
||
| 149 | * |
||
| 150 | * @param string $path |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function isCreatable($path) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * check if a file can be read |
||
| 159 | * |
||
| 160 | * @param string $path |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function isReadable($path) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * check if a file can be written to |
||
| 169 | * |
||
| 170 | * @param string $path |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function isUpdatable($path) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * check if a file can be deleted |
||
| 179 | * |
||
| 180 | * @param string $path |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function isDeletable($path) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * check if a file can be shared |
||
| 189 | * |
||
| 190 | * @param string $path |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isSharable($path) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * get the full permissions of a path. |
||
| 199 | * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php |
||
| 200 | * |
||
| 201 | * @param string $path |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | public function getPermissions($path) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * see http://php.net/manual/en/function.file_exists.php |
||
| 210 | * |
||
| 211 | * @param string $path |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function file_exists($path) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * see http://php.net/manual/en/function.filemtime.php |
||
| 220 | * |
||
| 221 | * @param string $path |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | public function filemtime($path) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * see http://php.net/manual/en/function.file_get_contents.php |
||
| 230 | * |
||
| 231 | * @param string $path |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function file_get_contents($path) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * see http://php.net/manual/en/function.file_put_contents.php |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * @param string $data |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function file_put_contents($path, $data) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * see http://php.net/manual/en/function.unlink.php |
||
| 251 | * |
||
| 252 | * @param string $path |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function unlink($path) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * see http://php.net/manual/en/function.rename.php |
||
| 261 | * |
||
| 262 | * @param string $path1 |
||
| 263 | * @param string $path2 |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function rename($path1, $path2) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * see http://php.net/manual/en/function.copy.php |
||
| 272 | * |
||
| 273 | * @param string $path1 |
||
| 274 | * @param string $path2 |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function copy($path1, $path2) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * see http://php.net/manual/en/function.fopen.php |
||
| 283 | * |
||
| 284 | * @param string $path |
||
| 285 | * @param string $mode |
||
| 286 | * @return resource |
||
| 287 | */ |
||
| 288 | public function fopen($path, $mode) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * get the mimetype for a file or folder |
||
| 294 | * The mimetype for a folder is required to be "httpd/unix-directory" |
||
| 295 | * |
||
| 296 | * @param string $path |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function getMimeType($path) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * see http://php.net/manual/en/function.hash.php |
||
| 305 | * |
||
| 306 | * @param string $type |
||
| 307 | * @param string $path |
||
| 308 | * @param bool $raw |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function hash($type, $path, $raw = false) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * see http://php.net/manual/en/function.free_space.php |
||
| 317 | * |
||
| 318 | * @param string $path |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | public function free_space($path) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * search for occurrences of $query in file names |
||
| 327 | * |
||
| 328 | * @param string $query |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public function search($query) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * see http://php.net/manual/en/function.touch.php |
||
| 337 | * If the backend does not support the operation, false should be returned |
||
| 338 | * |
||
| 339 | * @param string $path |
||
| 340 | * @param int $mtime |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function touch($path, $mtime = null) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * get the path to a local version of the file. |
||
| 349 | * The local version of the file can be temporary and doesn't have to be persistent across requests |
||
| 350 | * |
||
| 351 | * @param string $path |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getLocalFile($path) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * check if a file or folder has been updated since $time |
||
| 360 | * |
||
| 361 | * @param string $path |
||
| 362 | * @param int $time |
||
| 363 | * @return bool |
||
| 364 | * |
||
| 365 | * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed. |
||
| 366 | * returning true for other changes in the folder is optional |
||
| 367 | */ |
||
| 368 | public function hasUpdated($path, $time) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * get a cache instance for the storage |
||
| 374 | * |
||
| 375 | * @param string $path |
||
| 376 | * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache |
||
| 377 | * @return \OC\Files\Cache\Cache |
||
| 378 | */ |
||
| 379 | public function getCache($path = '', $storage = null) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * get the user id of the owner of a file or folder |
||
| 389 | * |
||
| 390 | * @param string $path |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function getOwner($path) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * get a watcher instance for the cache |
||
| 399 | * |
||
| 400 | * @param string $path |
||
| 401 | * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher |
||
| 402 | * @return \OC\Files\Cache\Watcher |
||
| 403 | */ |
||
| 404 | public function getWatcher($path = '', $storage = null) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * get the ETag for a file or folder |
||
| 413 | * |
||
| 414 | * @param string $path |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getETag($path) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $path |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | public function getMetaData($path) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $path |
||
| 431 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 432 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 433 | * @throws \OCP\Lock\LockedException |
||
| 434 | */ |
||
| 435 | public function acquireLock($path, $type, ILockingProvider $provider) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $path |
||
| 441 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 442 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 443 | */ |
||
| 444 | public function releaseLock($path, $type, ILockingProvider $provider) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $path |
||
| 450 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 451 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 452 | */ |
||
| 453 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Resolve the path for the source of the share |
||
| 459 | * |
||
| 460 | * @param string $path |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function resolvePath($path) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param \OCP\Files\Storage $sourceStorage |
||
| 469 | * @param string $sourceInternalPath |
||
| 470 | * @param string $targetInternalPath |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param \OCP\Files\Storage $sourceStorage |
||
| 482 | * @param string $sourceInternalPath |
||
| 483 | * @param string $targetInternalPath |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 492 | |||
| 493 | View Code Duplication | public function getPropagator($storage = null) { |
|
| 504 | } |
||
| 505 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.