| Total Complexity | 54 |
| Total Lines | 458 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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; |
||
| 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']; |
||
| 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'); |
||
| 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 = []) |
||
| 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>'; |
||
| 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) |
||
| 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 ' ' . _AD_XDONATION_WIDTH . " \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 ' ' . _AD_XDONATION_HEIGHT . " \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 = '') |
||
| 340 | { |
||
| 341 | global $tr_config, $ilog, $xoopsDB; |
||
| 342 | if ('array' === $sub) { |
||
| 343 | $newArr = ''; |
||
| 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); |
||
| 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) |
||
| 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() |
||
| 468 | } |
||
| 469 | } |
||
| 470 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths