Total Complexity | 54 |
Total Lines | 199 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
22 | trait FilesManagement |
||
23 | { |
||
24 | /** |
||
25 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
26 | * |
||
27 | * @param string $folder The full path of the directory to check |
||
28 | */ |
||
29 | public static function createFolder(string $folder): void |
||
30 | { |
||
31 | try { |
||
32 | if (!\is_dir($folder)) { |
||
33 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
34 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
35 | } |
||
36 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
37 | } |
||
38 | } catch (\Exception $e) { |
||
39 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | public static function copyFile(string $file, string $folder): bool |
||
44 | { |
||
45 | return \copy($file, $folder); |
||
46 | } |
||
47 | |||
48 | public static function recurseCopy(string $src, string $dst): void |
||
49 | { |
||
50 | $dir = \opendir($src); |
||
51 | // @mkdir($dst); |
||
52 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
53 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
54 | } |
||
55 | while (false !== ($file = \readdir($dir))) { |
||
56 | if (('.' !== $file) && ('..' !== $file)) { |
||
57 | if (\is_dir($src . '/' . $file)) { |
||
58 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
59 | } else { |
||
60 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | \closedir($dir); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Remove files and (sub)directories |
||
69 | * |
||
70 | * @param string $src source directory to delete |
||
71 | * |
||
72 | * @return bool true on success |
||
73 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
74 | * |
||
75 | * @uses \Xmf\Module\Helper::getHelper() |
||
76 | */ |
||
77 | public static function deleteDirectory(string $src): bool |
||
78 | { |
||
79 | // Only continue if user is a 'global' Admin |
||
80 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
81 | return false; |
||
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 | return $success; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Recursively remove directory |
||
113 | * |
||
114 | * @todo currently won't remove directories with hidden files, should it? |
||
115 | * |
||
116 | * @param string $src directory to remove (delete) |
||
117 | * |
||
118 | * @return bool true on success |
||
119 | */ |
||
120 | public static function rrmdir(string $src): bool |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Recursively move files from one directory to another |
||
151 | * |
||
152 | * @param string $src - Source of files being moved |
||
153 | * @param string $dest - Destination of files being moved |
||
154 | * |
||
155 | * @return bool true on success |
||
156 | */ |
||
157 | public static function rmove(string $src, string $dest): bool |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Recursively copy directories and files from one directory to another |
||
188 | * |
||
189 | * @param string $src - Source of files being moved |
||
190 | * @param string $dest - Destination of files being moved |
||
191 | * |
||
192 | * @return bool true on success |
||
193 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
194 | * |
||
195 | * @uses \Xmf\Module\Helper::getHelper() |
||
196 | */ |
||
197 | public static function rcopy(string $src, string $dest): bool |
||
223 |