| Total Complexity | 77 |
| Total Lines | 466 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
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 declare(strict_types=1); |
||
| 21 | class Utility extends Common\SysUtility |
||
| 22 | { |
||
| 23 | //--------------- Custom module methods ----------------------------- |
||
| 24 | /** |
||
| 25 | * @param $permtype |
||
| 26 | * @param $dirname |
||
| 27 | * @return mixed |
||
| 28 | */ |
||
| 29 | public function getItemIds($permtype, $dirname) |
||
| 30 | { |
||
| 31 | global $xoopsUser; |
||
| 32 | |||
| 33 | static $permissions = []; |
||
| 34 | |||
| 35 | if (\is_array($permissions) && \array_key_exists($permtype, $permissions)) { |
||
| 36 | return $permissions[$permtype]; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 40 | |||
| 41 | $moduleHandler = \xoops_getHandler('module'); |
||
| 42 | |||
| 43 | $tdmModule = $moduleHandler->getByDirname($dirname); |
||
| 44 | |||
| 45 | $groups = \is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
||
| 46 | |||
| 47 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 48 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
| 49 | |||
| 50 | return $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid')); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * retourne le nombre de téléchargements dans le catégories enfants d'une catégorie |
||
| 55 | * @param \XoopsModules\Tdmdownloads\Tree $mytree |
||
| 56 | * @param $categories |
||
| 57 | * @param $entries |
||
| 58 | * @param $cid |
||
| 59 | * @return int |
||
| 60 | */ |
||
| 61 | public function getNumbersOfEntries($mytree, $categories, $entries, $cid) |
||
| 62 | { |
||
| 63 | $count = 0; |
||
| 64 | |||
| 65 | $child_arr = []; |
||
|
|
|||
| 66 | |||
| 67 | if (\in_array($cid, $categories)) { |
||
| 68 | $child = $mytree->getAllChild($cid); |
||
| 69 | |||
| 70 | foreach (\array_keys($entries) as $i) { |
||
| 71 | /** @var \XoopsModules\Tdmdownloads\Downloads[] $entries */ |
||
| 72 | |||
| 73 | if ($entries[$i]->getVar('cid') == $cid) { |
||
| 74 | $count++; |
||
| 75 | } |
||
| 76 | |||
| 77 | foreach (\array_keys($child) as $j) { |
||
| 78 | if ($entries[$i]->getVar('cid') == $j) { |
||
| 79 | $count++; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return $count; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * retourne une image "nouveau" ou "mise à jour" |
||
| 90 | * @param $time |
||
| 91 | * @param $status |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getStatusImage($time, $status) |
||
| 95 | { |
||
| 96 | global $xoopsModuleConfig; |
||
| 97 | |||
| 98 | $count = 7; |
||
| 99 | |||
| 100 | $new = ''; |
||
| 101 | |||
| 102 | $startdate = \time() - (86400 * $count); |
||
| 103 | |||
| 104 | if (1 == $xoopsModuleConfig['showupdated']) { |
||
| 105 | if ($startdate < $time) { |
||
| 106 | $language = $GLOBALS['xoopsConfig']['language']; |
||
| 107 | |||
| 108 | if (!\is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) { |
||
| 109 | $language = 'english'; |
||
| 110 | } |
||
| 111 | |||
| 112 | $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/'; |
||
| 113 | |||
| 114 | $img_url = XOOPS_URL . '/modules/tdmdownloads/language/' . $language . '/'; |
||
| 115 | |||
| 116 | if (1 == $status) { |
||
| 117 | if (\is_readable($img_path . 'new.png')) { |
||
| 118 | $new = ' <img src="' . $img_url . 'new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">'; |
||
| 119 | } else { |
||
| 120 | $new = ' <img src="' . XOOPS_URL . '/modules/tdmdownloads/language/english/new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">'; |
||
| 121 | } |
||
| 122 | } elseif (2 == $status) { |
||
| 123 | if (\is_readable($img_path . 'updated.png')) { |
||
| 124 | $new = ' <img src="' . $img_url . 'updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">'; |
||
| 125 | } else { |
||
| 126 | $new = ' <img src="' . XOOPS_URL . '/modules/tdmdownloads/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">'; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return $new; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * retourne une image "populaire" |
||
| 137 | * @param $hits |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getPopularImage($hits) |
||
| 141 | { |
||
| 142 | global $xoopsModuleConfig; |
||
| 143 | |||
| 144 | $pop = ''; |
||
| 145 | |||
| 146 | if ($hits >= $xoopsModuleConfig['popular']) { |
||
| 147 | $language = $GLOBALS['xoopsConfig']['language']; |
||
| 148 | |||
| 149 | if (!\is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) { |
||
| 150 | $language = 'english'; |
||
| 151 | } |
||
| 152 | |||
| 153 | $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/'; |
||
| 154 | |||
| 155 | $img_url = XOOPS_URL . '/modules/tdmdownloads/language/' . $language . '/'; |
||
| 156 | |||
| 157 | if (\is_readable($img_path . 'popular.png')) { |
||
| 158 | $pop = ' <img src="' . $img_url . 'popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">'; |
||
| 159 | } else { |
||
| 160 | $pop = ' <img src ="' . XOOPS_URL . '/modules/tdmdownloads/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">'; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $pop; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param int $size |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public static function prettifyBytes($size) |
||
| 172 | { |
||
| 173 | if ($size > 0) { |
||
| 174 | $mb = 1024 * 1024; |
||
| 175 | |||
| 176 | if ($size > $mb) { |
||
| 177 | $mysize = \sprintf('%01.2f', $size / $mb) . ' MB'; |
||
| 178 | } elseif ($size >= 1024) { |
||
| 179 | $mysize = \sprintf('%01.2f', $size / 1024) . ' KB'; |
||
| 180 | } else { |
||
| 181 | $mysize = \sprintf(_AM_TDMDOWNLOADS_NUMBYTES, $size); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $mysize; |
||
| 185 | } |
||
| 186 | |||
| 187 | return ''; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param $global |
||
| 192 | * @param $key |
||
| 193 | * @param string $default |
||
| 194 | * @param string $type |
||
| 195 | * @return mixed|string |
||
| 196 | */ |
||
| 197 | public static function cleanVars($global, $key, $default = '', $type = 'int') |
||
| 198 | { |
||
| 199 | switch ($type) { |
||
| 200 | case 'string': |
||
| 201 | $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_MAGIC_QUOTES) : $default; |
||
| 202 | break; |
||
| 203 | case 'int': |
||
| 204 | default: |
||
| 205 | $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_NUMBER_INT) : $default; |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | |||
| 209 | if (false === $ret) { |
||
| 210 | return $default; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $ret; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param $mytree |
||
| 218 | * @param $key |
||
| 219 | * @param $category_array |
||
| 220 | * @param $title |
||
| 221 | * @param string $prefix |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public static function getPathTree($mytree, $key, $category_array, $title, $prefix = '') |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param \XoopsModules\Tdmdownloads\Tree $mytree |
||
| 255 | * @param $key |
||
| 256 | * @param $category_array |
||
| 257 | * @param $title |
||
| 258 | * @param string $prefix |
||
| 259 | * @param bool $link |
||
| 260 | * @param string $order |
||
| 261 | * @param bool $lasturl |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public static function getPathTreeUrl($mytree, $key, $category_array, $title, $prefix = '', $link = false, $order = 'ASC', $lasturl = false) |
||
| 265 | { |
||
| 266 | global $xoopsModule; |
||
| 267 | |||
| 268 | $categoryParent = $mytree->getAllParent($key); |
||
| 269 | |||
| 270 | if ('ASC' === $order) { |
||
| 271 | $categoryParent = \array_reverse($categoryParent); |
||
| 272 | |||
| 273 | if ($link) { |
||
| 274 | $path = '<a href="index.php">' . $xoopsModule->name() . '</a>' . $prefix; |
||
| 275 | } else { |
||
| 276 | $path = $xoopsModule->name() . $prefix; |
||
| 277 | } |
||
| 278 | } else { |
||
| 279 | if (\array_key_exists($key, $category_array)) { |
||
| 280 | /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */ |
||
| 281 | |||
| 282 | $firstCategory = $category_array[$key]->getVar($title); |
||
| 283 | } else { |
||
| 284 | $firstCategory = ''; |
||
| 285 | } |
||
| 286 | |||
| 287 | $path = $firstCategory . $prefix; |
||
| 288 | } |
||
| 289 | |||
| 290 | foreach (\array_keys($categoryParent) as $j) { |
||
| 291 | /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */ |
||
| 292 | |||
| 293 | if ($link) { |
||
| 294 | $path .= '<a href="viewcat.php?cid=' . $categoryParent[$j]->getVar('cat_cid') . '">' . $categoryParent[$j]->getVar($title) . '</a>' . $prefix; |
||
| 295 | } else { |
||
| 296 | $path .= $categoryParent[$j]->getVar($title) . $prefix; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | if ('ASC' === $order) { |
||
| 301 | if (\array_key_exists($key, $category_array)) { |
||
| 302 | if ($lasturl) { |
||
| 303 | $firstCategory = '<a href="viewcat.php?cid=' . $category_array[$key]->getVar('cat_cid') . '">' . $category_array[$key]->getVar($title) . '</a>'; |
||
| 304 | } else { |
||
| 305 | $firstCategory = $category_array[$key]->getVar($title); |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | $firstCategory = ''; |
||
| 309 | } |
||
| 310 | |||
| 311 | $path .= $firstCategory; |
||
| 312 | } else { |
||
| 313 | if ($link) { |
||
| 314 | $path .= '<a href="index.php">' . $xoopsModule->name() . '</a>'; |
||
| 315 | } else { |
||
| 316 | $path .= $xoopsModule->name(); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | return $path; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Utility::convertStringToSize() |
||
| 325 | * |
||
| 326 | * @param mixed $stringSize |
||
| 327 | * @return mixed|int |
||
| 328 | */ |
||
| 329 | public static function convertStringToSize($stringSize) |
||
| 330 | { |
||
| 331 | if ('' != $stringSize) { |
||
| 332 | $kb = 1024; |
||
| 333 | |||
| 334 | $mb = 1024 * 1024; |
||
| 335 | |||
| 336 | $gb = 1024 * 1024 * 1024; |
||
| 337 | |||
| 338 | $size_value_arr = \explode(' ', $stringSize); |
||
| 339 | |||
| 340 | if ('B' == $size_value_arr[1]) { |
||
| 341 | $mysize = $size_value_arr[0]; |
||
| 342 | } elseif ('K' == $size_value_arr[1]) { |
||
| 343 | $mysize = $size_value_arr[0] * $kb; |
||
| 344 | } elseif ('M' == $size_value_arr[1]) { |
||
| 345 | $mysize = $size_value_arr[0] * $mb; |
||
| 346 | } else { |
||
| 347 | $mysize = $size_value_arr[0] * $gb; |
||
| 348 | } |
||
| 349 | |||
| 350 | return $mysize; |
||
| 351 | } |
||
| 352 | |||
| 353 | return 0; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Utility::convertSizeToString() |
||
| 358 | * |
||
| 359 | * @param mixed $sizeString |
||
| 360 | * @return mixed|string |
||
| 361 | */ |
||
| 362 | public static function convertSizeToString($sizeString) |
||
| 363 | { |
||
| 364 | $mysizeString = ''; |
||
| 365 | |||
| 366 | if ('' != $sizeString) { |
||
| 367 | $size_value_arr = \explode(' ', $sizeString); |
||
| 368 | |||
| 369 | if (\array_key_exists(0, $size_value_arr) && \array_key_exists(1, $size_value_arr)) { |
||
| 370 | if ('' != $size_value_arr[0]) { |
||
| 371 | $mysizeString = ''; |
||
| 372 | |||
| 373 | switch ($size_value_arr[1]) { |
||
| 374 | case 'B': |
||
| 375 | $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_BYTES; |
||
| 376 | break; |
||
| 377 | case 'K': |
||
| 378 | $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_KBYTES; |
||
| 379 | break; |
||
| 380 | case 'M': |
||
| 381 | $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_MBYTES; |
||
| 382 | break; |
||
| 383 | case 'G': |
||
| 384 | $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_GBYTES; |
||
| 385 | break; |
||
| 386 | case 'T': |
||
| 387 | $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_TBYTES; |
||
| 388 | break; |
||
| 389 | } |
||
| 390 | |||
| 391 | return $mysizeString; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | return $mysizeString; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Utility::getFileSize() |
||
| 401 | * |
||
| 402 | * @param mixed $url |
||
| 403 | * @return mixed|string |
||
| 404 | */ |
||
| 405 | public static function getFileSize($url) |
||
| 406 | { |
||
| 407 | if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init($url))) { |
||
| 408 | \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true); |
||
| 409 | |||
| 410 | \curl_setopt($curlHandle, \CURLOPT_HEADER, true); |
||
| 411 | |||
| 412 | \curl_setopt($curlHandle, \CURLOPT_NOBODY, true); |
||
| 413 | |||
| 414 | \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized' |
||
| 415 | |||
| 416 | $curlReturn = \curl_exec($curlHandle); |
||
| 417 | |||
| 418 | if (false === $curlReturn) { |
||
| 419 | \trigger_error(\curl_error($curlHandle)); |
||
| 420 | |||
| 421 | $size = 0; |
||
| 422 | } else { |
||
| 423 | $size = \curl_getinfo($curlHandle, \CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
||
| 424 | } |
||
| 425 | |||
| 426 | \curl_close($curlHandle); |
||
| 427 | |||
| 428 | if ($size <= 0) { |
||
| 429 | return 0; |
||
| 430 | } |
||
| 431 | |||
| 432 | return self::convertFileSize($size); |
||
| 433 | } |
||
| 434 | |||
| 435 | return 0; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Utility::convertFileSize() |
||
| 440 | * |
||
| 441 | * @param mixed $size |
||
| 442 | * @return mixed|string |
||
| 443 | */ |
||
| 444 | public static function convertFileSize($size) |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param $val |
||
| 471 | * @return float|int |
||
| 472 | */ |
||
| 473 | public static function returnBytes($val) |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } |
||
| 490 |