| Total Complexity | 61 |
| Total Lines | 492 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SysUtility 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 SysUtility, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 36 | class SysUtility |
||
| 37 | { |
||
| 38 | //traits |
||
| 39 | use VersionChecks; //checkVerXoops, checkVerPhp Traits |
||
|
|
|||
| 40 | use ServerStats; // getServerStats Trait |
||
| 41 | use FilesManagement; // Files Management Trait |
||
| 42 | use ModuleStats; // ModuleStats Trait |
||
| 43 | |||
| 44 | //--------------- Common module methods ----------------------------- |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Access the only instance of this class |
||
| 48 | */ |
||
| 49 | public static function getInstance(): self |
||
| 50 | { |
||
| 51 | static $instance; |
||
| 52 | if (null === $instance) { |
||
| 53 | $instance = new static(); |
||
| 54 | } |
||
| 55 | |||
| 56 | return $instance; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $text |
||
| 61 | * @param string $form_sort |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public static function selectSorting(string $text, string $form_sort): string |
||
| 66 | { |
||
| 67 | global $start, $order, $sort; |
||
| 68 | |||
| 69 | $selectView = ''; |
||
| 70 | $helper = Helper::getInstance(); |
||
| 71 | |||
| 72 | //$pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getConfig('modicons16'); |
||
| 73 | $pathModIcon16 = $helper->url($helper->getModule()->getInfo('modicons16')); |
||
| 74 | |||
| 75 | $selectView = '<form name="form_switch" id="form_switch" action="' . Request::getString('REQUEST_URI', '', 'SERVER') . '" method="post"><span style="font-weight: bold;">' . $text . '</span>'; |
||
| 76 | //$sorts = $sort == 'asc' ? 'desc' : 'asc'; |
||
| 77 | if ($form_sort == $sort) { |
||
| 78 | $sel1 = 'asc' === $order ? 'selasc.png' : 'asc.png'; |
||
| 79 | $sel2 = 'desc' === $order ? 'seldesc.png' : 'desc.png'; |
||
| 80 | } else { |
||
| 81 | $sel1 = 'asc.png'; |
||
| 82 | $sel2 = 'desc.png'; |
||
| 83 | } |
||
| 84 | $selectView .= ' <a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=asc"><img src="' . $pathModIcon16 . '/' . $sel1 . '" title="ASC" alt="ASC"></a>'; |
||
| 85 | $selectView .= '<a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=desc"><img src="' . $pathModIcon16 . '/' . $sel2 . '" title="DESC" alt="DESC"></a>'; |
||
| 86 | $selectView .= '</form>'; |
||
| 87 | |||
| 88 | return $selectView; |
||
| 89 | } |
||
| 90 | |||
| 91 | //--------------- BLOCKS ----------------------------- |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $cats |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public static function blockAddCatSelect(array $cats): string |
||
| 99 | { |
||
| 100 | $catSql = ''; |
||
| 101 | if (!empty($cats)) { |
||
| 102 | $catSql = '(' . \current($cats); |
||
| 103 | \array_shift($cats); |
||
| 104 | foreach ($cats as $cat) { |
||
| 105 | $catSql .= ',' . $cat; |
||
| 106 | } |
||
| 107 | $catSql .= ')'; |
||
| 108 | } |
||
| 109 | |||
| 110 | return $catSql; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $content |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public static function metaKeywords(string $content): void |
||
| 118 | { |
||
| 119 | global $xoopsTpl, $xoTheme; |
||
| 120 | $myts = \MyTextSanitizer::getInstance(); |
||
| 121 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 122 | if (\is_object($xoTheme)) { |
||
| 123 | $xoTheme->addMeta('meta', 'keywords', \strip_tags($content)); |
||
| 124 | } else { // Compatibility for old Xoops versions |
||
| 125 | $xoopsTpl->assign('xoops_metaKeywords', \strip_tags($content)); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $content |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public static function metaDescription(string $content): void |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $tableName |
||
| 147 | * @param string $columnName |
||
| 148 | * |
||
| 149 | * @return array|null |
||
| 150 | */ |
||
| 151 | public static function enumerate(string $tableName, string $columnName): ?array |
||
| 152 | { |
||
| 153 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 154 | |||
| 155 | // $result = $GLOBALS['xoopsDB']->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS |
||
| 156 | // WHERE TABLE_NAME = '" . $table . "' AND COLUMN_NAME = '" . $columnName . "'") |
||
| 157 | // || exit ($GLOBALS['xoopsDB']->error()); |
||
| 158 | |||
| 159 | $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "' . $table . '" AND COLUMN_NAME = "' . $columnName . '"'; |
||
| 160 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 161 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 162 | // \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), E_USER_ERROR); |
||
| 163 | $logger = \XoopsLogger::getInstance(); |
||
| 164 | $logger->handleError(\E_USER_WARNING, $sql, __FILE__, __LINE__); |
||
| 165 | return null; |
||
| 166 | } |
||
| 167 | |||
| 168 | $row = $GLOBALS['xoopsDB']->fetchBoth($result); |
||
| 169 | $enumList = \explode(',', \str_replace("'", '', \mb_substr($row['COLUMN_TYPE'], 5, - 6))); |
||
| 170 | return $enumList; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Clone a record in a dB |
||
| 176 | * |
||
| 177 | * @TODO need to exit more gracefully on error. Should throw/trigger error and then return false |
||
| 178 | * |
||
| 179 | * @param string $tableName name of dB table (without prefix) |
||
| 180 | * @param string $idField name of field (column) in dB table |
||
| 181 | * @param int $id item id to clone |
||
| 182 | * @return int|null |
||
| 183 | */ |
||
| 184 | public static function cloneRecord(string $tableName, string $idField, int $id): ?int |
||
| 185 | { |
||
| 186 | $newId = null; |
||
| 187 | $tempTable = []; |
||
| 188 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 189 | // copy content of the record you wish to clone |
||
| 190 | $sql = "SELECT * FROM $table WHERE $idField='" . $id . "' "; |
||
| 191 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 192 | if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 193 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($result, \MYSQLI_ASSOC); |
||
| 194 | } |
||
| 195 | if (!$tempTable) { |
||
| 196 | \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
||
| 197 | } |
||
| 198 | // set the auto-incremented id's value to blank. |
||
| 199 | unset($tempTable[$idField]); |
||
| 200 | // insert cloned copy of the original record |
||
| 201 | $sql = "INSERT INTO $table (" . \implode(', ', \array_keys($tempTable)) . ") VALUES ('" . \implode("', '", $tempTable) . "')"; |
||
| 202 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 203 | if (!$result) { |
||
| 204 | \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
||
| 205 | } else { |
||
| 206 | // Return the new id |
||
| 207 | $newId = $GLOBALS['xoopsDB']->getInsertId(); |
||
| 208 | } |
||
| 209 | return $newId; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
| 214 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
| 215 | * www.cakephp.org |
||
| 216 | * |
||
| 217 | * @TODO: Refactor to consider HTML5 & void (self-closing) elements |
||
| 218 | * @TODO: Consider using https://github.com/jlgrall/truncateHTML/blob/master/truncateHTML.php |
||
| 219 | * |
||
| 220 | * @param string $text String to truncate. |
||
| 221 | * @param int|null $length Length of returned string, including ellipsis. |
||
| 222 | * @param string $ending Ending to be appended to the trimmed string. |
||
| 223 | * @param bool $exact If false, $text will not be cut mid-word |
||
| 224 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
| 225 | * |
||
| 226 | * @return string Trimmed string. |
||
| 227 | */ |
||
| 228 | public static function truncateHtml( |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get correct text editor based on user rights |
||
| 327 | * |
||
| 328 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
| 329 | */ |
||
| 330 | public static function getEditor(?\Xmf\Module\Helper $helper = null, ?array $options = null): ?\XoopsFormTextArea |
||
| 331 | { |
||
| 332 | $descEditor = null; |
||
| 333 | |||
| 334 | /** @var Helper $helper */ |
||
| 335 | if (null === $options) { |
||
| 336 | $options = []; |
||
| 337 | $options['name'] = 'Editor'; |
||
| 338 | $options['value'] = 'Editor'; |
||
| 339 | $options['rows'] = 10; |
||
| 340 | $options['cols'] = '100%'; |
||
| 341 | $options['width'] = '100%'; |
||
| 342 | $options['height'] = '400px'; |
||
| 343 | } |
||
| 344 | |||
| 345 | if (null === $helper) { |
||
| 346 | $helper = Helper::getInstance(); |
||
| 347 | } |
||
| 348 | |||
| 349 | $isAdmin = $helper->isUserAdmin(); |
||
| 350 | |||
| 351 | if (\class_exists('XoopsFormEditor')) { |
||
| 352 | if ($isAdmin) { |
||
| 353 | $descEditor = new \XoopsFormEditor(\ucfirst((string) $options['name']), $helper->getConfig('editorAdmin'), $options, false, 'textarea'); |
||
| 354 | } else { |
||
| 355 | $descEditor = new \XoopsFormEditor(\ucfirst((string) $options['name']), $helper->getConfig('editorUser'), $options, false, 'textarea'); |
||
| 356 | } |
||
| 357 | } else { |
||
| 358 | $descEditor = new \XoopsFormDhtmlTextArea(\ucfirst((string) $options['name']), $options['name'], $options['value']); |
||
| 359 | } |
||
| 360 | |||
| 361 | // $form->addElement($descEditor); |
||
| 362 | |||
| 363 | return $descEditor; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Check if column in dB table exists |
||
| 368 | * |
||
| 369 | * @param string $fieldname name of dB table field |
||
| 370 | * @param string $table name of dB table (including prefix) |
||
| 371 | * |
||
| 372 | * @return bool true if table exists |
||
| 373 | * @deprecated |
||
| 374 | */ |
||
| 375 | public static function fieldExists(string $fieldname, string $table): bool |
||
| 376 | { |
||
| 377 | $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
||
| 378 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . "() use Xmf\Database\Tables instead - instantiated from {$trace[0]['file']} line {$trace[0]['line']}"); |
||
| 379 | |||
| 380 | $result = $GLOBALS['xoopsDB']->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
||
| 381 | return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0); |
||
| 382 | } |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 387 | * |
||
| 388 | * @param string $folder The full path of the directory to check |
||
| 389 | */ |
||
| 390 | public static function prepareFolder(string $folder): void |
||
| 391 | { |
||
| 392 | try { |
||
| 393 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
| 394 | throw new \Exception(\sprintf('Unable to create the %s directory', $folder)); |
||
| 395 | } |
||
| 396 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 397 | } catch (\Exception $e) { |
||
| 398 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Check if dB table exists |
||
| 404 | * |
||
| 405 | * @param string $tablename dB tablename with prefix |
||
| 406 | * @return bool true if table exists |
||
| 407 | */ |
||
| 408 | public static function tableExists(string $tablename): bool |
||
| 409 | { |
||
| 410 | $ret = false; |
||
| 411 | $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
||
| 412 | $GLOBALS['xoopsLogger']->addDeprecated( |
||
| 413 | \basename(\dirname(__DIR__, 2)) . ' Module: ' . __FUNCTION__ . ' function is deprecated, please use Xmf\Database\Tables method(s) instead.' . " Called from {$trace[0]['file']}line {$trace[0]['line']}" |
||
| 414 | ); |
||
| 415 | $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'"); |
||
| 416 | |||
| 417 | if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 418 | $ret = $GLOBALS['xoopsDB']->getRowsNum($result) > 0; |
||
| 419 | } |
||
| 420 | |||
| 421 | return $ret; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Add a field to a mysql table |
||
| 426 | * |
||
| 427 | * @return bool|\mysqli_result |
||
| 428 | */ |
||
| 429 | public static function addField(string $field, string $table) |
||
| 430 | { |
||
| 431 | global $xoopsDB; |
||
| 432 | return $xoopsDB->queryF('ALTER TABLE ' . $table . " ADD $field;"); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | public static function cleanCache(): void |
||
| 439 | { |
||
| 440 | $myDirName = \basename(\dirname(__DIR__, 2)); |
||
| 441 | $cacheHelper = new Cache($myDirName); |
||
| 442 | if (\method_exists($cacheHelper, 'clear')) { |
||
| 443 | $cacheHelper->clear(); |
||
| 444 | |||
| 445 | return; |
||
| 446 | } |
||
| 447 | // for 2.5 systems, clear everything |
||
| 448 | require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php'; |
||
| 449 | $maintenance = new \SystemMaintenance(); |
||
| 450 | $cacheList = [ |
||
| 451 | 3, // xoops_cache |
||
| 452 | ]; |
||
| 453 | $maintenance->CleanCache($cacheList); |
||
| 454 | \xoops_setActiveModules(); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public static function renameUploadFolder(): bool |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Query and check if the result is a valid result set |
||
| 487 | * |
||
| 488 | * @param \XoopsMySQLDatabase $xoopsDB XOOPS Database |
||
| 489 | * @param string $sql a valid MySQL query |
||
| 490 | * @param int $limit number of records to return |
||
| 491 | * @param int $start offset of first record to return |
||
| 492 | * |
||
| 493 | * @return \mysqli_result query result |
||
| 494 | */ |
||
| 495 | public static function queryAndCheck(\XoopsMySQLDatabase $xoopsDB, string $sql, $limit = 0, $start = 0): \mysqli_result |
||
| 496 | { |
||
| 497 | $result = $xoopsDB->query($sql, $limit, $start); |
||
| 498 | |||
| 499 | if (!$xoopsDB->isResultSet($result)) { |
||
| 500 | throw new \RuntimeException( |
||
| 501 | \sprintf(\_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), \E_USER_ERROR); |
||
| 502 | } |
||
| 503 | |||
| 504 | return $result; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * QueryF and check if the result is a valid result set |
||
| 509 | * |
||
| 510 | * @param \XoopsMySQLDatabase $xoopsDB XOOPS Database |
||
| 511 | * @param string $sql a valid MySQL query |
||
| 512 | * @param int $limit number of records to return |
||
| 513 | * @param int $start offset of first record to return |
||
| 514 | * |
||
| 515 | * @return \mysqli_result query result |
||
| 516 | */ |
||
| 517 | public static function queryFAndCheck(\XoopsMySQLDatabase $xoopsDB, string $sql, $limit = 0, $start = 0): \mysqli_result |
||
| 528 | } |
||
| 529 | } |
||
| 530 |