Total Complexity | 50 |
Total Lines | 252 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Utility 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 Utility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Utility |
||
38 | { |
||
39 | use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits |
||
|
|||
40 | |||
41 | use Common\ServerStats; // getServerStats Trait |
||
42 | |||
43 | use Common\FilesManagement; // Files Management Trait |
||
44 | |||
45 | /** |
||
46 | * |
||
47 | * Verifies XOOPS version meets minimum requirements for this module |
||
48 | * @static |
||
49 | * @param \XoopsModule $module |
||
50 | * |
||
51 | * @return bool true if meets requirements, false if not |
||
52 | */ |
||
53 | public static function checkVerXoops($module) |
||
54 | { |
||
55 | $currentVersion = strtolower(str_replace('XOOPS ', '', XOOPS_VERSION)); |
||
56 | $requiredVersion = strtolower($module->getInfo('min_xoops')); |
||
57 | $vc = version_compare($currentVersion, $requiredVersion); |
||
58 | $success = ($vc >= 0); |
||
59 | if (false === $success) { |
||
60 | xoops_loadLanguage('admin', $module->dirname()); |
||
61 | $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_XOOPS, $requiredVersion, $currentVersion)); |
||
62 | } |
||
63 | |||
64 | return $success; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * Verifies PHP version meets minimum requirements for this module |
||
70 | * @static |
||
71 | * @param \XoopsModule $module |
||
72 | * |
||
73 | * @return bool true if meets requirements, false if not |
||
74 | */ |
||
75 | public static function checkVerPHP($module) |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * Remove files and (sub)directories |
||
94 | * |
||
95 | * @param string $src source directory to delete |
||
96 | * |
||
97 | * @return bool true on success |
||
98 | * @see \XoopsModules\Xoopsfaq\Helper::isUserAdmin() |
||
99 | * |
||
100 | * @see \XoopsModules\Xoopsfaq\Helper::getHelper() |
||
101 | */ |
||
102 | public static function deleteDirectory($src) |
||
103 | { |
||
104 | // Only continue if user is a 'global' Admin |
||
105 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
106 | return false; |
||
107 | } |
||
108 | |||
109 | $success = true; |
||
110 | // remove old files |
||
111 | $dirInfo = new \SplFileInfo($src); |
||
112 | // validate is a directory |
||
113 | if ($dirInfo->isDir()) { |
||
114 | $fileList = array_diff(scandir($src), ['..', '.']); |
||
115 | foreach ($fileList as $k => $v) { |
||
116 | $fileInfo = new \SplFileInfo($src . '/' . $v); |
||
117 | if ($fileInfo->isDir()) { |
||
118 | // recursively handle subdirectories |
||
119 | if (!$success = static::deleteDirectory($fileInfo->getRealPath())) { |
||
120 | break; |
||
121 | } |
||
122 | } elseif (!($success = unlink($fileInfo->getRealPath()))) { |
||
123 | break; |
||
124 | } |
||
125 | } |
||
126 | // now delete this (sub)directory if all the files are gone |
||
127 | if ($success) { |
||
128 | $success = rmdir($dirInfo->getRealPath()); |
||
129 | } |
||
130 | } else { |
||
131 | // input is not a valid directory |
||
132 | $success = false; |
||
133 | } |
||
134 | return $success; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * |
||
139 | * Recursively remove directory |
||
140 | * |
||
141 | * @todo currently won't remove directories with hidden files, should it? |
||
142 | * |
||
143 | * @param string $src directory to remove (delete) |
||
144 | * |
||
145 | * @return bool true on success |
||
146 | */ |
||
147 | public static function rrmdir($src) |
||
148 | { |
||
149 | // Only continue if user is a 'global' Admin |
||
150 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
151 | return false; |
||
152 | } |
||
153 | |||
154 | // If source is not a directory stop processing |
||
155 | if (!is_dir($src)) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | // Open the source directory to read in files |
||
160 | $iterator = new \DirectoryIterator($src); |
||
161 | foreach ($iterator as $fObj) { |
||
162 | if ($fObj->isFile()) { |
||
163 | $filename = $fObj->getPathname(); |
||
164 | $fObj = null; // clear this iterator object to close the file |
||
165 | if (!unlink($filename)) { |
||
166 | return false; // couldn't delete the file |
||
167 | } |
||
168 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
169 | // Try recursively on directory |
||
170 | static::rrmdir($fObj->getPathname()); |
||
171 | } |
||
172 | } |
||
173 | $iterator = null; // clear iterator Obj to close file/directory |
||
174 | return rmdir($src); // remove the directory & return results |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Recursively move files from one directory to another |
||
179 | * |
||
180 | * @param String $src - Source of files being moved |
||
181 | * @param String $dest - Destination of files being moved |
||
182 | * |
||
183 | * @return bool true on success |
||
184 | */ |
||
185 | public static function rmove($src, $dest) |
||
186 | { |
||
187 | // Only continue if user is a 'global' Admin |
||
188 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
189 | return false; |
||
190 | } |
||
191 | |||
192 | // If source is not a directory stop processing |
||
193 | if (!is_dir($src)) { |
||
194 | return false; |
||
195 | } |
||
196 | |||
197 | // If the destination directory does not exist and could not be created stop processing |
||
198 | if (!is_dir($dest) && !mkdir($dest, 0755)) { |
||
199 | return false; |
||
200 | } |
||
201 | |||
202 | // Open the source directory to read in files |
||
203 | $iterator = new \DirectoryIterator($src); |
||
204 | foreach ($iterator as $fObj) { |
||
205 | if ($fObj->isFile()) { |
||
206 | rename($fObj->getPathname(), $dest . '/' . $fObj->getFilename()); |
||
207 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
208 | // Try recursively on directory |
||
209 | static::rmove($fObj->getPathname(), $dest . '/' . $fObj->getFilename()); |
||
210 | } |
||
211 | } |
||
212 | $iterator = null; // clear iterator Obj to close file/directory |
||
213 | return rmdir($src); // remove the directory & return results |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Recursively copy directories and files from one directory to another |
||
218 | * |
||
219 | * @param string $src - Source of files being moved |
||
220 | * @param string $dest - Destination of files being moved |
||
221 | * |
||
222 | * @return bool true on success |
||
223 | * @see \XoopsModules\Xoopsfaq\Helper::isUserAdmin() |
||
224 | * |
||
225 | * @see \XoopsModules\Xoopsfaq\Helper::getHelper() |
||
226 | */ |
||
227 | public static function rcopy($src, $dest) |
||
228 | { |
||
229 | // Only continue if user is a 'global' Admin |
||
230 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
231 | return false; |
||
232 | } |
||
233 | |||
234 | // If source is not a directory stop processing |
||
235 | if (!is_dir($src)) { |
||
236 | return false; |
||
237 | } |
||
238 | |||
239 | // If the destination directory does not exist and could not be created stop processing |
||
240 | if (!is_dir($dest) && !mkdir($dest, 0755)) { |
||
241 | return false; |
||
242 | } |
||
243 | |||
244 | // Open the source directory to read in files |
||
245 | $iterator = new \DirectoryIterator($src); |
||
246 | foreach ($iterator as $fObj) { |
||
247 | if ($fObj->isFile()) { |
||
248 | copy($fObj->getPathname(), $dest . '/' . $fObj->getFilename()); |
||
249 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
250 | static::rcopy($fObj->getPathname(), $dest . '/' . $fObj->getFilename()); |
||
251 | } |
||
252 | } |
||
253 | return true; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Render the icon links |
||
258 | * |
||
259 | * @param array $icon_array contains operation=>icon_name as key=>value |
||
260 | * @param mixed $param HTML parameter |
||
261 | * @param mixed $value HTML parameter value to set |
||
262 | * @param mixed $extra are any additional HTML attributes desired for the <a> tag |
||
263 | * @return string |
||
264 | */ |
||
265 | public static function renderIconLinks($icon_array = [], $param, $value = null, $extra = null) |
||
289 | } |
||
290 | } |
||
291 |