Complex classes like FileManager 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 FileManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class FileManager extends Component implements SingletonInterface, FilesInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Declares to IoC that component instance should be treated as singleton. |
||
| 24 | */ |
||
| 25 | const SINGLETON = self::class; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Files to be removed when component destructed. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $destruct = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * New File Manager. |
||
| 36 | * |
||
| 37 | * @todo Potentially can be depended on Symfony Filesystem. |
||
| 38 | */ |
||
| 39 | public function __construct() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | * |
||
| 48 | * @param bool $recursive Every created directory will get specified permissions. |
||
| 49 | */ |
||
| 50 | public function ensureDirectory($directory, $mode = self::RUNTIME, $recursive = true) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | */ |
||
| 83 | public function read($filename) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | * |
||
| 95 | * @param bool $append To append data at the end of existed file. |
||
| 96 | */ |
||
| 97 | public function write($filename, $data, $mode = null, $ensureDirectory = false, $append = false) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | public function append($filename, $data, $mode = null, $ensureDirectory = false) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function localUri($filename) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function delete($filename) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function deleteDirectory($directory, $clean = false) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function move($filename, $destination) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function copy($filename, $destination) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function touch($filename, $mode = null, $ensureLocation = false) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function exists($filename) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function size($filename) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function extension($filename) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function md5($filename) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function time($filename) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function isDirectory($filename) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function isFile($filename) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function getPermissions($filename) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function setPermissions($filename, $mode) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritdoc} |
||
| 307 | * |
||
| 308 | * @param Finder $finder Optional initial finder. |
||
| 309 | */ |
||
| 310 | public function getFiles($location, $pattern = null, Finder $finder = null) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | public function tempFilename($extension = '', $location = null) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function normalizePath($path, $directory = false) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | * |
||
| 364 | * @link http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php |
||
| 365 | */ |
||
| 366 | public function relativePath($path, $from) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Destruct every temporary file. |
||
| 399 | */ |
||
| 400 | public function __destruct() |
||
| 406 | } |