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