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 |
||
29 | class File |
||
30 | { |
||
31 | const REGEX_BINARY_FILE = '/^(.*?)\.(gif|jpg|jpeg|png|webp|ico|mp3|mp4|mov|avi|flv|mpg|mpeg|wmv|ogg|ogv|webm|pdf|ttf|eot|woff|svg|swf)$/i'; |
||
32 | |||
33 | const RENAME_FUNC_NAME = 'rename'; |
||
34 | const COPY_FUNC_NAME = 'copy'; |
||
35 | |||
36 | const DIR_HANDLE_FUNC_NAMES = [ |
||
37 | self::RENAME_FUNC_NAME, |
||
38 | self::COPY_FUNC_NAME |
||
39 | ]; |
||
40 | |||
41 | const WILDCARD_SYMBOL = '*'; |
||
42 | |||
43 | // End Of Line relative to the operating system |
||
44 | const EOL = PHP_EOL; |
||
45 | |||
46 | /** |
||
47 | * Mime Types list. |
||
48 | * |
||
49 | * @var array $aMimeTypes |
||
50 | */ |
||
51 | private static $aMimeTypes = [ |
||
52 | 'pdf' => 'application/pdf', |
||
53 | 'txt' => 'text/plain', |
||
54 | 'html' => 'text/html', |
||
55 | 'htm' => 'text/html', |
||
56 | 'exe' => 'application/octet-stream', |
||
57 | 'zip' => 'application/zip', |
||
58 | 'doc' => 'application/msword', |
||
59 | 'xls' => 'application/vnd.ms-excel', |
||
60 | 'ppt' => 'application/vnd.ms-powerpoint', |
||
61 | 'gif' => 'image/gif', |
||
62 | 'png' => 'image/png', |
||
63 | 'jpeg' => 'image/jpg', |
||
64 | 'jpg' => 'image/jpg', |
||
65 | 'webp' => 'image/webp', |
||
66 | 'ico' => 'image/x-icon', |
||
67 | 'eot' => 'application/vnd.ms-fontobject', |
||
68 | 'otf' => 'application/octet-stream', |
||
69 | 'ttf' => 'application/octet-stream', |
||
70 | 'woff' => 'application/octet-stream', |
||
71 | 'svg' => 'application/octet-stream', |
||
72 | 'swf' => 'application/x-shockwave-flash', |
||
73 | 'mp3' => 'audio/mpeg', |
||
74 | 'mp4' => 'video/mp4', |
||
75 | 'webm' => 'video/webm', |
||
76 | 'mov' => 'video/quicktime', |
||
77 | 'avi' => 'video/x-msvideo', |
||
78 | 'php' => 'text/plain', |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * @param string $sExt Extension File. |
||
83 | * |
||
84 | * @return string (string | null) Returns the "mime type" if it is found, otherwise "null" |
||
85 | */ |
||
86 | public function getMimeType($sExt) |
||
90 | |||
91 | /** |
||
92 | * Get Extension file without the dot. |
||
93 | * |
||
94 | * @param string $sFile The File Name. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getFileExt($sFile) |
||
102 | |||
103 | /** |
||
104 | * Get File without Extension and dot. |
||
105 | * This function is smarter than just a code like this, substr($sFile,0,strpos($sFile,'.')) |
||
106 | * Just look at the example below for you to realize that the function removes only the extension and nothing else! |
||
107 | * Example 1 "my_file.pl" The return value is "my_file" |
||
108 | * Example 2 "my_file.inc.pl" The return value is "my_file.inc" |
||
109 | * Example 3 "my_file.class.html.php" The return value is "my_file.class.html" |
||
110 | * |
||
111 | * @see File::getFileExt() To see the method that retrieves the file extension. |
||
112 | * |
||
113 | * @param string $sFile |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getFileWithoutExt($sFile) |
||
123 | |||
124 | /** |
||
125 | * Get File Contents. |
||
126 | * |
||
127 | * @param string $sFile File name. |
||
128 | * @param bool $bIncPath Default FALSE |
||
129 | * |
||
130 | * @return string|bool Returns the read data or FALSE on failure. |
||
131 | */ |
||
132 | public function getFile($sFile, $bIncPath = false) |
||
136 | |||
137 | /** |
||
138 | * Put File Contents. |
||
139 | * |
||
140 | * @param string $sFile File name. |
||
141 | * @param string $sContents Contents file. |
||
142 | * @param int $iFlag Constant (see http://php.net/manual/function.file-put-contents.php). |
||
143 | * |
||
144 | * @return int|bool Returns the number of bytes that were written to the file, or FALSE on failure. |
||
145 | */ |
||
146 | public function putFile($sFile, $sContents, $iFlag = 0) |
||
150 | |||
151 | /** |
||
152 | * Check if file exists. |
||
153 | * |
||
154 | * @param array|string $mFile |
||
155 | * |
||
156 | * @return bool TRUE if file exists, FALSE otherwise. |
||
157 | */ |
||
158 | public function existFile($mFile) |
||
174 | |||
175 | /** |
||
176 | * Check if directory exists. |
||
177 | * |
||
178 | * @param array|string $mDir |
||
179 | * |
||
180 | * @return bool TRUE if file exists, FALSE otherwise. |
||
181 | */ |
||
182 | public function existDir($mDir) |
||
198 | |||
199 | /** |
||
200 | * @param string $sDir The directory. |
||
201 | * |
||
202 | * @return array The list of the folder that is in the directory. |
||
203 | */ |
||
204 | public function getDirList($sDir) |
||
221 | |||
222 | /** |
||
223 | * Get file size. |
||
224 | * |
||
225 | * @param string $sFile |
||
226 | * |
||
227 | * @return int The size of the file in bytes. |
||
228 | */ |
||
229 | public function size($sFile) |
||
233 | |||
234 | /** |
||
235 | * @param string $sDir |
||
236 | * @param string|array|null $mExt Retrieves only files with specific extensions. |
||
237 | * |
||
238 | * @return array List of files sorted alphabetically. |
||
239 | */ |
||
240 | public function getFileList($sDir, $mExt = null) |
||
271 | |||
272 | /** |
||
273 | * Make sure that folder names have a trailing. |
||
274 | * |
||
275 | * @param string $sDir The directory. |
||
276 | * @param bool $bStart for check extension directory start. Default FALSE |
||
277 | * @param bool $bEnd for check extension end. Default TRUE |
||
278 | * |
||
279 | * @return string $sDir Directory |
||
280 | */ |
||
281 | public function checkExtDir($sDir, $bStart = false, $bEnd = true) |
||
295 | |||
296 | /** |
||
297 | * Creates a directory if they are in an array. If it does not exist and |
||
298 | * allows the creation of nested directories specified in the pathname. |
||
299 | * |
||
300 | * @param string|array $mDir |
||
301 | * @param int (octal) $iMode Default: 0777 |
||
302 | * |
||
303 | * @return void |
||
304 | * |
||
305 | * @throws PermissionException If the file cannot be created. |
||
306 | */ |
||
307 | public function createDir($mDir, $iMode = Chmod::MODE_ALL_EXEC) |
||
325 | |||
326 | /** |
||
327 | * Copy files and checks if the "from file" exists. |
||
328 | * |
||
329 | * @param string $sFrom File. |
||
330 | * @param string $sTo File. |
||
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function copy($sFrom, $sTo) |
||
342 | |||
343 | /** |
||
344 | * Copy the contents of a directory into another. |
||
345 | * |
||
346 | * @param string $sFrom Old directory. |
||
347 | * @param string $sTo New directory. |
||
348 | * |
||
349 | * @return bool TRUE if everything went well, otherwise FALSE if the "from directory" couldn't be found or if it couldn't be copied. |
||
350 | * |
||
351 | * @throws PH7InvalidArgumentException |
||
352 | */ |
||
353 | public function copyDir($sFrom, $sTo) |
||
357 | |||
358 | /** |
||
359 | * Copy a file or directory with the Unix cp command. |
||
360 | * |
||
361 | * @param string $sFrom File or directory. |
||
362 | * @param string $sTo File or directory. |
||
363 | * |
||
364 | * @return int|bool Returns the last line on success, and FALSE on failure. |
||
365 | */ |
||
366 | public function systemCopy($sFrom, $sTo) |
||
374 | |||
375 | /** |
||
376 | * Rename a file or directory and checks if the "from file" or directory exists with file_exists() function |
||
377 | * since it checks the existence of a file or directory (because, as in the Unix OS, a directory is a file). |
||
378 | * |
||
379 | * @param string $sFrom File or directory. |
||
380 | * @param string $sTo File or directory. |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function rename($sFrom, $sTo) |
||
392 | |||
393 | /** |
||
394 | * Rename the contents of a directory into another. |
||
395 | * |
||
396 | * @param string $sFrom Old directory. |
||
397 | * @param string $sTo New directory. |
||
398 | * |
||
399 | * @return bool TRUE if everything went well, otherwise FALSE if the "from directory" couldn't be found or if it couldn't be renamed. |
||
400 | * |
||
401 | * @throws PH7InvalidArgumentException |
||
402 | */ |
||
403 | public function renameDir($sFrom, $sTo) |
||
407 | |||
408 | /** |
||
409 | * Rename a file or directory with the Unix mv command. |
||
410 | * |
||
411 | * @param string $sFrom File or directory. |
||
412 | * @param string $sTo File or directory. |
||
413 | * |
||
414 | * @return int|bool Returns the last line on success, and FALSE on failure. |
||
415 | */ |
||
416 | public function systemRename($sFrom, $sTo) |
||
424 | |||
425 | /** |
||
426 | * Deletes a file or files if they are in an array. |
||
427 | * If the file does not exist, the function does nothing. |
||
428 | * |
||
429 | * @param string|array $mFile |
||
430 | * |
||
431 | * @return void |
||
432 | */ |
||
433 | public function deleteFile($mFile) |
||
445 | |||
446 | /** |
||
447 | * For deleting Directory and files! |
||
448 | * A "rmdir" function improved PHP which also delete files in a directory. |
||
449 | * |
||
450 | * @param string $sPath The path |
||
451 | * |
||
452 | * @return bool |
||
453 | */ |
||
454 | public function deleteDir($sPath) |
||
458 | |||
459 | /** |
||
460 | * Remove the contents of a directory. |
||
461 | * |
||
462 | * @param string $sDir |
||
463 | * |
||
464 | * @return void |
||
465 | */ |
||
466 | public function remove($sDir) |
||
476 | |||
477 | /** |
||
478 | * Clean paths if wildcard is found in order to get valid paths. |
||
479 | * |
||
480 | * @param string $sPath |
||
481 | * |
||
482 | * @return string |
||
483 | */ |
||
484 | public function removeWildcards($sPath) |
||
488 | |||
489 | /** |
||
490 | * Get the creation/modification time of a file in the Unix timestamp. |
||
491 | * |
||
492 | * @param string $sFile Full path of the file. |
||
493 | * |
||
494 | * @return int|bool Returns the time the file was last modified, or FALSE if it not found. |
||
495 | */ |
||
496 | public function getModifTime($sFile) |
||
500 | |||
501 | /** |
||
502 | * Get the version of a file based on the its latest modification. |
||
503 | * Shortened form of self::getModifTime() |
||
504 | * |
||
505 | * @param string $sFile Full path of the file. |
||
506 | * |
||
507 | * @return int Returns the latest modification time of the file in Unix timestamp. |
||
508 | */ |
||
509 | public static function version($sFile) |
||
513 | |||
514 | /** |
||
515 | * Delay script execution. |
||
516 | * |
||
517 | * @param int $iSleep Halt time in seconds. |
||
518 | * |
||
519 | * @return int|bool Returns 0 on success, or FALSE on error. |
||
520 | */ |
||
521 | public function sleep($iSleep = 5) |
||
525 | |||
526 | /** |
||
527 | * Changes permission on a file or directory. |
||
528 | * |
||
529 | * @param string $sFile |
||
530 | * @param int $iMode Octal Permission for the file. |
||
531 | * |
||
532 | * @return bool |
||
533 | */ |
||
534 | public function chmod($sFile, $iMode) |
||
543 | |||
544 | /** |
||
545 | * @param string $sFile |
||
546 | * |
||
547 | * @return string Octal Permissions. |
||
548 | */ |
||
549 | public function getOctalAccess($sFile) |
||
554 | |||
555 | /** |
||
556 | * @param string $sData |
||
557 | * |
||
558 | * @return string |
||
559 | */ |
||
560 | public function pack($sData) |
||
564 | |||
565 | /** |
||
566 | * Get the size of a directory. |
||
567 | * |
||
568 | * @param string $sPath |
||
569 | * |
||
570 | * @return int The size of the file in bytes. |
||
571 | */ |
||
572 | public function getDirSize($sPath) |
||
598 | |||
599 | /** |
||
600 | * Get free space of a directory. |
||
601 | * |
||
602 | * @param string $sPath |
||
603 | * |
||
604 | * @return float The number of available bytes as a float. |
||
605 | */ |
||
606 | public function getDirFreeSpace($sPath) |
||
610 | |||
611 | /** |
||
612 | * @param string $sData |
||
613 | * |
||
614 | * @return bool|int|float|string|array|object |
||
615 | */ |
||
616 | public function unpack($sData) |
||
620 | |||
621 | /** |
||
622 | * For download file. |
||
623 | * |
||
624 | * @param string $sFile File to download. |
||
625 | * @param string $sName A name for the file to download. |
||
626 | * @param string|null $sMimeType |
||
627 | * |
||
628 | * @return void |
||
629 | */ |
||
630 | public function download($sFile, $sName, $sMimeType = null) |
||
669 | |||
670 | /** |
||
671 | * Write Header Contents. |
||
672 | * |
||
673 | * @param string $sHeader Text to be shown in the headers |
||
674 | * @param array $aFile |
||
675 | * |
||
676 | * @return void |
||
677 | */ |
||
678 | public function writeHeader($sHeader, array $aFile = []) |
||
690 | |||
691 | /** |
||
692 | * Writes and saves the contents to a file. |
||
693 | * It also creates a temporary file to not delete the original file if something goes wrong during the recording file. |
||
694 | * |
||
695 | * @param string $sFile |
||
696 | * @param string $sData |
||
697 | * |
||
698 | * @return int Returns the number of bytes written, or NULL on error. |
||
699 | */ |
||
700 | public function save($sFile, $sData) |
||
715 | |||
716 | /** |
||
717 | * @param string $sPath |
||
718 | * @param array|string $mFiles |
||
719 | * |
||
720 | * @return array|string The Files. |
||
721 | */ |
||
722 | public function readFiles($sPath = './', &$mFiles) |
||
741 | |||
742 | /** |
||
743 | * Reading Directories. |
||
744 | * |
||
745 | * @param string $sPath |
||
746 | * |
||
747 | * @return array|bool Returns an ARRAY with the folders or FALSE if the folder could not be opened. |
||
748 | */ |
||
749 | public function readDirs($sPath = './') |
||
768 | |||
769 | /** |
||
770 | * Get the URL contents (For URLs, it is better to use CURL because it is faster than file_get_contents function). |
||
771 | * |
||
772 | * @param string $sUrl URL to be read contents. |
||
773 | * |
||
774 | * @return string|bool Return the result content on success, FALSE on failure. |
||
775 | */ |
||
776 | public function getUrlContents($sUrl) |
||
789 | |||
790 | /** |
||
791 | * Extract Zip archive. |
||
792 | * |
||
793 | * @param string $sFile Zip file. |
||
794 | * @param string $sDir Destination to extract the file. |
||
795 | * |
||
796 | * @return bool |
||
797 | */ |
||
798 | public function zipExtract($sFile, $sDir) |
||
811 | |||
812 | /** |
||
813 | * Check if the file is binary. |
||
814 | * |
||
815 | * @param string $sFile |
||
816 | * |
||
817 | * @return bool |
||
818 | */ |
||
819 | public function isBinary($sFile) |
||
846 | |||
847 | /** |
||
848 | * Create a recurive directory iterator for a given directory. |
||
849 | * |
||
850 | * @param string $sPath |
||
851 | * |
||
852 | * @return RecursiveDirectoryIterator |
||
853 | */ |
||
854 | private function getDirIterator($sPath) |
||
858 | |||
859 | /** |
||
860 | * Recursive Directory Iterator. |
||
861 | * |
||
862 | * @param string $sFrom Directory. |
||
863 | * @param string $sTo Directory. |
||
864 | * @param string $sFuncName The function name. Choose between 'copy' and 'rename'. |
||
865 | * |
||
866 | * @return bool |
||
867 | * |
||
868 | * @throws PH7InvalidArgumentException If the function name is invalid. |
||
869 | * @throws PermissionException If the directory cannot be created |
||
870 | * |
||
871 | */ |
||
872 | private function recursiveDirIterator($sFrom, $sTo, $sFuncName) |
||
900 | } |
||
901 |
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.