| Total Complexity | 59 |
| Total Lines | 416 |
| 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 declare(strict_types=1); |
||
| 18 | class Utilities |
||
| 19 | { |
||
| 20 | protected $db; |
||
| 21 | protected $helper; |
||
| 22 | /** |
||
| 23 | * @param mixed $permtype |
||
| 24 | * @param mixed $dirname |
||
| 25 | */ |
||
| 26 | |||
| 27 | // public static function __construct(\XoopsDatabase $db = null, $helper = null) |
||
| 28 | |||
| 29 | // { |
||
| 30 | |||
| 31 | // $this->db = $db; |
||
| 32 | |||
| 33 | // $this->helper = $helper; |
||
| 34 | |||
| 35 | // } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $permtype |
||
| 39 | * @param $dirname |
||
| 40 | * @return mixed |
||
| 41 | */ |
||
| 42 | public static function getItemIds($permtype, $dirname) |
||
| 43 | { |
||
| 44 | global $xoopsUser; |
||
| 45 | |||
| 46 | $permissions = []; |
||
| 47 | |||
| 48 | if (\is_array($permissions) && \array_key_exists($permtype, $permissions)) { |
||
| 49 | return $permissions[$permtype]; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 53 | $moduleHandler = \xoops_getHandler('module'); |
||
| 54 | |||
| 55 | $tdmModule = $moduleHandler->getByDirname($dirname); |
||
| 56 | |||
| 57 | $groups = \is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
||
| 58 | |||
| 59 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 60 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
| 61 | |||
| 62 | return $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid')); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * returns the number of updates downloads in the categories of children category |
||
| 67 | * |
||
| 68 | * @param $mytree |
||
| 69 | * @param $categories |
||
| 70 | * @param $entries |
||
| 71 | * @param $cid |
||
| 72 | * |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | public static function getNumbersOfEntries($mytree, $categories, $entries, $cid) |
||
| 76 | { |
||
| 77 | $count = 0; |
||
| 78 | |||
| 79 | $child_arr = []; |
||
|
|
|||
| 80 | |||
| 81 | if (\in_array($cid, $categories)) { |
||
| 82 | $child = $mytree->getAllChild($cid); |
||
| 83 | |||
| 84 | foreach (\array_keys($entries) as $i) { |
||
| 85 | if ($entries[$i]->getVar('cid') === $cid) { |
||
| 86 | ++$count; |
||
| 87 | } |
||
| 88 | |||
| 89 | foreach (\array_keys($child) as $j) { |
||
| 90 | if ($entries[$i]->getVar('cid') === $j) { |
||
| 91 | ++$count; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return $count; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * returns an image "new" or "updated" |
||
| 102 | * @param $time |
||
| 103 | * @param $status |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public static function getStatusImage($time, $status) |
||
| 107 | { |
||
| 108 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 109 | |||
| 110 | /** @var Tdmdownloads\Helper $helper */ |
||
| 111 | |||
| 112 | $helper = Tdmdownloads\Helper::getInstance(); |
||
| 113 | |||
| 114 | $count = 7; |
||
| 115 | |||
| 116 | $new = ''; |
||
| 117 | |||
| 118 | $startdate = \time() - (86400 * $count); |
||
| 119 | |||
| 120 | if (1 == $helper->getConfig('showupdated')) { |
||
| 121 | if ($startdate < $time) { |
||
| 122 | $language = $GLOBALS['xoopsConfig']['language']; |
||
| 123 | |||
| 124 | if (!\is_dir(XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/')) { |
||
| 125 | $language = 'english'; |
||
| 126 | } |
||
| 127 | |||
| 128 | $img_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/'; |
||
| 129 | |||
| 130 | $img_url = XOOPS_URL . "/modules/$moduleDirName/language/" . $language . '/'; |
||
| 131 | |||
| 132 | if (1 == $status) { |
||
| 133 | if (\is_readable($img_path . 'new.png')) { |
||
| 134 | $new = ' <img src="' . $img_url . 'new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">'; |
||
| 135 | } else { |
||
| 136 | $new = ' <img src="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">'; |
||
| 137 | } |
||
| 138 | } elseif (2 == $status) { |
||
| 139 | if (\is_readable($img_path . 'updated.png')) { |
||
| 140 | $new = ' <img src="' . $img_url . 'updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">'; |
||
| 141 | } else { |
||
| 142 | $new = ' <img src="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">'; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return $new; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * retourne une image "populaire" |
||
| 153 | * @param $hits |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public static function getPopularImage($hits) |
||
| 157 | { |
||
| 158 | /** @var Tdmdownloads\Helper $helper */ |
||
| 159 | |||
| 160 | $helper = Tdmdownloads\Helper::getInstance(); |
||
| 161 | |||
| 162 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 163 | |||
| 164 | $pop = ''; |
||
| 165 | |||
| 166 | if ($hits >= $helper->getConfig('popular')) { |
||
| 167 | $language = $GLOBALS['xoopsConfig']['language']; |
||
| 168 | |||
| 169 | if (!\is_dir(XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/')) { |
||
| 170 | $language = 'english'; |
||
| 171 | } |
||
| 172 | |||
| 173 | $img_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/'; |
||
| 174 | |||
| 175 | $img_url = XOOPS_URL . "/modules/$moduleDirName/language/" . $language . '/'; |
||
| 176 | |||
| 177 | if (\is_readable($img_path . 'popular.png')) { |
||
| 178 | $pop = ' <img src="' . $img_url . 'popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">'; |
||
| 179 | } else { |
||
| 180 | $pop = ' <img src ="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">'; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return $pop; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param mixed $global |
||
| 189 | * @param mixed $key |
||
| 190 | * @param mixed $default |
||
| 191 | * @param mixed $type |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | |||
| 196 | // public static function convertFileSize($size) |
||
| 197 | |||
| 198 | // { |
||
| 199 | |||
| 200 | // if ($size > 0) { |
||
| 201 | |||
| 202 | // $mb = 1024 * 1024; |
||
| 203 | |||
| 204 | // if ($size > $mb) { |
||
| 205 | |||
| 206 | // $mysize = sprintf("%01.2f", $size / $mb) . " MB"; |
||
| 207 | |||
| 208 | // } elseif ($size >= 1024) { |
||
| 209 | |||
| 210 | // $mysize = sprintf("%01.2f", $size / 1024) . " KB"; |
||
| 211 | |||
| 212 | // } else { |
||
| 213 | |||
| 214 | // $mysize = sprintf(_AM_TDMDOWNLOADS_NUMBYTES, $size); |
||
| 215 | |||
| 216 | // } |
||
| 217 | |||
| 218 | // return $mysize; |
||
| 219 | |||
| 220 | // } else { |
||
| 221 | |||
| 222 | // return ''; |
||
| 223 | |||
| 224 | // } |
||
| 225 | |||
| 226 | // } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param $global |
||
| 230 | * @param $key |
||
| 231 | * @param string $default |
||
| 232 | * @param string $type |
||
| 233 | * |
||
| 234 | * @return mixed|string |
||
| 235 | */ |
||
| 236 | public static function cleanVars($global, $key, $default = '', $type = 'int') |
||
| 237 | { |
||
| 238 | switch ($type) { |
||
| 239 | case 'string': |
||
| 240 | if (\defined('FILTER_SANITIZE_ADD_SLASHES')) { |
||
| 241 | $ret = isset($global[$key]) ? \filter_var($global[$key]) : $default; |
||
| 242 | } else { |
||
| 243 | $ret = isset($global[$key]) ? \filter_var($global[$key]) : $default; |
||
| 244 | } |
||
| 245 | break; |
||
| 246 | case 'int': |
||
| 247 | default: |
||
| 248 | $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_NUMBER_INT) : $default; |
||
| 249 | break; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (false === $ret) { |
||
| 253 | return $default; |
||
| 254 | } |
||
| 255 | |||
| 256 | return $ret; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param $mytree |
||
| 261 | * @param $key |
||
| 262 | * @param $category_array |
||
| 263 | * @param $title |
||
| 264 | * @param string $prefix |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public static function getPathTree($mytree, $key, $category_array, $title, $prefix = '') |
||
| 269 | { |
||
| 270 | $category_parent = $mytree->getAllParent($key); |
||
| 271 | |||
| 272 | $category_parent = \array_reverse($category_parent); |
||
| 273 | |||
| 274 | $Path = ''; |
||
| 275 | |||
| 276 | foreach (\array_keys($category_parent) as $j) { |
||
| 277 | $Path .= $category_parent[$j]->getVar($title) . $prefix; |
||
| 278 | } |
||
| 279 | |||
| 280 | $first_category = ''; |
||
| 281 | |||
| 282 | if (\array_key_exists($key, $category_array)) { |
||
| 283 | $first_category = $category_array[$key]->getVar($title); |
||
| 284 | } |
||
| 285 | |||
| 286 | $Path .= $first_category; |
||
| 287 | |||
| 288 | return $Path; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $mytree |
||
| 293 | * @param $key |
||
| 294 | * @param $category_array |
||
| 295 | * @param $title |
||
| 296 | * @param string $prefix |
||
| 297 | * @param bool $link |
||
| 298 | * @param string $order |
||
| 299 | * @param bool $lasturl |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public static function getPathTreeUrl( |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param $path |
||
| 368 | * @param int $mode |
||
| 369 | * @param $fileSource |
||
| 370 | * @param null $fileTarget |
||
| 371 | * @throws \RuntimeException |
||
| 372 | */ |
||
| 373 | public static function createFolder($path, $mode, $fileSource, $fileTarget = null) |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param $pathSource |
||
| 390 | * @param $pathTarget |
||
| 391 | * @throws \RuntimeException |
||
| 392 | */ |
||
| 393 | public static function cloneFolder($pathSource, $pathTarget) |
||
| 394 | { |
||
| 395 | if (\is_dir($pathSource)) { |
||
| 396 | // Create new dir |
||
| 397 | |||
| 398 | if (!\mkdir($pathTarget) && !\is_dir($pathTarget)) { |
||
| 399 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $pathTarget)); |
||
| 400 | } |
||
| 401 | |||
| 402 | // check all files in dir, and process it |
||
| 403 | |||
| 404 | $handle = \opendir($pathSource); |
||
| 405 | |||
| 406 | if ($handle) { |
||
| 407 | while ($file = \readdir($handle)) { |
||
| 408 | if ('.' !== $file && '..' !== $file) { |
||
| 409 | self::cloneFolder("$pathSource/$file", "$pathTarget/$file"); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | \closedir($handle); |
||
| 414 | } |
||
| 415 | } else { |
||
| 416 | \copy($pathSource, $pathTarget); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 422 | * |
||
| 423 | * @param string $folder Le chemin complet du répertoire à vérifier |
||
| 424 | * |
||
| 425 | * @throws \RuntimeException |
||
| 426 | */ |
||
| 427 | public static function prepareFolder($folder) |
||
| 434 | } |
||
| 435 | } |
||
| 436 |