Complex classes like Local 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 Local, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Local extends AbstractAdapter |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | const SKIP_LINKS = 0001; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | const DISALLOW_LINKS = 0002; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected static $permissions = array( |
||
| 35 | 'file' => array( |
||
| 36 | 'public' => 0644, |
||
| 37 | 'private' => 0600, |
||
| 38 | ), |
||
| 39 | 'dir' => array( |
||
| 40 | 'public' => 0755, |
||
| 41 | 'private' => 0700, |
||
| 42 | ), |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $pathSeparator = DIRECTORY_SEPARATOR; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $permissionMap; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | protected $writeFlags; |
||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $linkHandling; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor. |
||
| 66 | * |
||
| 67 | * @param string $root |
||
| 68 | * @param int $writeFlags |
||
| 69 | * @param int $linkHandling |
||
| 70 | * @param array $permissions |
||
| 71 | */ |
||
| 72 | 159 | public function __construct($root, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS, array $permissions = array()) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Ensure the root directory exists. |
||
| 89 | * |
||
| 90 | * @param string $root root directory path |
||
| 91 | * |
||
| 92 | * @return string real path to root |
||
| 93 | * |
||
| 94 | * @throws Exception in case the root directory can not be created |
||
| 95 | */ |
||
| 96 | 159 | protected function ensureDirectory($root) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | 75 | public function has($path) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritdoc |
||
| 123 | */ |
||
| 124 | 99 | public function write($path, $contents, Config $config) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | */ |
||
| 147 | 21 | public function writeStream($path, $resource, Config $config) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritdoc |
||
| 172 | */ |
||
| 173 | 6 | public function readStream($path) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @inheritdoc |
||
| 183 | */ |
||
| 184 | 9 | public function updateStream($path, $resource, Config $config) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @inheritdoc |
||
| 191 | */ |
||
| 192 | 9 | public function update($path, $contents, Config $config) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritdoc |
||
| 207 | */ |
||
| 208 | 27 | public function read($path) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritdoc |
||
| 222 | */ |
||
| 223 | 6 | public function rename($path, $newpath) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @inheritdoc |
||
| 235 | */ |
||
| 236 | 6 | public function copy($path, $newpath) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @inheritdoc |
||
| 247 | */ |
||
| 248 | 66 | public function delete($path) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @inheritdoc |
||
| 257 | */ |
||
| 258 | 15 | public function listContents($directory = '', $recursive = false) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @inheritdoc |
||
| 284 | */ |
||
| 285 | 45 | public function getMetadata($path) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritdoc |
||
| 295 | */ |
||
| 296 | 6 | public function getSize($path) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @inheritdoc |
||
| 303 | */ |
||
| 304 | 6 | public function getMimetype($path) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @inheritdoc |
||
| 314 | */ |
||
| 315 | 6 | public function getTimestamp($path) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @inheritdoc |
||
| 322 | */ |
||
| 323 | 18 | public function getVisibility($path) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @inheritdoc |
||
| 335 | */ |
||
| 336 | 21 | public function setVisibility($path, $visibility) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @inheritdoc |
||
| 351 | */ |
||
| 352 | 75 | public function createDir($dirname, Config $config) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @inheritdoc |
||
| 371 | */ |
||
| 372 | 75 | public function deleteDir($dirname) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param SplFileInfo $file |
||
| 393 | */ |
||
| 394 | 27 | protected function deleteFileInfoObject(SplFileInfo $file) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Normalize the file info. |
||
| 410 | * |
||
| 411 | * @param SplFileInfo $file |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | 57 | protected function normalizeFileInfo(SplFileInfo $file) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Get the normalized path from a SplFileInfo object. |
||
| 428 | * |
||
| 429 | * @param SplFileInfo $file |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | 57 | protected function getFilePath(SplFileInfo $file) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @param string $path |
||
| 443 | * @param int $mode |
||
| 444 | * |
||
| 445 | * @return RecursiveIteratorIterator |
||
| 446 | */ |
||
| 447 | 75 | protected function getRecursiveDirectoryIterator($path, $mode = RecursiveIteratorIterator::SELF_FIRST) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $path |
||
| 457 | * |
||
| 458 | * @return DirectoryIterator |
||
| 459 | */ |
||
| 460 | 9 | protected function getDirectoryIterator($path) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * @param SplFileInfo $file |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | 57 | protected function mapFileInfo(SplFileInfo $file) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @inheritdoc |
||
| 490 | */ |
||
| 491 | 144 | public function applyPathPrefix($path) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @param SplFileInfo $file |
||
| 500 | * |
||
| 501 | * @throws UnreadableFileException |
||
| 502 | */ |
||
| 503 | 30 | protected function guardAgainstUnreadableFileInfo(SplFileInfo $file) |
|
| 509 | } |
||
| 510 |
If you suppress an error, we recommend checking for the error condition explicitly: