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 |
||
| 22 | class FileManager extends Component implements SingletonInterface, FilesInterface |
||
| 23 | { |
||
| 24 | const DEFAULT_FILE_MODE = self::READONLY; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Files to be removed when component destructed. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $destructFiles = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * FileManager constructor. |
||
| 35 | */ |
||
| 36 | public function __construct() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | * |
||
| 45 | * @param bool $recursive Every created directory will get specified permissions. |
||
| 46 | */ |
||
| 47 | public function ensureDirectory( |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function read(string $filename): string |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | * |
||
| 99 | * @param bool $append To append data at the end of existed file. |
||
| 100 | */ |
||
| 101 | public function write( |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function append( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function localPath(string $filename): string |
||
| 153 | { |
||
| 154 | if (!$this->exists($filename)) { |
||
| 155 | throw new FileNotFoundException($filename); |
||
| 156 | } |
||
| 157 | |||
| 158 | //Since default implementation is local we are allowed to do that |
||
| 159 | return $filename; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | public function delete(string $filename) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | * |
||
| 177 | * @see http://stackoverflow.com/questions/3349753/delete-directory-with-files-in-it |
||
| 178 | * |
||
| 179 | * @param string $directory |
||
| 180 | * @param bool $contentOnly |
||
| 181 | * |
||
| 182 | * @throws FilesException |
||
| 183 | */ |
||
| 184 | public function deleteDirectory(string $directory, bool $contentOnly = false) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function move(string $filename, string $destination): bool |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | public function copy(string $filename, string $destination): bool |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function touch(string $filename, int $mode = null): bool |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function exists(string $filename): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function size(string $filename): int |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function extension(string $filename): string |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function md5(string $filename): string |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function time(string $filename): int |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function isDirectory(string $filename): bool |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function isFile(string $filename): bool |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function getPermissions(string $filename): int |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | */ |
||
| 327 | public function setPermissions(string $filename, int $mode) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritdoc} |
||
| 338 | * |
||
| 339 | * @param Finder $finder Optional initial finder. |
||
| 340 | */ |
||
| 341 | public function getFiles(string $location, string $pattern = null, Finder $finder = null): array |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | public function tempFilename(string $extension = '', string $location = null): string |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function normalizePath(string $path, bool $directory = false): string |
||
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | * |
||
| 395 | * @link http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php |
||
| 396 | */ |
||
| 397 | public function relativePath(string $path, string $from): string |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Destruct every temporary file. |
||
| 430 | */ |
||
| 431 | public function __destruct() |
||
| 437 | } |
||
| 438 |