Total Complexity | 59 |
Total Lines | 332 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Utilities 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 Utilities, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Utilities |
||
21 | { |
||
22 | protected $db; |
||
23 | protected $helper; |
||
24 | /** |
||
25 | * @param mixed $permtype |
||
26 | * @param mixed $dirname |
||
27 | */ |
||
28 | // public static function __construct(\XoopsDatabase $db = null, $helper = null) |
||
29 | // { |
||
30 | // $this->db = $db; |
||
31 | // $this->helper = $helper; |
||
32 | // } |
||
33 | /** |
||
34 | * @param $permtype |
||
35 | * @param $dirname |
||
36 | * @return mixed |
||
37 | */ |
||
38 | public static function getItemIds($permtype, $dirname) |
||
39 | { |
||
40 | global $xoopsUser; |
||
41 | $permissions = []; |
||
42 | if (\is_array($permissions) && \array_key_exists($permtype, $permissions)) { |
||
43 | return $permissions[$permtype]; |
||
44 | } |
||
45 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
46 | $moduleHandler = \xoops_getHandler('module'); |
||
47 | $tdmModule = $moduleHandler->getByDirname($dirname); |
||
48 | $groups = \is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
||
49 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
50 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
51 | return $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid')); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * returns the number of updates downloads in the categories of children category |
||
56 | * |
||
57 | * @param $mytree |
||
58 | * @param $categories |
||
59 | * @param $entries |
||
60 | * @param $cid |
||
61 | * |
||
62 | * @return int |
||
63 | */ |
||
64 | public static function getNumbersOfEntries($mytree, $categories, $entries, $cid) |
||
65 | { |
||
66 | $count = 0; |
||
67 | $child_arr = []; |
||
|
|||
68 | if (\in_array($cid, $categories)) { |
||
69 | $child = $mytree->getAllChild($cid); |
||
70 | foreach (\array_keys($entries) as $i) { |
||
71 | if ($entries[$i]->getVar('cid') === $cid) { |
||
72 | ++$count; |
||
73 | } |
||
74 | foreach (\array_keys($child) as $j) { |
||
75 | if ($entries[$i]->getVar('cid') === $j) { |
||
76 | ++$count; |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | return $count; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * returns an image "new" or "updated" |
||
86 | * @param $time |
||
87 | * @param $status |
||
88 | * @return string |
||
89 | */ |
||
90 | public static function getStatusImage($time, $status) |
||
91 | { |
||
92 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
93 | $helper = Helper::getInstance(); |
||
94 | $count = 7; |
||
95 | $new = ''; |
||
96 | $startdate = \time() - (86400 * $count); |
||
97 | if (1 == $helper->getConfig('showupdated')) { |
||
98 | if ($startdate < $time) { |
||
99 | $language = $GLOBALS['xoopsConfig']['language']; |
||
100 | if (!\is_dir(XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/')) { |
||
101 | $language = 'english'; |
||
102 | } |
||
103 | $img_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/'; |
||
104 | $img_url = XOOPS_URL . "/modules/$moduleDirName/language/" . $language . '/'; |
||
105 | if (1 == $status) { |
||
106 | if (\is_readable($img_path . 'new.png')) { |
||
107 | $new = ' <img src="' . $img_url . 'new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">'; |
||
108 | } else { |
||
109 | $new = ' <img src="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">'; |
||
110 | } |
||
111 | } elseif (2 == $status) { |
||
112 | if (\is_readable($img_path . 'updated.png')) { |
||
113 | $new = ' <img src="' . $img_url . 'updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">'; |
||
114 | } else { |
||
115 | $new = ' <img src="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">'; |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | return $new; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * retourne une image "populaire" |
||
125 | * @param $hits |
||
126 | * @return string |
||
127 | */ |
||
128 | public static function getPopularImage($hits) |
||
129 | { |
||
130 | $helper = Helper::getInstance(); |
||
131 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
132 | $pop = ''; |
||
133 | if ($hits >= $helper->getConfig('popular')) { |
||
134 | $language = $GLOBALS['xoopsConfig']['language']; |
||
135 | if (!\is_dir(XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/')) { |
||
136 | $language = 'english'; |
||
137 | } |
||
138 | $img_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/'; |
||
139 | $img_url = XOOPS_URL . "/modules/$moduleDirName/language/" . $language . '/'; |
||
140 | if (\is_readable($img_path . 'popular.png')) { |
||
141 | $pop = ' <img src="' . $img_url . 'popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">'; |
||
142 | } else { |
||
143 | $pop = ' <img src ="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">'; |
||
144 | } |
||
145 | } |
||
146 | return $pop; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param mixed $global |
||
151 | * @param mixed $key |
||
152 | * @param mixed $default |
||
153 | * @param mixed $type |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | // public static function convertFileSize($size) |
||
158 | // { |
||
159 | // if ($size > 0) { |
||
160 | // $mb = 1024 * 1024; |
||
161 | // if ($size > $mb) { |
||
162 | // $mysize = sprintf("%01.2f", $size / $mb) . " MB"; |
||
163 | // } elseif ($size >= 1024) { |
||
164 | // $mysize = sprintf("%01.2f", $size / 1024) . " KB"; |
||
165 | // } else { |
||
166 | // $mysize = sprintf(_AM_TDMDOWNLOADS_NUMBYTES, $size); |
||
167 | // } |
||
168 | // return $mysize; |
||
169 | // } else { |
||
170 | // return ''; |
||
171 | // } |
||
172 | // } |
||
173 | /** |
||
174 | * @param $global |
||
175 | * @param $key |
||
176 | * @param string $default |
||
177 | * @param string $type |
||
178 | * |
||
179 | * @return mixed|string |
||
180 | */ |
||
181 | public static function cleanVars($global, $key, $default = '', $type = 'int') |
||
182 | { |
||
183 | switch ($type) { |
||
184 | case 'string': |
||
185 | if (\defined('FILTER_SANITIZE_ADD_SLASHES')) { |
||
186 | $ret = isset($global[$key]) ? \filter_var($global[$key]) : $default; |
||
187 | } else { |
||
188 | $ret = isset($global[$key]) ? \filter_var($global[$key]) : $default; |
||
189 | } |
||
190 | break; |
||
191 | case 'int': |
||
192 | default: |
||
193 | $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_NUMBER_INT) : $default; |
||
194 | break; |
||
195 | } |
||
196 | if (false === $ret) { |
||
197 | return $default; |
||
198 | } |
||
199 | return $ret; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param $mytree |
||
204 | * @param $key |
||
205 | * @param $category_array |
||
206 | * @param $title |
||
207 | * @param string $prefix |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public static function getPathTree($mytree, $key, $category_array, $title, $prefix = '') |
||
212 | { |
||
213 | $category_parent = $mytree->getAllParent($key); |
||
214 | $category_parent = \array_reverse($category_parent); |
||
215 | $Path = ''; |
||
216 | foreach (\array_keys($category_parent) as $j) { |
||
217 | $Path .= $category_parent[$j]->getVar($title) . $prefix; |
||
218 | } |
||
219 | $first_category = ''; |
||
220 | if (\array_key_exists($key, $category_array)) { |
||
221 | $first_category = $category_array[$key]->getVar($title); |
||
222 | } |
||
223 | $Path .= $first_category; |
||
224 | return $Path; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param $mytree |
||
229 | * @param $key |
||
230 | * @param $category_array |
||
231 | * @param $title |
||
232 | * @param string $prefix |
||
233 | * @param bool $link |
||
234 | * @param string $order |
||
235 | * @param bool $lasturl |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public static function getPathTreeUrl( |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param $path |
||
295 | * @param int $mode |
||
296 | * @param $fileSource |
||
297 | * @param null $fileTarget |
||
298 | * @throws \RuntimeException |
||
299 | */ |
||
300 | public static function createFolder($path, $mode, $fileSource, $fileTarget = null) |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param $pathSource |
||
314 | * @param $pathTarget |
||
315 | * @throws \RuntimeException |
||
316 | */ |
||
317 | public static function cloneFolder($pathSource, $pathTarget) |
||
318 | { |
||
319 | if (\is_dir($pathSource)) { |
||
320 | // Create new dir |
||
321 | if (!\mkdir($pathTarget) && !\is_dir($pathTarget)) { |
||
322 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $pathTarget)); |
||
323 | } |
||
324 | // check all files in dir, and process it |
||
325 | $handle = \opendir($pathSource); |
||
326 | if ($handle) { |
||
327 | while ($file = \readdir($handle)) { |
||
328 | if ('.' !== $file && '..' !== $file) { |
||
329 | self::cloneFolder("$pathSource/$file", "$pathTarget/$file"); |
||
330 | } |
||
331 | } |
||
332 | \closedir($handle); |
||
333 | } |
||
334 | } else { |
||
335 | \copy($pathSource, $pathTarget); |
||
336 | } |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
341 | * |
||
342 | * @param string $folder Le chemin complet du répertoire à vérifier |
||
343 | * |
||
344 | * @throws \RuntimeException |
||
345 | */ |
||
346 | public static function prepareFolder($folder) |
||
352 | } |
||
353 | } |
||
354 |