Complex classes like Directory 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 Directory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Directory |
||
| 28 | { |
||
| 29 | private $filtername = 'ImagesOnly'; |
||
| 30 | private $directory = ''; |
||
| 31 | |||
| 32 | public function __construct($directory = null) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Available Filter types: image. |
||
| 43 | */ |
||
| 44 | public function setFilter($filtername) |
||
| 50 | |||
| 51 | public function setDirectory($directory) |
||
| 66 | |||
| 67 | public function getDirectory($directory = '') |
||
| 68 | { |
||
| 69 | if ($directory !== '') { |
||
| 70 | $this->directory = $directory; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (empty($this->directory) === false) { |
||
| 74 | return $this->directory; |
||
| 75 | } else { // default path |
||
| 76 | |||
| 77 | return APPLICATION_PATH . 'uploads/images/gallery'; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function getFiles($return_as_array = false) |
||
| 82 | { |
||
| 83 | // compose the full name of the filter class |
||
| 84 | $classname = 'Koch_' . $this->filtername . 'FilterIterator'; |
||
| 85 | |||
| 86 | // wrap the iterator in a filter class, when looking for a specific file type, like imagesOnly |
||
| 87 | $iterator = new $classname(new \DirectoryIterator($this->getDirectory())); |
||
| 88 | |||
| 89 | // return objects |
||
| 90 | if ($return_as_array === false) { |
||
| 91 | // create new array to take the SPL FileInfo Objects |
||
| 92 | $data = new \ArrayObject(); |
||
| 93 | |||
| 94 | // while iterating |
||
| 95 | foreach ($iterator as $file) { |
||
| 96 | /* |
||
| 97 | * push the SPL FileInfo Objects into the array |
||
| 98 | * @see http://www.php.net/~helly/php/ext/spl/classSplFileInfo.html |
||
| 99 | */ |
||
| 100 | $data[$file->getFilename()] = $file->getFileInfo(); |
||
| 101 | } |
||
| 102 | |||
| 103 | $data->ksort(); |
||
| 104 | } else { // return array |
||
| 105 | // create array |
||
| 106 | $data = []; |
||
| 107 | |||
| 108 | // while iterating |
||
| 109 | foreach ($iterator as $file) { |
||
| 110 | $wwwpath = WWW_ROOT . '/' . $this->getDirectory() . '/' . $file->getFilename(); |
||
| 111 | $wwwpath = str_replace('//', '/', $wwwpath); |
||
| 112 | $data[$wwwpath] = $file->getFilename(); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // return the array with SPL FileInfo Objects |
||
| 117 | return $data; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the filePath with filename. |
||
| 122 | * |
||
| 123 | * @return array pathinfo |
||
| 124 | */ |
||
| 125 | public function filePath($filePath) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Calculates the size of a directory (recursiv). |
||
| 138 | * |
||
| 139 | * @param $dir Directory Path |
||
| 140 | * |
||
| 141 | * @return $size size of directory in bytes |
||
| 142 | */ |
||
| 143 | public static function size($dir) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Copy a directory recursively. |
||
| 176 | * |
||
| 177 | * @param $source |
||
| 178 | * @param $destination |
||
| 179 | * @param $overwrite boolean |
||
| 180 | */ |
||
| 181 | public function dirCopy($source, $destination, $overwrite = true) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Recursively delete directory using PHP iterators. |
||
| 239 | * |
||
| 240 | * Uses a CHILD_FIRST RecursiveIteratorIterator to sort files |
||
| 241 | * before directories, creating a single non-recursive loop |
||
| 242 | * to delete files/directories in the correct order. |
||
| 243 | * |
||
| 244 | * @param string $directory |
||
| 245 | * @param bool $delete_dir_itself |
||
| 246 | * |
||
| 247 | * @return bool|null |
||
| 248 | */ |
||
| 249 | public function deleteDir($directory, $delete_dir_itself = false) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Performs a chmod operation. |
||
| 269 | * |
||
| 270 | * @param $path |
||
| 271 | * @param $chmod |
||
| 272 | * @param $recursive |
||
| 273 | */ |
||
| 274 | public function chmod($path = '', $chmod = '755', $recursive = false) |
||
| 315 | } |
||
| 316 |