| Total Complexity | 51 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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); |
||
| 34 | class SysUtility |
||
| 35 | { |
||
| 36 | use VersionChecks; |
||
|
|
|||
| 37 | |||
| 38 | //checkVerXoops, checkVerPhp Traits |
||
| 39 | |||
| 40 | use ServerStats; |
||
| 41 | |||
| 42 | // getServerStats Trait |
||
| 43 | |||
| 44 | use FilesManagement; |
||
| 45 | |||
| 46 | // Files Management Trait |
||
| 47 | // use ModuleStats; // ModuleStats Trait |
||
| 48 | |||
| 49 | //--------------- Common module methods ----------------------------- |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Access the only instance of this class |
||
| 53 | */ |
||
| 54 | public static function getInstance(): self |
||
| 55 | { |
||
| 56 | static $instance; |
||
| 57 | if (null === $instance) { |
||
| 58 | $instance = new static(); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $instance; |
||
| 62 | } |
||
| 63 | |||
| 64 | public static function selectSorting(string $text, string $form_sort): string |
||
| 65 | { |
||
| 66 | global $start, $order, $sort; |
||
| 67 | |||
| 68 | $selectView = ''; |
||
| 69 | $helper = Helper::getInstance(); |
||
| 70 | |||
| 71 | //$pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getConfig('modicons16'); |
||
| 72 | $pathModIcon16 = $helper->url($helper->getModule()->getInfo('modicons16')); |
||
| 73 | |||
| 74 | $selectView = '<form name="form_switch" id="form_switch" action="' . Request::getString('REQUEST_URI', '', 'SERVER') . '" method="post"><span style="font-weight: bold;">' . $text . '</span>'; |
||
| 75 | //$sorts = $sort == 'asc' ? 'desc' : 'asc'; |
||
| 76 | if ($form_sort == $sort) { |
||
| 77 | $sel1 = 'asc' === $order ? 'selasc.png' : 'asc.png'; |
||
| 78 | $sel2 = 'desc' === $order ? 'seldesc.png' : 'desc.png'; |
||
| 79 | } else { |
||
| 80 | $sel1 = 'asc.png'; |
||
| 81 | $sel2 = 'desc.png'; |
||
| 82 | } |
||
| 83 | $selectView .= ' <a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=asc"><img src="' . $pathModIcon16 . '/' . $sel1 . '" title="ASC" alt="ASC"></a>'; |
||
| 84 | $selectView .= '<a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=desc"><img src="' . $pathModIcon16 . '/' . $sel2 . '" title="DESC" alt="DESC"></a>'; |
||
| 85 | $selectView .= '</form>'; |
||
| 86 | |||
| 87 | return $selectView; |
||
| 88 | } |
||
| 89 | |||
| 90 | /***************Blocks***************/ |
||
| 91 | |||
| 92 | public static function blockAddCatSelect(array $cats): string |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function metaKeywords(string $content): void |
||
| 108 | { |
||
| 109 | global $xoopsTpl, $xoTheme; |
||
| 110 | $myts = \MyTextSanitizer::getInstance(); |
||
| 111 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 112 | if (\is_object($xoTheme)) { |
||
| 113 | $xoTheme->addMeta('meta', 'keywords', \strip_tags($content)); |
||
| 114 | } else { // Compatibility for old Xoops versions |
||
| 115 | $xoopsTpl->assign('xoops_metaKeywords', \strip_tags($content)); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | public static function metaDescription(string $content): void |
||
| 120 | { |
||
| 121 | global $xoopsTpl, $xoTheme; |
||
| 122 | $myts = \MyTextSanitizer::getInstance(); |
||
| 123 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 124 | if (\is_object($xoTheme)) { |
||
| 125 | $xoTheme->addMeta('meta', 'description', \strip_tags($content)); |
||
| 126 | } else { // Compatibility for old Xoops versions |
||
| 127 | $xoopsTpl->assign('xoops_metaDescription', \strip_tags($content)); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | public static function enumerate(string $tableName, string $columnName): ?array |
||
| 132 | { |
||
| 133 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 134 | |||
| 135 | // $result = $GLOBALS['xoopsDB']->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS |
||
| 136 | // WHERE TABLE_NAME = '" . $table . "' AND COLUMN_NAME = '" . $columnName . "'") |
||
| 137 | // || exit ($GLOBALS['xoopsDB']->error()); |
||
| 138 | |||
| 139 | $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "' . $table . '" AND COLUMN_NAME = "' . $columnName . '"'; |
||
| 140 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 141 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 142 | |||
| 143 | // \trigger_error($GLOBALS['xoopsDB']->error()); |
||
| 144 | $logger = \XoopsLogger::getInstance(); |
||
| 145 | $logger->handleError(\E_USER_WARNING, $sql, __FILE__, __LINE__); |
||
| 146 | |||
| 147 | return null; |
||
| 148 | } |
||
| 149 | |||
| 150 | $row = $GLOBALS['xoopsDB']->fetchBoth($result); |
||
| 151 | $enumList = \explode(',', \str_replace("'", '', \mb_substr($row['COLUMN_TYPE'], 5, -6))); |
||
| 152 | |||
| 153 | return $enumList; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Clone a record in a dB |
||
| 158 | * |
||
| 159 | * @TODO need to exit more gracefully on error. Should throw/trigger error and then return false |
||
| 160 | * |
||
| 161 | * @param string $tableName name of dB table (without prefix) |
||
| 162 | * @param string $idField name of field (column) in dB table |
||
| 163 | * @param int $id item id to clone |
||
| 164 | */ |
||
| 165 | public static function cloneRecord(string $tableName, string $idField, int $id): ?int |
||
| 166 | { |
||
| 167 | $newId = null; |
||
| 168 | $tempTable = ''; |
||
| 169 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 170 | // copy content of the record you wish to clone |
||
| 171 | $sql = "SELECT * FROM $table WHERE $idField='" . $id . "' "; |
||
| 172 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 173 | |||
| 174 | if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 175 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($result, \MYSQLI_ASSOC); |
||
| 176 | } else { |
||
| 177 | // trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR); |
||
| 178 | $logger = \XoopsLogger::getInstance(); |
||
| 179 | $logger->handleError(\E_USER_WARNING, $sql, __FILE__, __LINE__); |
||
| 180 | |||
| 181 | return null; |
||
| 182 | } |
||
| 183 | |||
| 184 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 185 | if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 186 | $result_array = $GLOBALS['xoopsDB']->fetchArray($result); |
||
| 187 | } else { |
||
| 188 | \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
||
| 189 | $logger = \XoopsLogger::getInstance(); |
||
| 190 | $logger->handleError(\E_USER_WARNING, $sql, __FILE__, __LINE__); |
||
| 191 | |||
| 192 | return null; |
||
| 193 | } |
||
| 194 | |||
| 195 | if (!$tempTable) { |
||
| 196 | \trigger_error($GLOBALS['xoopsDB']->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($GLOBALS['xoopsDB']->error()); |
||
| 205 | } |
||
| 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($options['name']), $helper->getConfig('editorAdmin'), $options, false, 'textarea'); |
||
| 354 | } else { |
||
| 355 | $descEditor = new \XoopsFormEditor(\ucfirst($options['name']), $helper->getConfig('editorUser'), $options, false, 'textarea'); |
||
| 356 | } |
||
| 357 | } else { |
||
| 358 | $descEditor = new \XoopsFormDhtmlTextArea(\ucfirst($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 | \trigger_error(__METHOD__ . " is deprecated, 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 | |||
| 382 | return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0); |
||
| 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 |
||
| 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 |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Add a field to a mysql table |
||
| 422 | * |
||
| 423 | * @return bool|\mysqli_result |
||
| 424 | */ |
||
| 425 | public static function addField(string $field, string $table) |
||
| 430 |