SysUtility   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 394
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 51
eloc 167
c 4
b 2
f 0
dl 0
loc 394
rs 7.92

13 Methods

Rating   Name   Duplication   Size   Complexity  
A blockAddCatSelect() 0 13 3
F truncateHtml() 0 95 19
A cloneRecord() 0 45 5
A metaDescription() 0 9 2
A selectSorting() 0 24 4
A prepareFolder() 0 9 4
A enumerate() 0 23 2
A metaKeywords() 0 9 2
A getInstance() 0 8 2
A tableExists() 0 10 1
A getEditor() 0 34 5
A fieldExists() 0 8 1
A addField() 0 3 1

How to fix   Complexity   

Complex Class

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);
2
3
namespace XoopsModules\Adslight\Common;
4
5
/*
6
 Utility Class Definition
7
8
 You may not change or alter any portion of this comment or credits of
9
 supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit
11
 authors.
12
13
 This program is distributed in the hope that it will be useful, but
14
 WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
18
/**
19
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @copyright    https://xoops.org 2000-2020 &copy; XOOPS Project
21
 * @author       ZySpec <[email protected]>
22
 * @author       Mamba <[email protected]>
23
 */
24
25
use Xmf\Request;
26
use XoopsFormEditor;
27
use XoopsModules\Adslight\{
28
    Helper
29
};
30
31
/**
32
 * Class SysUtility
33
 */
34
class SysUtility
35
{
36
    use VersionChecks;
0 ignored issues
show
introduced by
The trait XoopsModules\Adslight\Common\VersionChecks requires some properties which are not provided by XoopsModules\Adslight\Common\SysUtility: $tag_name, $prerelease
Loading history...
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 = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $selectView is dead and can be removed.
Loading history...
69
        $helper     = Helper::getInstance();
70
71
        //$pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getConfig('modicons16');
72
        $pathModIcon16 = $helper->url($helper->getModule()->getInfo('modicons16'));
0 ignored issues
show
Bug introduced by
It seems like $helper->getModule()->getInfo('modicons16') can also be of type array; however, parameter $url of Xmf\Module\Helper\GenericHelper::url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $pathModIcon16 = $helper->url(/** @scrutinizer ignore-type */ $helper->getModule()->getInfo('modicons16'));
Loading history...
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
93
    {
94
        $catSql = '';
95
        if (!empty($cats)) {
96
            $catSql = '(' . \current($cats);
97
            \array_shift($cats);
98
            foreach ($cats as $cat) {
99
                $catSql .= ',' . $cat;
100
            }
101
            $catSql .= ')';
102
        }
103
104
        return $catSql;
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $newId is dead and can be removed.
Loading history...
168
        $tempTable = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $tempTable is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $result_array is dead and can be removed.
Loading history...
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(
229
        string $text,
230
        ?int $length = 100,
231
        string $ending = '...',
232
        bool $exact = false,
233
        bool $considerHtml = true
234
    ): string {
235
        $openTags = [];
236
        if ($considerHtml) {
237
            // if the plain text is shorter than the maximum length, return the whole text
238
            if (\mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) {
239
                return $text;
240
            }
241
            // splits all html-tags to scanable lines
242
            \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER);
243
            $totalLength = \mb_strlen($ending);
244
            //$openTags    = [];
245
            $truncate = '';
246
            foreach ($lines as $lineMatchings) {
247
                // if there is any html-tag in this line, handle it and add it (uncounted) to the output
248
                if (!empty($lineMatchings[1])) {
249
                    // if it's an "empty element" with or without xhtml-conform closing slash
250
                    if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $lineMatchings[1])) {
251
                        // do nothing
252
                        // if tag is a closing tag
253
                    } elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/s', $lineMatchings[1], $tagMatchings)) {
254
                        // delete tag from $openTags list
255
                        $pos = \array_search($tagMatchings[1], $openTags, true);
256
                        if (false !== $pos) {
257
                            unset($openTags[$pos]);
258
                        }
259
                        // if tag is an opening tag
260
                    } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $lineMatchings[1], $tagMatchings)) {
261
                        // add tag to the beginning of $openTags list
262
                        \array_unshift($openTags, \mb_strtolower($tagMatchings[1]));
263
                    }
264
                    // add html-tag to $truncate'd text
265
                    $truncate .= $lineMatchings[1];
266
                }
267
                // calculate the length of the plain text part of the line; handle entities as one character
268
                $contentLength = \mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', ' ', $lineMatchings[2]));
269
                if (($totalLength + $contentLength) > $length) {
270
                    // the number of characters which are left
271
                    $left           = $length - $totalLength;
272
                    $entitiesLength = 0;
273
                    // search for html entities
274
                    if (\preg_match_all('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', $lineMatchings[2], $entities, \PREG_OFFSET_CAPTURE)) {
275
                        // calculate the real length of all entities in the legal range
276
                        foreach ($entities[0] as $entity) {
277
                            if ($left >= $entity[1] + 1 - $entitiesLength) {
278
                                $left--;
279
                                $entitiesLength += \mb_strlen($entity[0]);
280
                            } else {
281
                                // no more characters left
282
                                break;
283
                            }
284
                        }
285
                    }
286
                    $truncate .= \mb_substr($lineMatchings[2], 0, $left + $entitiesLength);
287
                    // maximum length is reached, so get off the loop
288
                    break;
289
                }
290
                $truncate    .= $lineMatchings[2];
291
                $totalLength += $contentLength;
292
293
                // if the maximum length is reached, get off the loop
294
                if ($totalLength >= $length) {
295
                    break;
296
                }
297
            }
298
        } else {
299
            if (\mb_strlen($text) <= $length) {
300
                return $text;
301
            }
302
            $truncate = \mb_substr($text, 0, $length - \mb_strlen($ending));
303
        }
304
        // if the words shouldn't be cut in the middle...
305
        if (!$exact) {
306
            // ...search the last occurance of a space...
307
            $spacepos = \mb_strrpos($truncate, ' ');
308
            if (isset($spacepos)) {
309
                // ...and cut the text in this position
310
                $truncate = \mb_substr($truncate, 0, $spacepos);
311
            }
312
        }
313
        // add the defined ending to the text
314
        $truncate .= $ending;
315
        if ($considerHtml) {
316
            // close all unclosed html-tags
317
            foreach ($openTags as $tag) {
318
                $truncate .= '</' . $tag . '>';
319
            }
320
        }
321
322
        return $truncate;
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
391
    {
392
        try {
393
            if (!@\mkdir($folder) && !\is_dir($folder)) {
394
                throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder));
395
            }
396
            file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
397
        } catch (\Throwable $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
        $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
411
        \trigger_error(__FUNCTION__ . " is deprecated, called from {$trace[0]['file']} line {$trace[0]['line']}");
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
        return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
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)
426
    {
427
        return $GLOBALS['xoopsDB']->queryF('ALTER TABLE ' . $table . " ADD $field;");
428
    }
429
}
430