Total Complexity | 54 |
Total Lines | 212 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like FilesManagement 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.
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 FilesManagement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | trait FilesManagement |
||
21 | { |
||
22 | /** |
||
23 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
24 | * |
||
25 | * @param string $folder The full path of the directory to check |
||
26 | */ |
||
27 | public static function createFolder(string $folder): void |
||
39 | } |
||
40 | } |
||
41 | |||
42 | public static function copyFile(string $file, string $folder): bool |
||
45 | } |
||
46 | |||
47 | public static function recurseCopy(string $src, string $dst): void |
||
48 | { |
||
49 | $dir = \opendir($src); |
||
50 | // @mkdir($dst); |
||
51 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
52 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
53 | } |
||
54 | while (false !== ($file = \readdir($dir))) { |
||
55 | if (('.' !== $file) && ('..' !== $file)) { |
||
56 | if (\is_dir($src . '/' . $file)) { |
||
57 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
58 | } else { |
||
59 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | \closedir($dir); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Remove files and (sub)directories |
||
68 | * |
||
69 | * @param string $src source directory to delete |
||
70 | * |
||
71 | * @return bool true on success |
||
72 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
73 | * |
||
74 | * @uses \Xmf\Module\Helper::getHelper() |
||
75 | */ |
||
76 | public static function deleteDirectory(string $src): bool |
||
77 | { |
||
78 | // Only continue if user is a 'global' Admin |
||
79 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | $success = true; |
||
84 | // remove old files |
||
85 | $dirInfo = new \SplFileInfo($src); |
||
86 | // validate is a directory |
||
87 | if ($dirInfo->isDir()) { |
||
88 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
89 | foreach ($fileList as $k => $v) { |
||
90 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
91 | if ($fileInfo->isDir()) { |
||
92 | // recursively handle subdirectories |
||
93 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
94 | break; |
||
95 | } |
||
96 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) { |
||
97 | break; |
||
98 | } |
||
99 | } |
||
100 | // now delete this (sub)directory if all the files are gone |
||
101 | if ($success) { |
||
102 | $success = \rmdir($dirInfo->getRealPath()); |
||
103 | } |
||
104 | } else { |
||
105 | // input is not a valid directory |
||
106 | $success = false; |
||
107 | } |
||
108 | |||
109 | return $success; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Recursively remove directory |
||
114 | * |
||
115 | * @todo currently won't remove directories with hidden files, should it? |
||
116 | * |
||
117 | * @param string $src directory to remove (delete) |
||
118 | * |
||
119 | * @return bool true on success |
||
120 | */ |
||
121 | public static function rrmdir(string $src): bool |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Recursively move files from one directory to another |
||
155 | * |
||
156 | * @param string $src - Source of files being moved |
||
157 | * @param string $dest - Destination of files being moved |
||
158 | * |
||
159 | * @return bool true on success |
||
160 | */ |
||
161 | public static function rmove(string $src, string $dest): bool |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Recursively copy directories and files from one directory to another |
||
195 | * |
||
196 | * @param string $src - Source of files being moved |
||
197 | * @param string $dest - Destination of files being moved |
||
198 | * |
||
199 | * @return bool true on success |
||
200 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
201 | * |
||
202 | * @uses \Xmf\Module\Helper::getHelper() |
||
203 | */ |
||
204 | public static function rcopy(string $src, string $dest): bool |
||
232 | } |
||
233 | } |
||
234 |