Passed
Branch master (30ffcd)
by Michael
06:06
created

Utility   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 458
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 217
dl 0
loc 458
rs 6.4799
c 0
b 0
f 0
wmc 54

15 Methods

Rating   Name   Duplication   Size   Complexity  
B runSimpleQuery() 0 37 9
A showDropBox() 0 23 5
A adminmain() 0 14 1
A getLibConfig() 0 11 1
A getAllLibConfig() 0 22 3
A getUserInfo() 0 10 2
B showArrayDropBox() 0 30 6
B defineCurrency() 0 25 7
A updateDbShort() 0 27 5
A updateDb() 0 12 2
A displayDonorArray() 0 20 2
A showTextBox() 0 15 2
A showYNBox() 0 19 3
A showImgXYBox() 0 21 3
A getConfigInfo() 0 12 3

How to fix   Complexity   

Complex Class

Complex classes like Utility 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 Utility, and based on these observations, apply Extract Interface, too.

1
<?php namespace XoopsModules\Xdonations;
2
3
use Xmf\Request;
0 ignored issues
show
Bug introduced by
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use XoopsModules\Xdonations;
5
use XoopsModules\Xdonations\Common;
6
7
/**
8
 * Class Utility
9
 */
10
class Utility
11
{
12
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
13
14
    use Common\ServerStats; // getServerStats Trait
15
16
    use Common\FilesManagement; // Files Management Trait
17
18
    //--------------- Custom module methods -----------------------------
19
20
    /**
21
     * @param $curr
22
     * @return string
23
     */
24
    public static function defineCurrency($curr)
25
    {
26
        switch ($curr) {
27
            case 'AUD':
28
                $currencySign = _MD_XDONATION_CURR_AUD;
29
                break;
30
            case 'EUR':
31
                $currencySign = _MD_XDONATION_CURR_EUR;
32
                break;
33
            case 'GBP':
34
                $currencySign = _MD_XDONATION_CURR_GBP;
35
                break;
36
            case 'JPY':
37
                $currencySign = _MD_XDONATION_CURR_JPY;
38
                break;
39
            case 'CAD':
40
                $currencySign = _MD_XDONATION_CURR_CAD;
41
                break;
42
            case 'USD':
43
            default:
44
                $currencySign = _MD_XDONATION_CURR_USD;
45
                break;
46
        }
47
48
        return $currencySign;
49
    }
50
51
    /**
52
     * Get all Config fields from DB
53
     *
54
     * @return array
55
     */
56
    public static function getConfigInfo()
57
    {
58
        global $xoopsDB;
59
60
        $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE subtype = '' OR subtype = 'array'";
61
        $cfgset    = $xoopsDB->query($query_cfg);
62
        $tr_config = [];
63
        while (false !== ($cfgset && $row = $xoopsDB->fetchArray($cfgset))) {
64
            $tr_config[$row['name']] = $row['value'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row does not seem to be defined for all execution paths leading up to this point.
Loading history...
65
        }
66
67
        return $tr_config;
68
    }
69
70
    /**
71
     * Get XOOPS Member Object
72
     *
73
     * @param  int $muser_id
74
     * @return FALSE - no member info avail for this id, SUCCESS - member object
75
     */
76
    public static function getUserInfo($muser_id)
77
    {
78
        global $xoopsDB;
79
        $thisUser = false;
80
        if ((int)$muser_id > 0) {
81
            $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

81
            $memberHandler = /** @scrutinizer ignore-call */ xoops_getHandler('member');
Loading history...
82
            $thisUser      = $memberHandler->getUser($muser_id);
83
        }
84
85
        return $thisUser;
86
    }
87
88
    /**
89
     * Retrieve list of db table's field names
90
     *
91
     * EXAMPLE USAGE:
92
     *
93
     * $list=$utility::runSimpleQuery($xoopsDB->prefix('donations_transactions'));
94
     *
95
     * @param  string $table_name DB table name
96
     * @param  string $key_col    (optional) table column name
97
     * @param  mixed  $key_val    (optional) table column value
98
     * @param  array  $ignore     (optional) list of values to ignore (clear)
99
     * @return mixed  FALSE - nothing found, SUCCESS - array() of values
100
     */
101
    public static function runSimpleQuery($table_name, $key_col = '', $key_val = '', $ignore = [])
102
    {
103
        global $xoopsDB;
104
        // open the db
105
        //    $db_link = mysqli_connect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS);
106
        $keys = '';
107
        if ('' != $key_col && '' != $key_val) {
108
            $keys = "WHERE $key_col = $key_val";
109
        }
110
        // query table using key col/val
111
        $simple_q   = false;
112
        $db_rs      = $xoopsDB->query("SELECT * FROM $table_name $keys");
113
        $num_fields = $xoopsDB->getFieldsNum($db_rs);
114
        if ($num_fields) {
115
            // first (and only) row
116
            $simple_q = [];
117
            $row      = $xoopsDB->fetchArray($db_rs);
118
            // load up array
119
            if ('' != $key_col && '' != $key_val) {
120
                for ($i = 0; $i < $num_fields; ++$i) {
121
                    $var            = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $var is dead and can be removed.
Loading history...
122
                    $var            = $xoopsDB->getFieldName($db_rs, $i);
123
                    $simple_q[$var] = $row[$var];
124
                }
125
            } else {
126
                for ($i = 0; $i < $num_fields; ++$i) {
127
                    $var = '';
128
                    $var = $xoopsDB->getFieldName($db_rs, $i);
129
                    if (!in_array($var, $ignore)) {
130
                        $simple_q[$var] = '';
131
                    }
132
                }
133
            }
134
        }
135
        $xoopsDB->freeRecordSet($db_rs);
136
137
        return $simple_q;
138
    }
139
140
    /*
141
     * Functions for Administration display
142
     */
143
144
    /**
145
     * Display a Config Option html Option Box in a 2 column table row
146
     *
147
     * @param string $name name of config variable in config DB table
148
     * @param string $desc description of option box
149
     */
150
    public static function showYNBox($name, $desc)
151
    {
152
        global $tr_config, $modversion, $xoopsDB;
153
154
        $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '{$name}'";
155
        $cfgset    = $xoopsDB->query($query_cfg);
156
        if ($cfgset) {
157
            $cfg  = $xoopsDB->fetchArray($cfgset);
158
            $text = htmlentities($cfg['text'], ENT_QUOTES | ENT_HTML5);
159
            echo "<tr>\n" . "  <td title=\"{$text}\" style=\"text-align: right;\">{$desc}</td>\n" . "  <td title=\"{$text}\" style=\"text-align: left;\">";
160
            echo "    <select size=\"1\" name=\"var_{$name}\">";
161
            if ($cfg['value']) {
162
                echo '      <option selected value="1">' . _YES . '</option>' . '      <option value="0">' . _NO . '</option>';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xdonations\_NO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant XoopsModules\Xdonations\_YES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
163
            } else {
164
                echo '      <option value="1">' . _YES . '</option>' . '      <option selected value="0">' . _NO . '</option>';
165
            }
166
            echo "    </select>\n";
167
            echo "  </td>\n";
168
            echo "</tr>\n";
169
        }
170
    }
171
172
    /**
173
     * Display a Config option HTML Select Box in 2 column table
174
     *
175
     * @param string $name name of config DB table column
176
     * @param string $desc description of select box to show
177
     */
178
    public static function showDropBox($name, $desc)
179
    {
180
        global $tr_config, $modversion, $xoopsDB;
181
182
        $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '{$name}'";
183
        $cfgset    = $xoopsDB->query($query_cfg);
184
        if ($cfgset) {
185
            $cfg  = $xoopsDB->fetchArray($cfgset);
186
            $text = htmlentities($cfg['text'], ENT_QUOTES | ENT_HTML5);
187
            echo "<tr style=\"text-align: center;\">\n" . "  <td title=\"{$text}\" style=\"text-align: right; width: 50%;\">{$desc}</td>\n" . "  <td title=\"{$text}\" style=\"text-align: left;\">\n";
188
            echo "    <select size=\"1\" name=\"var_{$name}-array\">\n";
189
            if (isset($cfg['value'])) {
190
                $splitArr = explode('|', $cfg['value']);
191
                $i        = 0;
192
                while ($i < count($splitArr)) {
193
                    $selected = (0 == $i) ? ' selected' : '';
194
                    echo "      <option{$selected} value=\"{$splitArr[$i]}\">{$splitArr[$i]}</option>\n";
195
                    ++$i;
196
                }
197
            }
198
            echo "    </select>\n";
199
            echo "  </td>\n";
200
            echo "</tr>\n";
201
        }
202
    }
203
204
    /**
205
     * Display Config Array Drop Box in HTML 2 column table row
206
     *
207
     * @param string $name    name of DB column in config table
208
     * @param string $desc    description to display for select box
209
     * @param array  $x_array array( array($value1, $attrib1), array(...) )
210
     */
211
    public static function showArrayDropBox($name, $desc, $x_array)
212
    {
213
        global $tr_config, $modversion, $xoopsDB;
214
        $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '{$name}' LIMIT 1";
215
        $cfgset    = $xoopsDB->query($query_cfg);
216
        if ($cfgset) {
217
            $cfg  = $xoopsDB->fetchArray($cfgset);
218
            $text = htmlentities($cfg['text'], ENT_QUOTES | ENT_HTML5);
219
            echo "<tr>\n" . "  <td title=\"{$text}\" style=\"text-align: right;\">{$desc}</td>\n" . "  <td title=\"{$text}\" style=\"text-align: left;\">\n";
220
            echo "    <select size=\"1\" name=\"var_{$name}\">\n";
221
            if (isset($cfg['value'])) {
222
                if (0 == $cfg['value']) {
223
                    echo "      <option selected value=\"0\">-------</option>\n";
224
                } else {
225
                    echo "      <option value=\"0\">-------</option>\n";
226
                }
227
                $i = 0;
228
                while ($i < count($x_array)) {
229
                    $mvar     = $x_array[$i];
230
                    $selected = '';
231
                    if ($mvar[0] == $cfg['value']) {
232
                        $selected = ' selected';
233
                    }
234
                    echo "      <option{$selected} value=\"{$mvar[0]}\">{$mvar[1]}</option>\n";
235
                    ++$i;
236
                }
237
            }
238
            echo "    </select>\n";
239
            echo "  </td>\n";
240
            echo "</tr>\n";
241
        }
242
    }
243
244
    /**
245
     * Display Config Option Text Box in a 2 column table row
246
     *
247
     * @param string $name    name of DB column in config table
248
     * @param string $desc    description of text box to display
249
     * @param int    $tdWidth width of description field
250
     * @param int    $inpSize width of text input box
251
     * @param string $extra   extra info included in input box 'string'
252
     */
253
    public static function showTextBox($name, $desc, $tdWidth, $inpSize, $extra)
254
    {
255
        global $tr_config, $modversion, $xoopsDB;
256
257
        $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '{$name}'";
258
        $cfgset    = $xoopsDB->query($query_cfg);
259
        if ($cfgset) {
260
            $cfg  = $xoopsDB->fetchArray($cfgset);
261
            $text = htmlentities($cfg['text'], ENT_QUOTES | ENT_HTML5);
262
            echo "<tr>\n"
263
                 . "  <td title=\"{$text}\" style=\"text-align: right; width: {$tdWidth};\">{$desc}</td>\n"
264
                 . "  <td title=\"{$text}\" style=\"text-align: left;\">\n"
265
                 . "    <input size=\"{$inpSize}\" name=\"var_{$name}\" type=\"text\" value=\"{$cfg['value']}\"  {$extra}>\n"
266
                 . "  </td>\n"
267
                 . "</tr>\n";
268
        }
269
    }
270
271
    /************************************************************************
272
     *
273
     ***********************************************************************
274
     * @param $xnm
275
     * @param $ynm
276
     * @param $desc
277
     * @param $inpSize
278
     * @param $extra
279
     */
280
    public static function showImgXYBox($xnm, $ynm, $desc, $inpSize, $extra)
281
    {
282
        global $tr_config, $modversion, $xoopsDB;
283
284
        $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '$xnm'";
285
        $cfgset    = $xoopsDB->query($query_cfg);
286
287
        if ($cfgset) {
288
            $cfg = $xoopsDB->fetchArray($cfgset);
289
290
            $text = htmlentities($cfg['text'], ENT_QUOTES | ENT_HTML5);
291
            echo "<tr>\n" . "  <td title=\"{$text}\" style=\"text-align: right;\">{$desc}</td>\n" . "  <td title=\"{$text}\" style=\"text-align: left;\">\n";
292
            echo '    &nbsp;' . _AD_XDONATION_WIDTH . "&nbsp;\n" . "    <input size=\"{$inpSize}\" name=\"var_{$cfg['name']}\" type=\"text\" value=\"{$cfg['value']}\" {$extra}>\n";
293
294
            $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '$ynm'";
295
            $cfgset    = $xoopsDB->query($query_cfg);
296
            if ($cfgset) {
297
                $cfg = $xoopsDB->fetchArray($cfgset);
298
                echo '    &nbsp;&nbsp;' . _AD_XDONATION_HEIGHT . "&nbsp;\n" . "    <input size=\"{$inpSize}\" name=\"var_{$cfg['name']}\" type=\"text\" value=\"{$cfg['value']}\" {$extra}>\n";
299
            }
300
            echo "  </td>\n" . "</tr>\n";
301
        }
302
    }
303
304
    /*
305
     * Functions to save Administration settings
306
     */
307
308
    /**
309
     * Update the Config option in the database
310
     *
311
     * @param  string $name config var name in the database
312
     * @param  string $sub  config subtype in the database
313
     * @param  mixed  $val  config var value
314
     * @param  string $txt  configuration text for this var
315
     * @return bool   TRUE value updated, FALSE value not updated
316
     */
317
    public static function updateDb($name, $sub, $val, $txt)
318
    {
319
        global $tr_config, $ilog, $xoopsDB;
320
        $insertRecordset = 'UPDATE `' . $xoopsDB->prefix('donations_config') . '`' . " SET `value`='$val', `text`='{$txt}'" . " WHERE `name`='{$name}' AND `subtype`='{$sub}'";
321
        $ilog            .= "{$insertRecordset}<br><br>";
322
        echo "{$insertRecordset}<br><br>";
323
        echo '<span style="color: #FF0000; font-weight: bold;">';
324
        $rvalue = $xoopsDB->query($insertRecordset);
325
        echo '</span>';
326
        $retVal = $rvalue ? true : false;
327
328
        return $retVal;
329
    }
330
331
    /************************************************************************
332
     *
333
     ***********************************************************************
334
     * @param $name
335
     * @param $sub
336
     * @param $val
337
     * @param $txt
338
     */
339
    public static function updateDbShort($name, $sub, $val, $txt = '')
0 ignored issues
show
Unused Code introduced by
The parameter $txt is not used and could be removed. ( Ignorable by Annotation )

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

339
    public static function updateDbShort($name, $sub, $val, /** @scrutinizer ignore-unused */ $txt = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
340
    {
341
        global $tr_config, $ilog, $xoopsDB;
342
        if ('array' === $sub) {
343
            $newArr    = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $newArr is dead and can be removed.
Loading history...
344
            $query_cfg = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '{$name}'";
345
            $cfgset    = $xoopsDB->query($query_cfg);
346
            $cfg       = $xoopsDB->fetchArray($cfgset);
347
            if (isset($cfg['value'])) {
348
                $splitArr = explode('|', $cfg['value']);
349
                $newArr   = $val;
350
                $i        = 0;
351
                while (false !== ($singleVar = $splitArr[$i])) {
352
                    if ($singleVar != $val) {
353
                        $newArr = $newArr . '|' . $singleVar;
354
                    }
355
                    ++$i;
356
                }
357
                $val = $newArr;
358
            }
359
        }
360
        $insertRecordset = 'UPDATE `' . $xoopsDB->prefix('donations_config') . '`' . " SET `value`='{$val}'" . " WHERE `name`='{$name}' AND `subtype`='{$sub}'";
361
362
        $ilog .= "{$insertRecordset}<br><br>\n";
363
        echo "{$insertRecordset}<br><br><span style=\"color: #FF0000; font-weight: bold;\">\n";
364
        $rvalue = $xoopsDB->query($insertRecordset);
0 ignored issues
show
Unused Code introduced by
The assignment to $rvalue is dead and can be removed.
Loading history...
365
        echo "</span>\n";
366
    }
367
368
    /**
369
     * Get Configuration Value
370
     *
371
     * @param  string $name name of configuration variable
372
     * @return mixed  value of config var on success, FALSE on failure
373
     *
374
     */
375
    public static function getLibConfig($name)
376
    {
377
        global $xoopsDB;
378
379
        $sql       = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name = '{$name}'";
380
        $Recordset = $xoopsDB->query($sql);
381
        $row       = $xoopsDB->fetchArray($Recordset);
382
        //  $text = $b = html_entity_decode($row['text']);
383
        $text = html_entity_decode($row['text']);
384
385
        return $text;
386
    }
387
388
    /**
389
     *
390
     * Get All Configuration Values
391
     *
392
     * @return array SUCCESS - array of config values (name as key); FAIL - empty
393
     */
394
    public static function getAllLibConfig()
395
    {
396
        global $xoopsDB;
397
398
        $sql      = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . ' ORDER BY name, subtype';
399
        $sqlQuery = $xoopsDB->query($sql);
400
401
        $t = [];
402
        while (false !== ($sqlfetch = $xoopsDB->fetchArray($sqlQuery))) {
403
            $text = html_entity_decode($sqlfetch['text']);
404
            $text = str_replace('<br>', "\r\n", $text);
405
            $text = str_replace('<br>', "\r\n", $text);
406
407
            if ('' == $sqlfetch['subtype']) {
408
                $t[$sqlfetch['name']] = $text;
409
            } else {
410
                $t[$sqlfetch['name']][$sqlfetch['subtype']] = $text;
411
            }
412
        }
413
414
        //displayArray($t,"------getAllLibConfig-----------");
415
        return $t;
416
    }
417
418
    /*******************************************************************
419
     *
420
     ******************************************************************
421
     * @param        $t
422
     * @param string $name
423
     * @param int    $ident
424
     */
425
    public static function displayDonorArray($t, $name = '', $ident = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $ident is not used and could be removed. ( Ignorable by Annotation )

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

425
    public static function displayDonorArray($t, $name = '', /** @scrutinizer ignore-unused */ $ident = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
426
    {
427
        if (is_array($t)) {
428
            echo '------------------------------------------------<br>';
429
            echo 'displayArray: ' . $name . ' - count = ' . count($t);
430
            //echo "<table ".getTblStyle().">";
431
            echo "<table>\n";
432
433
            echo '  <tr><td>';
434
            //jjd_echo ("displayArray: ".$name." - count = ".count($t), 255, "-") ;
435
            echo "</td></tr>\n";
436
437
            echo "  <tr><td>\n";
438
            echo '    <pre>';
439
            echo print_r($t);
440
            echo "</pre>\n";
441
            echo "  </td></tr>\n";
442
            echo "</table>\n";
443
        } else {
444
            echo "The variable ---|{$t}|--- is not an array\n";
445
            //        echo "l'indice ---|{$t}|--- n'est pas un tableau\n";
446
        }
447
        //jjd_echo ("Fin - ".$name, 255, "-") ;
448
    }
449
450
    /**
451
     * Display main top header table
452
     *
453
     */
454
    public static function adminmain()
455
    {
456
        global $tr_config, $modversion, $xoopsDB;
457
458
        echo "<div style=\"text-align: center;\">\n";
459
        echo "<table style='text-align: center; border-width: 1px; padding: 2px; margin: 2px; width: 90%;'>\n";
460
        echo "  <tr>\n";
461
        echo "    <td style='text-align: center; width: 25%;'><a href='index.php?op=Treasury'><img src='../images/admin/business_sm.png' alt='" . _AD_XDONATION_TREASURY . "'>&nbsp;" . _AD_XDONATION_TREASURY . "</a></td>\n";
462
        echo "    <td style='text-align: center; width: 25%;'><a href='index.php?op=ShowLog'><img src='../images/admin/view_text_sm.png' alt='" . _AD_XDONATION_SHOW_LOG . "'>&nbsp;" . _AD_XDONATION_SHOW_LOG . "</a></td>\n";
463
        echo "    <td style='text-align: center; width: 25%;'><a href='transaction.php'><img src='../images/admin/view_detailed_sm.png' alt='" . _AD_XDONATION_SHOW_TXN . "'>&nbsp;" . _AD_XDONATION_SHOW_TXN . "</a></td>\n";
464
        echo "    <td style='text-align: center; width: 25%;'><a href='index.php?op=Config'><img src='../images/admin/configure_sm.png' alt='" . _AD_XDONATION_CONFIGURATION . "'>&nbsp;" . _AD_XDONATION_CONFIGURATION . "</a></td>\n";
465
        echo "  </tr>\n";
466
        echo "</table>\n";
467
        echo "<br></div>\n";
468
    }
469
}
470