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 |
||
| 19 | class FileManager extends Component implements SingletonInterface, FilesInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Declares to IoC that component instance should be treated as singleton. |
||
| 23 | */ |
||
| 24 | const SINGLETON = self::class; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Files to be removed when component destructed. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $destruct = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * New File Manager. |
||
| 35 | * |
||
| 36 | * @todo Potentially can be depended on Symfony Filesystem. |
||
| 37 | */ |
||
| 38 | public function __construct() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | * |
||
| 47 | * @param bool $recursive Every created directory will get specified permissions. |
||
| 48 | */ |
||
| 49 | public function ensureDirectory($directory, $mode = self::RUNTIME, $recursive = true) |
||
| 50 | { |
||
| 51 | $mode = $mode | 0111; |
||
| 52 | if (is_dir($directory)) { |
||
| 53 | //Exists :( |
||
| 54 | return $this->setPermissions($directory, $mode); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!$recursive) { |
||
| 58 | return mkdir($directory, $mode, true); |
||
| 59 | } |
||
| 60 | |||
| 61 | $directoryChain = [basename($directory)]; |
||
| 62 | |||
| 63 | $baseDirectory = $directory; |
||
| 64 | while (!is_dir($baseDirectory = dirname($baseDirectory))) { |
||
| 65 | $directoryChain[] = basename($baseDirectory); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach (array_reverse($directoryChain) as $directory) { |
||
| 69 | if (!mkdir($baseDirectory = $baseDirectory . '/' . $directory)) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | chmod($baseDirectory, $mode); |
||
| 74 | } |
||
| 75 | |||
| 76 | return true; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function read($filename) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | * |
||
| 94 | * @param bool $append To append data at the end of existed file. |
||
| 95 | */ |
||
| 96 | public function write($filename, $data, $mode = null, $ensureDirectory = false, $append = false) |
||
| 97 | { |
||
| 98 | try { |
||
| 99 | if ($ensureDirectory) { |
||
| 100 | $this->ensureDirectory(dirname($filename), $mode); |
||
| 101 | } |
||
| 102 | |||
| 103 | if (!empty($mode) && $this->exists($filename)) { |
||
| 104 | //Forcing mode for existed file |
||
| 105 | $this->setPermissions($filename, $mode); |
||
| 106 | } |
||
| 107 | |||
| 108 | $result = (file_put_contents( |
||
| 109 | $filename, $data, $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
||
| 110 | ) !== false); |
||
| 111 | |||
| 112 | if ($result && !empty($mode)) { |
||
| 113 | //Forcing mode after file creation |
||
| 114 | $this->setPermissions($filename, $mode); |
||
| 115 | } |
||
| 116 | } catch (\ErrorException $exception) { |
||
| 117 | throw new WriteErrorException( |
||
| 118 | $exception->getMessage(), |
||
| 119 | $exception->getCode(), |
||
| 120 | $exception |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $result; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function append($filename, $data, $mode = null, $ensureDirectory = false) |
||
| 131 | { |
||
| 132 | return $this->write($filename, $data, $mode, $ensureDirectory, true); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function localUri($filename) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function delete($filename) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function move($filename, $destination) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function copy($filename, $destination) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function touch($filename, $mode = null, $ensureLocation = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | public function exists($filename) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | public function size($filename) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function extension($filename) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | public function md5($filename) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function time($filename) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function getPermissions($filename) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function setPermissions($filename, $mode) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | * |
||
| 271 | * @param Finder $finder Optional initial finder. |
||
| 272 | */ |
||
| 273 | public function getFiles($location, $pattern = null, Finder $finder = null) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function tempFilename($extension = '', $location = null) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function normalizePath($path, $directory = false) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | * |
||
| 327 | * @link http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php |
||
| 328 | */ |
||
| 329 | public function relativePath($path, $from) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Destruct every temporary file. |
||
| 362 | */ |
||
| 363 | public function __destruct() |
||
| 369 | } |