| Total Complexity | 45 |
| Total Lines | 314 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 36 | class SysUtility |
||
| 37 | { |
||
| 38 | use VersionChecks; |
||
|
|
|||
| 39 | use ServerStats; |
||
| 40 | use FilesManagement; |
||
| 41 | |||
| 42 | // Files Management Trait |
||
| 43 | |||
| 44 | //--------------- Custom module methods ----------------------------- |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $text |
||
| 48 | * @param $form_sort |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public static function selectSorting($text, $form_sort) |
||
| 76 | } |
||
| 77 | |||
| 78 | /***************Blocks***************/ |
||
| 79 | /** |
||
| 80 | * @param array $cats |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public static function blockAddCatSelect($cats) |
||
| 84 | { |
||
| 85 | $cat_sql = ''; |
||
| 86 | if (\is_array($cats) && !empty($cats)) { |
||
| 87 | $cat_sql = '(' . \current($cats); |
||
| 88 | \array_shift($cats); |
||
| 89 | foreach ($cats as $cat) { |
||
| 90 | $cat_sql .= ',' . $cat; |
||
| 91 | } |
||
| 92 | $cat_sql .= ')'; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $cat_sql; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param $content |
||
| 100 | */ |
||
| 101 | public static function metaKeywords($content) |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $content |
||
| 115 | */ |
||
| 116 | public static function metaDescription($content) |
||
| 117 | { |
||
| 118 | global $xoopsTpl, $xoTheme; |
||
| 119 | $myts = \MyTextSanitizer::getInstance(); |
||
| 120 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 121 | if (null !== $xoTheme && \is_object($xoTheme)) { |
||
| 122 | $xoTheme->addMeta('meta', 'description', \strip_tags($content)); |
||
| 123 | } else { // Compatibility for old Xoops versions |
||
| 124 | $xoopsTpl->assign('xoops_metaDescription', \strip_tags($content)); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param $tableName |
||
| 130 | * @param $columnName |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public static function enumerate($tableName, $columnName) |
||
| 135 | { |
||
| 136 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 137 | |||
| 138 | // $result = $GLOBALS['xoopsDB']->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS |
||
| 139 | // WHERE TABLE_NAME = '" . $table . "' AND COLUMN_NAME = '" . $columnName . "'") |
||
| 140 | // || exit ($GLOBALS['xoopsDB']->error()); |
||
| 141 | |||
| 142 | $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "' . $table . '" AND COLUMN_NAME = "' . $columnName . '"'; |
||
| 143 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 144 | if (!$result) { |
||
| 145 | exit($GLOBALS['xoopsDB']->error()); |
||
| 146 | } |
||
| 147 | |||
| 148 | $row = $GLOBALS['xoopsDB']->fetchBoth($result); |
||
| 149 | $enumList = \explode(',', \str_replace("'", '', \substr($row['COLUMN_TYPE'], 5, -6))); |
||
| 150 | return $enumList; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param array|string $tableName |
||
| 155 | * @param int $id_field |
||
| 156 | * @param int $id |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | public static function cloneRecord($tableName, $id_field, $id) |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
| 180 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
| 181 | * www.cakephp.org |
||
| 182 | * |
||
| 183 | * @param string $text String to truncate. |
||
| 184 | * @param int $length Length of returned string, including ellipsis. |
||
| 185 | * @param string $ending Ending to be appended to the trimmed string. |
||
| 186 | * @param bool $exact If false, $text will not be cut mid-word |
||
| 187 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
| 188 | * |
||
| 189 | * @return string Trimmed string. |
||
| 190 | */ |
||
| 191 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param \Xmf\Module\Helper $helper |
||
| 285 | * @param array|null $options |
||
| 286 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
| 287 | */ |
||
| 288 | public static function getEditor( |
||
| 289 | $helper = null, |
||
| 290 | $options = null |
||
| 291 | ) { |
||
| 292 | /** @var Helper $helper */ |
||
| 293 | if (null === $options) { |
||
| 294 | $options = []; |
||
| 295 | $options['name'] = 'Editor'; |
||
| 296 | $options['value'] = 'Editor'; |
||
| 297 | $options['rows'] = 10; |
||
| 298 | $options['cols'] = '100%'; |
||
| 299 | $options['width'] = '100%'; |
||
| 300 | $options['height'] = '400px'; |
||
| 301 | } |
||
| 302 | |||
| 303 | if (null === $helper) { |
||
| 304 | $helper = Helper::getInstance(); |
||
| 305 | } |
||
| 306 | |||
| 307 | $isAdmin = $helper->isUserAdmin(); |
||
| 308 | |||
| 309 | if (\class_exists('XoopsFormEditor')) { |
||
| 310 | if ($isAdmin) { |
||
| 311 | $descEditor = new XoopsFormEditor( |
||
| 312 | \ucfirst($options['name']), $helper->getConfig( |
||
| 313 | 'editorAdmin' |
||
| 314 | ), $options, $nohtml = false, $onfailure = 'textarea' |
||
| 315 | ); |
||
| 316 | } else { |
||
| 317 | $descEditor = new XoopsFormEditor( |
||
| 318 | \ucfirst($options['name']), $helper->getConfig( |
||
| 319 | 'editorUser' |
||
| 320 | ), $options, $nohtml = false, $onfailure = 'textarea' |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | } else { |
||
| 324 | $descEditor = new XoopsFormDhtmlTextArea( |
||
| 325 | \ucfirst( |
||
| 326 | $options['name'] |
||
| 327 | ), $options['name'], $options['value'], '100%', '100%' |
||
| 328 | ); |
||
| 329 | } |
||
| 330 | |||
| 331 | // $form->addElement($descEditor); |
||
| 332 | |||
| 333 | return $descEditor; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param $fieldname |
||
| 338 | * @param $table |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function fieldExists( |
||
| 350 | } |
||
| 351 | } |
||
| 352 |