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 | /** |
||
| 25 | * Default file mode for this manager. |
||
| 26 | */ |
||
| 27 | const DEFAULT_FILE_MODE = self::READONLY; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Files to be removed when component destructed. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $destructFiles = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * FileManager constructor. |
||
| 38 | */ |
||
| 39 | 68 | public function __construct() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | * |
||
| 47 | * @param bool $recursivePermissions Propagate permissions on created directories. |
||
| 48 | */ |
||
| 49 | 44 | public function ensureDirectory( |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 25 | public function read(string $filename): string |
|
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | * |
||
| 102 | * @param bool $append To append data at the end of existed file. |
||
| 103 | */ |
||
| 104 | 19 | public function write( |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | 2 | public function append( |
|
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | 2 | public function localFilename(string $filename): string |
|
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | 21 | public function delete(string $filename) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | * |
||
| 185 | * @see http://stackoverflow.com/questions/3349753/delete-directory-with-files-in-it |
||
| 186 | * |
||
| 187 | * @param string $directory |
||
| 188 | * @param bool $contentOnly |
||
| 189 | * |
||
| 190 | * @throws FilesException |
||
| 191 | */ |
||
| 192 | 44 | public function deleteDirectory(string $directory, bool $contentOnly = false) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | 2 | public function move(string $filename, string $destination): bool |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | 2 | public function copy(string $filename, string $destination): bool |
|
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | 2 | public function touch(string $filename, int $mode = null): bool |
|
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | 63 | public function exists(string $filename): bool |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | 2 | public function size(string $filename): int |
|
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | 3 | public function extension(string $filename): string |
|
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | 21 | public function md5(string $filename): string |
|
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | 2 | public function time(string $filename): int |
|
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | 44 | public function isDirectory(string $filename): bool |
|
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | 5 | public function isFile(string $filename): bool |
|
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritdoc} |
||
| 322 | */ |
||
| 323 | 43 | public function getPermissions(string $filename): int |
|
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | 43 | public function setPermissions(string $filename, int $mode) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | * |
||
| 348 | * @param Finder $finder Pre-configured Finder. |
||
| 349 | */ |
||
| 350 | 2 | public function getFiles(string $location, string $pattern = null, Finder $finder = null): array |
|
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | */ |
||
| 370 | 4 | public function tempFilename(string $extension = '', string $location = null): string |
|
| 386 | |||
| 387 | /** |
||
| 388 | * {@inheritdoc} |
||
| 389 | */ |
||
| 390 | 24 | public function normalizePath(string $path, bool $asDirectory = false): string |
|
| 397 | |||
| 398 | /** |
||
| 399 | * {@inheritdoc} |
||
| 400 | * |
||
| 401 | * @link http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php |
||
| 402 | */ |
||
| 403 | 1 | public function relativePath(string $path, string $from): string |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Destruct every temporary file. |
||
| 436 | */ |
||
| 437 | 1 | public function __destruct() |
|
| 443 | } |
||
| 444 |