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 |
||
21 | class FileManager extends Component implements SingletonInterface, FilesInterface |
||
22 | { |
||
23 | /** |
||
24 | * Declares to IoC that component instance should be treated as singleton. |
||
25 | */ |
||
26 | const SINGLETON = self::class; |
||
27 | |||
28 | /** |
||
29 | * Files to be removed when component destructed. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $destruct = []; |
||
34 | |||
35 | /** |
||
36 | * New File Manager. |
||
37 | * |
||
38 | * @todo Potentially can be depended on Symfony Filesystem. |
||
39 | */ |
||
40 | public function __construct() |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | * |
||
49 | * @param bool $recursive Every created directory will get specified permissions. |
||
50 | */ |
||
51 | public function ensureDirectory($directory, $mode = self::RUNTIME, $recursive = true) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function read($filename) |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | * |
||
96 | * @param bool $append To append data at the end of existed file. |
||
97 | */ |
||
98 | public function write($filename, $data, $mode = null, $ensureDirectory = false, $append = false) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function append($filename, $data, $mode = null, $ensureDirectory = false) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function localUri($filename) |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function delete($filename) |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | * |
||
165 | * @see http://stackoverflow.com/questions/3349753/delete-directory-with-files-in-it |
||
166 | * @param string $directory |
||
167 | * @param bool $contentOnly |
||
168 | * @throws FilesException |
||
169 | */ |
||
170 | public function deleteDirectory($directory, $contentOnly = false) |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function move($filename, $destination) |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function copy($filename, $destination) |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function touch($filename, $mode = null, $ensureLocation = false) |
||
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | public function exists($filename) |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function size($filename) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function extension($filename) |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function md5($filename) |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function time($filename) |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function isDirectory($filename) |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function isFile($filename) |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | public function getPermissions($filename) |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function setPermissions($filename, $mode) |
||
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | * |
||
321 | * @param Finder $finder Optional initial finder. |
||
322 | */ |
||
323 | public function getFiles($location, $pattern = null, Finder $finder = null) |
||
342 | |||
343 | /** |
||
344 | * {@inheritdoc} |
||
345 | */ |
||
346 | public function tempFilename($extension = '', $location = null) |
||
362 | |||
363 | /** |
||
364 | * {@inheritdoc} |
||
365 | */ |
||
366 | public function normalizePath($path, $directory = false) |
||
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | * |
||
377 | * @link http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php |
||
378 | */ |
||
379 | public function relativePath($path, $from) |
||
409 | |||
410 | /** |
||
411 | * Destruct every temporary file. |
||
412 | */ |
||
413 | public function __destruct() |
||
419 | } |