Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like XoopsfaqUtility 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 XoopsfaqUtility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | /** |
||
32 | * Xoopsfaq\Utility |
||
33 | * |
||
34 | * Static utility class to provide common functionality |
||
35 | * |
||
36 | */ |
||
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) |
||
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) |
||
266 | { |
||
267 | $moduleDirName = basename(dirname(__DIR__)); |
||
268 | xoops_loadLanguage('admin', $moduleDirName); |
||
269 | $ret = ''; |
||
270 | if (null !== $value) { |
||
271 | foreach ($icon_array as $_op => $icon) { |
||
272 | if (false === strpos($icon, '.')) { |
||
273 | $iconName = $icon; |
||
274 | $iconExt = 'png'; |
||
275 | } else { |
||
276 | $iconName = substr($icon, 0, strlen($icon) - strrchr($icon, '.')); |
||
277 | $iconExt = substr(strrchr($icon, '.'), 1); |
||
278 | } |
||
279 | $url = (!is_numeric($_op)) ? $_op . '?' . $param . '=' . $value : xoops_getenv('SCRIPT_NAME') . '?op=' . $iconName . '&' . $param . '=' . $value; |
||
280 | if (null !== $extra) { |
||
281 | $url .= ' ' . $extra; |
||
282 | } |
||
283 | $title = constant(htmlspecialchars(mb_strtoupper('_XO_LA_' . $iconName), ENT_QUOTES | ENT_HTML5)); |
||
291 |