Total Complexity | 54 |
Total Lines | 432 |
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); |
||
33 | class SysUtility |
||
34 | { |
||
35 | use VersionChecks; |
||
|
|||
36 | |||
37 | //checkVerXoops, checkVerPhp Traits |
||
38 | |||
39 | use ServerStats; |
||
40 | |||
41 | // getServerStats Trait |
||
42 | |||
43 | use FilesManagement; |
||
44 | |||
45 | // Files Management Trait |
||
46 | //use ModuleStats; // ModuleStats Trait |
||
47 | |||
48 | //--------------- Common module methods ----------------------------- |
||
49 | |||
50 | /** |
||
51 | * Access the only instance of this class |
||
52 | */ |
||
53 | public static function getInstance(): self |
||
54 | { |
||
55 | static $instance; |
||
56 | if (null === $instance) { |
||
57 | $instance = new static(); |
||
58 | } |
||
59 | |||
60 | return $instance; |
||
61 | } |
||
62 | |||
63 | public static function selectSorting(string $text, string $form_sort): string |
||
64 | { |
||
65 | global $start, $order, $sort; |
||
66 | |||
67 | $selectView = ''; |
||
68 | $helper = Helper::getInstance(); |
||
69 | |||
70 | //$pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getConfig('modicons16'); |
||
71 | $pathModIcon16 = $helper->url( |
||
72 | $helper->getModule() |
||
73 | ->getInfo('modicons16') |
||
74 | ); |
||
75 | |||
76 | $selectView = '<form name="form_switch" id="form_switch" action="' . Request::getString('REQUEST_URI', '', 'SERVER') . '" method="post"><span style="font-weight: bold;">' . $text . '</span>'; |
||
77 | //$sorts = $sort == 'asc' ? 'desc' : 'asc'; |
||
78 | if ($form_sort === $sort) { |
||
79 | $sel1 = 'asc' === $order ? 'selasc.png' : 'asc.png'; |
||
80 | $sel2 = 'desc' === $order ? 'seldesc.png' : 'desc.png'; |
||
81 | } else { |
||
82 | $sel1 = 'asc.png'; |
||
83 | $sel2 = 'desc.png'; |
||
84 | } |
||
85 | $selectView .= ' <a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=asc"><img src="' . $pathModIcon16 . '/' . $sel1 . '" title="ASC" alt="ASC"></a>'; |
||
86 | $selectView .= '<a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=desc"><img src="' . $pathModIcon16 . '/' . $sel2 . '" title="DESC" alt="DESC"></a>'; |
||
87 | $selectView .= '</form>'; |
||
88 | |||
89 | return $selectView; |
||
90 | } |
||
91 | |||
92 | /***************Blocks***************/ |
||
93 | public static function blockAddCatSelect(array $cats): string |
||
106 | } |
||
107 | |||
108 | public static function metaKeywords(string $content): void |
||
109 | { |
||
110 | global $xoopsTpl, $xoTheme; |
||
111 | $myts = \MyTextSanitizer::getInstance(); |
||
112 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
113 | if (\is_object($xoTheme)) { |
||
114 | $xoTheme->addMeta('meta', 'keywords', \strip_tags($content)); |
||
115 | } else { // Compatibility for old Xoops versions |
||
116 | $xoopsTpl->assign('xoops_metaKeywords', \strip_tags($content)); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | public static function metaDescription(string $content): void |
||
121 | { |
||
122 | global $xoopsTpl, $xoTheme; |
||
123 | $myts = \MyTextSanitizer::getInstance(); |
||
124 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
125 | if (\is_object($xoTheme)) { |
||
126 | $xoTheme->addMeta('meta', 'description', \strip_tags($content)); |
||
127 | } else { // Compatibility for old Xoops versions |
||
128 | $xoopsTpl->assign('xoops_metaDescription', \strip_tags($content)); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | public static function enumerate(string $tableName, string $columnName): ?array |
||
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 |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
196 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
197 | * www.cakephp.org |
||
198 | * |
||
199 | * @TODO: Refactor to consider HTML5 & void (self-closing) elements |
||
200 | * @TODO: Consider using https://github.com/jlgrall/truncateHTML/blob/master/truncateHTML.php |
||
201 | * |
||
202 | * @param string $text String to truncate. |
||
203 | * @param int|null $length Length of returned string, including ellipsis. |
||
204 | * @param string $ending Ending to be appended to the trimmed string. |
||
205 | * @param bool $exact If false, $text will not be cut mid-word |
||
206 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
207 | * |
||
208 | * @return string Trimmed string. |
||
209 | */ |
||
210 | public static function truncateHtml( |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get correct text editor based on user rights |
||
310 | * |
||
311 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
312 | */ |
||
313 | public static function getEditor(?Helper $helper = null, ?array $options = null): ?\XoopsFormTextArea |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Check if column in dB table exists |
||
350 | * |
||
351 | * @param string $fieldname name of dB table field |
||
352 | * @param string $table name of dB table (including prefix) |
||
353 | * |
||
354 | * @return bool true if table exists |
||
355 | * @deprecated |
||
356 | */ |
||
357 | public static function fieldExists(string $fieldname, string $table): bool |
||
358 | { |
||
359 | $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
||
360 | \trigger_error(__METHOD__ . " is deprecated, use Xmf\Database\Tables instead - instantiated from {$trace[0]['file']} line {$trace[0]['line']},"); |
||
361 | |||
362 | $result = $GLOBALS['xoopsDB']->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
||
363 | |||
364 | return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
369 | * |
||
370 | * @param string $folder The full path of the directory to check |
||
371 | */ |
||
372 | public static function prepareFolder(string $folder): void |
||
381 | } |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Check if dB table exists |
||
386 | * |
||
387 | * @param string $tablename dB tablename with prefix |
||
388 | * @return bool true if table exists |
||
389 | */ |
||
390 | public static function tableExists(string $tablename): bool |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Add a field to a mysql table |
||
405 | * @return bool|\mysqli_result |
||
406 | */ |
||
407 | public static function addField(string $field, string $table) |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Query and check if the result is a valid result set |
||
416 | * |
||
417 | * @param \XoopsMySQLDatabase $xoopsDB XOOPS Database |
||
418 | * @param string $sql a valid MySQL query |
||
419 | * @param int|null $limit number of records to return |
||
420 | * @param int|null $start offset of first record to return |
||
421 | * |
||
422 | * @return \mysqli_result query result |
||
423 | */ |
||
424 | public static function queryAndCheck(\XoopsMySQLDatabase $xoopsDB, string $sql, ?int $limit = null, ?int $start = null): \mysqli_result |
||
425 | { |
||
426 | $limit ??= 0; |
||
427 | $start ??= 0; |
||
428 | $result = $xoopsDB->query($sql, $limit, $start); |
||
429 | |||
430 | if (!$xoopsDB->isResultSet($result)) { |
||
431 | throw new \RuntimeException( |
||
432 | \sprintf(\_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), \E_USER_ERROR |
||
433 | ); |
||
434 | } |
||
435 | |||
436 | /** @var \mysqli_result $result */ |
||
437 | return $result; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * QueryF and check if the result is a valid result set |
||
442 | * |
||
443 | * @param \XoopsMySQLDatabase $xoopsDB XOOPS Database |
||
444 | * @param string $sql a valid MySQL query |
||
445 | * @param int|null $limit number of records to return |
||
446 | * @param int|null $start offset of first record to return |
||
447 | * |
||
448 | * @return \mysqli_result query result |
||
449 | */ |
||
450 | public static function queryFAndCheck( |
||
465 | } |
||
466 | } |
||
467 |