Complex classes like Files 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 Files, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Files |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Contains the details for any file collections |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $file_data = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Returns the file data collection |
||
| 34 | * |
||
| 35 | * @return \mithra62\array |
||
| 36 | */ |
||
| 37 | public function getFileData() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Sets the file to store for later |
||
| 44 | * |
||
| 45 | * @param mixed $file |
||
| 46 | * The file to set |
||
| 47 | * @param bool $reset |
||
| 48 | * Whether the counter should be reset when set |
||
| 49 | * @return \mithra62\Files |
||
| 50 | */ |
||
| 51 | public function setFileData($file = false, $reset = false) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Writes data to $path creating it if it doesn't exist |
||
| 66 | * |
||
| 67 | * @param string $path |
||
| 68 | * The path to the file |
||
| 69 | * @param string $data |
||
| 70 | * The data to write |
||
| 71 | * @param string $mode |
||
| 72 | * The access we need to stream the file |
||
| 73 | * @return boolean |
||
| 74 | */ |
||
| 75 | public function write($path, $data, $mode = 'w+') |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the contents of a file |
||
| 91 | * |
||
| 92 | * @param string $file |
||
| 93 | * The path to the file to read |
||
| 94 | * @return boolean|string |
||
| 95 | */ |
||
| 96 | public function read($file) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Removes a file from the file system |
||
| 107 | * |
||
| 108 | * @param string $path |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function delete($path) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Removes all the files in the given $path |
||
| 120 | * |
||
| 121 | * Files have to be writable and/or owned by the system user in order to be processed |
||
| 122 | * |
||
| 123 | * @param string $path |
||
| 124 | * @param string $del_dir |
||
| 125 | * @param number $level |
||
| 126 | * @param array $exclude |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | public function deleteDir($path, $del_dir = false, $level = 0, $exclude = array()) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Format a number of bytes into a human readable format. |
||
| 161 | * Optionally choose the output format and/or force a particular unit |
||
| 162 | * |
||
| 163 | * @param string $val |
||
| 164 | * The number to format |
||
| 165 | * @param number $digits |
||
| 166 | * How many digits to display |
||
| 167 | * @param string $mode |
||
| 168 | * Either SI or EIC to determine either 1000 or 1024 bytes |
||
| 169 | * @param string $bB |
||
| 170 | * Whether to use b or B formatting |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function filesizeFormat($val, $digits = 3, $mode = "IEC", $bB = "B") |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Given the full system path to a file it will force the "Save As" dialogue of browsers |
||
| 234 | * |
||
| 235 | * @param unknown $filename |
||
| 236 | * @param string $force_name |
||
| 237 | */ |
||
| 238 | public function fileDownload($filename, $force_name = FALSE) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns all the files in a directory |
||
| 310 | * |
||
| 311 | * @param string $source_dir |
||
| 312 | * The path to the directory |
||
| 313 | * @param boolean $include_path |
||
| 314 | * Whether to include the full path or jsut file name |
||
| 315 | * @param boolean $recursion |
||
| 316 | * Whether to drill down into further directories |
||
| 317 | * @return array|boolean |
||
| 318 | */ |
||
| 319 | public function getFilenames($source_dir, $include_path = true, $recursion = true) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Copies a directory to another |
||
| 346 | * |
||
| 347 | * @param string $dir |
||
| 348 | * The path to copy |
||
| 349 | * @param string $destination |
||
| 350 | * The path to save all the files to |
||
| 351 | */ |
||
| 352 | public function copyDir($dir, $destination) |
||
| 363 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.