Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class File |
||
24 | { |
||
25 | // End Of Line relative to the operating system |
||
26 | const EOL = PHP_EOL; |
||
27 | |||
28 | /** |
||
29 | * Mime Types list. |
||
30 | * |
||
31 | * @staticvar array $_aMimeTypes |
||
32 | */ |
||
33 | private static $_aMimeTypes = [ |
||
34 | 'pdf' => 'application/pdf', |
||
35 | 'txt' => 'text/plain', |
||
36 | 'html' => 'text/html', |
||
37 | 'htm' => 'text/html', |
||
38 | 'exe' => 'application/octet-stream', |
||
39 | 'zip' => 'application/zip', |
||
40 | 'doc' => 'application/msword', |
||
41 | 'xls' => 'application/vnd.ms-excel', |
||
42 | 'ppt' => 'application/vnd.ms-powerpoint', |
||
43 | 'gif' => 'image/gif', |
||
44 | 'png' => 'image/png', |
||
45 | 'jpeg' => 'image/jpg', |
||
46 | 'jpg' => 'image/jpg', |
||
47 | 'ico' => 'image/x-icon', |
||
48 | 'eot' => 'application/vnd.ms-fontobject', |
||
49 | 'otf' => 'application/octet-stream', |
||
50 | 'ttf' => 'application/octet-stream', |
||
51 | 'woff' => 'application/octet-stream', |
||
52 | 'svg' => 'application/octet-stream', |
||
53 | 'swf' => 'application/x-shockwave-flash', |
||
54 | 'mp3' => 'audio/mpeg', |
||
55 | 'mp4' => 'video/mp4', |
||
56 | 'mov' => 'video/quicktime', |
||
57 | 'avi' => 'video/x-msvideo', |
||
58 | 'php' => 'text/plain', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @param string $sExt Extension File. |
||
63 | * @return string (string | null) Returns the "mime type" if it is found, otherwise "null" |
||
64 | */ |
||
65 | public function getMimeType($sExt) |
||
69 | |||
70 | /** |
||
71 | * Get Extension file without the dot. |
||
72 | * |
||
73 | * @param string $sFile The File Name. |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getFileExt($sFile) |
||
80 | |||
81 | /** |
||
82 | * Get File without Extension and dot. |
||
83 | * This function is smarter than just a code like this, substr($sFile,0,strpos($sFile,'.')) |
||
84 | * Just look at the example below for you to realize that the function removes only the extension and nothing else! |
||
85 | * Example 1 "my_file.pl" The return value is "my_file" |
||
86 | * Example 2 "my_file.inc.pl" The return value is "my_file.inc" |
||
87 | * Example 3 "my_file.class.html.php" The return value is "my_file.class.html" |
||
88 | * |
||
89 | * @see File::getFileExt() To see the method that retrieves the file extension. |
||
90 | * @param string $sFile |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getFileWithoutExt($sFile) |
||
98 | |||
99 | /** |
||
100 | * Get File Contents. |
||
101 | * |
||
102 | * @param string $sFile File name. |
||
103 | * @param boolean $bIncPath Default FALSE |
||
104 | * @return mixed (string | boolean) Returns the read data or FALSE on failure. |
||
105 | */ |
||
106 | public function getFile($sFile, $bIncPath = false) |
||
110 | |||
111 | /** |
||
112 | * Put File Contents. |
||
113 | * |
||
114 | * @param string $sFile File name. |
||
115 | * @param string $sContents Contents file. |
||
116 | * @param integer $iFlag Constant (see http://php.net/manual/function.file-put-contents.php). Default 0 |
||
117 | * @return mixed (integer | boolean) Returns the number of bytes that were written to the file, or FALSE on failure. |
||
118 | */ |
||
119 | public function putFile($sFile, $sContents, $iFlag = 0) |
||
123 | |||
124 | /** |
||
125 | * Check if file exists. |
||
126 | * |
||
127 | * @param mixed (array | string) $mFile |
||
128 | * @return boolean TRUE if file exists, FALSE otherwise. |
||
129 | */ |
||
130 | public function existFile($mFile) |
||
149 | |||
150 | /** |
||
151 | * Check if directory exists. |
||
152 | * |
||
153 | * @param mixed (array | string) $mDir |
||
154 | * @return boolean TRUE if file exists, FALSE otherwise. |
||
155 | */ |
||
156 | public function existDir($mDir) |
||
175 | |||
176 | /** |
||
177 | * @param string $sDir The directory. |
||
178 | * @return array The list of the folder that is in the directory. |
||
179 | */ |
||
180 | public function getDirList($sDir) |
||
197 | |||
198 | /** |
||
199 | * Get file size. |
||
200 | * |
||
201 | * @param string $sFile |
||
202 | * @return integer The size of the file in bytes. |
||
203 | */ |
||
204 | public function size($sFile) |
||
208 | |||
209 | /** |
||
210 | * @param string $sDir |
||
211 | * @param mixed (string | array) $mExt Optional, retrieves only files with specific extensions. Default value is NULL. |
||
212 | * @return array List of files sorted alphabetically. |
||
213 | */ |
||
214 | public function getFileList($sDir, $mExt = null) |
||
253 | |||
254 | /** |
||
255 | * Make sure that folder names have a trailing. |
||
256 | * |
||
257 | * @param string $sDir The directory. |
||
258 | * @param bool $bStart for check extension directory start. Default FALSE |
||
259 | * @param bool $bEnd for check extension end. Default TRUE |
||
260 | * @return string $sDir Directory |
||
261 | */ |
||
262 | public function checkExtDir($sDir, $bStart = false, $bEnd = true) |
||
274 | |||
275 | /** |
||
276 | * Creates a directory if they are in an array. If it does not exist and |
||
277 | * allows the creation of nested directories specified in the pathname. |
||
278 | * |
||
279 | * @param mixed (string | array) $mDir |
||
280 | * @param integer (octal) $iMode Default: 0777 |
||
281 | * @return void |
||
282 | * @throws Exception If the file cannot be created. |
||
283 | */ |
||
284 | public function createDir($mDir, $iMode = 0777) |
||
297 | |||
298 | /** |
||
299 | * Copy files and checks if the "from file" exists. |
||
300 | * |
||
301 | * @param string $sFrom File. |
||
302 | * @param string $sTo File. |
||
303 | * @return boolean |
||
304 | */ |
||
305 | public function copy($sFrom, $sTo) |
||
311 | |||
312 | /** |
||
313 | * Copy the contents of a directory into another. |
||
314 | * |
||
315 | * @param string $sFrom Old directory. |
||
316 | * @param string $sTo New directory. |
||
317 | * @return boolean TRUE if everything went well, otherwise FALSE if the "from directory" couldn't be found or if it couldn't be copied. |
||
318 | */ |
||
319 | public function copyDir($sFrom, $sTo) |
||
323 | |||
324 | /** |
||
325 | * Copy a file or directory with the Unix cp command. |
||
326 | * |
||
327 | * @param string $sFrom File or directory. |
||
328 | * @param string $sTo File or directory. |
||
329 | * @return mixed (integer | boolean) Returns the last line on success, and FALSE on failure. |
||
330 | */ |
||
331 | public function systemCopy($sFrom, $sTo) |
||
338 | |||
339 | /** |
||
340 | * Rename a file or directory and checks if the "from file" or directory exists with file_exists() function |
||
341 | * since it checks the existance of a file or directory (because, as in the Unix OS, a directory is a file). |
||
342 | * |
||
343 | * @param string $sFrom File or directory. |
||
344 | * @param string $sTo File or directory. |
||
345 | * @return boolean |
||
346 | */ |
||
347 | public function rename($sFrom, $sTo) |
||
353 | |||
354 | /** |
||
355 | * Rename the contents of a directory into another. |
||
356 | * |
||
357 | * @param string $sFrom Old directory. |
||
358 | * @param string $sTo New directory. |
||
359 | * @return boolean TRUE if everything went well, otherwise FALSE if the "from directory" couldn't be found or if it couldn't be renamed. |
||
360 | */ |
||
361 | public function renameDir($sFrom, $sTo) |
||
365 | |||
366 | /** |
||
367 | * Rename a file or directory with the Unix mv command. |
||
368 | * |
||
369 | * @param string $sFrom File or directory. |
||
370 | * @param string $sTo File or directory. |
||
371 | * @return mixed (integer | boolean) Returns the last line on success, and FALSE on failure. |
||
372 | */ |
||
373 | public function systemRename($sFrom, $sTo) |
||
380 | |||
381 | /** |
||
382 | * Deletes a file or files if they are in an array. |
||
383 | * If the file does not exist, the function does nothing. |
||
384 | * |
||
385 | * @param mixed (string | array) $mFile |
||
386 | * @return void |
||
387 | */ |
||
388 | public function deleteFile($mFile) |
||
395 | |||
396 | /** |
||
397 | * For deleting Directory and files! |
||
398 | * A "rmdir" function improved PHP which also delete files in a directory. |
||
399 | * |
||
400 | * @param string $sPath The path |
||
401 | * @return boolean |
||
402 | */ |
||
403 | public function deleteDir($sPath) |
||
407 | |||
408 | /** |
||
409 | * Remove the contents of a directory. |
||
410 | * |
||
411 | * @param string $sDir |
||
412 | * @return void |
||
413 | */ |
||
414 | public function remove($sDir) |
||
420 | |||
421 | /** |
||
422 | * Get the creation/modification time of a file in the Unix timestamp. |
||
423 | * |
||
424 | * @param string Full path of the file. |
||
425 | * @return mixed (integer | boolean) Returns the time the file was last modified, or FALSE if it not found. |
||
426 | */ |
||
427 | public function getModifTime($sFile) |
||
431 | |||
432 | /** |
||
433 | * Get the version of a file based on the its latest modification. |
||
434 | * Shortened form of self::getModifTime() |
||
435 | * |
||
436 | * @param string Full path of the file. |
||
437 | * @return integer Returns the latest modification time of the file in Unix timestamp. |
||
438 | */ |
||
439 | public static function version($sFile) |
||
443 | |||
444 | /** |
||
445 | * Delay script execution. |
||
446 | * |
||
447 | * @param integer $iSleep Halt time in seconds. Optional parameter, default value is 5. |
||
448 | * @return mixed (integer | boolean) Returns "0" on success, or "false" on error. |
||
449 | */ |
||
450 | public function sleep($iSleep = null) |
||
455 | |||
456 | /** |
||
457 | * Changes permission on a file or directory. |
||
458 | * |
||
459 | * @param string $sFile |
||
460 | * @param integer $iMode Octal Permission for the file. |
||
461 | * @return boolean |
||
462 | */ |
||
463 | public function chmod($sFile, $iMode) |
||
471 | |||
472 | /** |
||
473 | * @param string $sFile |
||
474 | * @return string Octal Permissions. |
||
475 | */ |
||
476 | public function getOctalAccess($sFile) |
||
481 | |||
482 | /** |
||
483 | * @param string $sData |
||
484 | * @return string |
||
485 | */ |
||
486 | public function pack($sData) |
||
490 | |||
491 | /** |
||
492 | * Get the size of a directory. |
||
493 | * |
||
494 | * @param string $sPath |
||
495 | * @return integer The size of the file in bytes. |
||
496 | */ |
||
497 | public function getDirSize($sPath) |
||
519 | |||
520 | /** |
||
521 | * Get free space of a directory. |
||
522 | * |
||
523 | * @param string $sPath |
||
524 | * @return float The number of available bytes as a float. |
||
525 | */ |
||
526 | public function getDirFreeSpace($sPath) |
||
530 | |||
531 | /** |
||
532 | * @param string $sData |
||
533 | * @return mixed (boolean, integer, float, string, array or object) |
||
534 | */ |
||
535 | public function unpack($sData) |
||
539 | |||
540 | /** |
||
541 | * For download file. |
||
542 | * |
||
543 | * @param string $sFile File to download. |
||
544 | * @param string $sName A name for the file to download. |
||
545 | * @param string $sMimeType Optional, default value is NULL. |
||
546 | * @return void |
||
547 | */ |
||
548 | public function download($sFile, $sName, $sMimeType = null) |
||
590 | |||
591 | /** |
||
592 | * Write Header Contents. |
||
593 | * |
||
594 | * @param string $sHeader Text to be shown in the headers |
||
595 | * @param array $aFile |
||
596 | * @return void |
||
597 | */ |
||
598 | public function writeHeader($sHeader, $aFile = array()) |
||
612 | |||
613 | /** |
||
614 | * Writes and saves the contents to a file. |
||
615 | * It also creates a temporary file to not delete the original file if something goes wrong during the recording file. |
||
616 | * |
||
617 | * @param string $sFile |
||
618 | * @param string $sData |
||
619 | * @return integer Returns the number of bytes written, or NULL on error. |
||
620 | */ |
||
621 | public function save($sFile, $sData) |
||
636 | |||
637 | /** |
||
638 | * Reading Files. |
||
639 | * |
||
640 | * @param string $sPath |
||
641 | * @param mixed (array | string) $mFiles |
||
642 | * @return mixed (array | string) The Files. |
||
643 | */ |
||
644 | public function readFiles($sPath = './', &$mFiles) |
||
663 | |||
664 | /** |
||
665 | * Reading Directories. |
||
666 | * |
||
667 | * @param string $sPath |
||
668 | * @return mixed (array | boolean) Returns an ARRAY with the folders or FALSE if the folder could not be opened. |
||
669 | */ |
||
670 | public function readDirs($sPath = './') |
||
687 | |||
688 | /** |
||
689 | * Get the URL contents (For URLs, it is better to use CURL because it is faster than file_get_contents function). |
||
690 | * |
||
691 | * @param string $sUrl URL to be read contents. |
||
692 | * @return mixed (string | boolean) Return the result content on success, FALSE on failure. |
||
693 | */ |
||
694 | public function getUrlContents($sUrl) |
||
707 | |||
708 | /** |
||
709 | * Extract Zip archive. |
||
710 | * |
||
711 | * @param string $sFile Zip file. |
||
712 | * @param string $sDir Destination to extract the file. |
||
713 | * @return boolean |
||
714 | */ |
||
715 | public function zipExtract($sFile, $sDir) |
||
728 | |||
729 | /** |
||
730 | * Check if the file is binary. |
||
731 | * |
||
732 | * @param string $sFile |
||
733 | * @return boolean |
||
734 | */ |
||
735 | public function isBinary($sFile) |
||
761 | |||
762 | /** |
||
763 | * Create a recurive directory iterator for a given directory. |
||
764 | * |
||
765 | * @param string $sPath |
||
766 | * @return string The directory. |
||
767 | */ |
||
768 | public function getDirIterator($sPath) |
||
772 | |||
773 | /** |
||
774 | * Recursive Directory Iterator. |
||
775 | * |
||
776 | * @param string $sFuncName The function name. Choose between 'copy' and 'rename'. |
||
777 | * @param string $sFrom Directory. |
||
778 | * @param string $sTo Directory. |
||
779 | * |
||
780 | * @return boolean |
||
781 | * |
||
782 | * @throws PH7InvalidArgumentException If the type is bad. |
||
783 | */ |
||
784 | private function _recursiveDirIterator($sFrom, $sTo, $sFuncName) |
||
811 | } |
||
812 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.