b_referaccum_show()   C
last analyzed

Complexity

Conditions 13
Paths 42

Size

Total Lines 69
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 69
rs 6.6166
cc 13
nc 42
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 * @param mixed $options
19
 */
20
21
/******************************************************************************
22
 * Function: b_referaccum_show
23
 * Output  : retruns the total of hit for a given referer
24
 *****************************************************************************
25
 * @param $options
26
 * @return array
27
 */
28
function b_referaccum_show($options)
29
{
30
    global $xoopsDB, $configHandler, $xoopsStatConfig;
31
32
    /** @var \XoopsModuleHandler $moduleHandler */
33
    $moduleHandler   = xoops_getHandler('module');
34
    $xoopsStatModule = $moduleHandler->getByDirname('statistics');
35
    /** @var \XoopsConfigHandler $configHandler */
36
    $configHandler = xoops_getHandler('config');
37
    $xoopsStatConfig = $configHandler->getConfigsByCat(0, $xoopsStatModule->getVar('mid'));
38
39
    $showhowmany   = $options[0];
40
    $showselfrefer = $options[1];
41
42
    // get any current blacklist
43
    $result = $xoopsDB->queryF('SELECT * FROM ' . $xoopsDB->prefix('stats_refer_blacklist'));
44
    [$id, $referer] = $xoopsDB->fetchRow($result);
45
    $referblacklist = unserialize(stripslashes($referer));
46
    if (!is_array($referblacklist)) { // something went wrong, or there is no data...
47
        $referblacklist = [];
48
    }
49
50
    $block = [];
51
    // this sql is getting the total hits for a refer by summing the hits and grouping by refer.  We order
52
    // by the total hits in DESC fashion.  Highest is first.  total is an alias for SUM(hits)
53
    $result    = $xoopsDB->queryF('SELECT refer, SUM(hits) AS total FROM ' . $xoopsDB->prefix('stats_refer') . ' GROUP BY refer ORDER BY total DESC');
54
    $referhits = [];
55
    $cnt       = 0;
56
    while (list($refer, $total) = $xoopsDB->fetchRow($result)) {
57
        if ((0 == $showselfrefer && !strcmp($refer, $_SERVER['HTTP_HOST'])) || '' == $refer) {
58
            continue;
59
        }
60
        // see if we have a blacklist
61
        $blacklisted = false;
62
63
        if ('Allow' != $xoopsStatConfig['refererspam']) {  // make sure they really want them ignored
64
            if (count($referblacklist) > 0) {
65
                $rbldelimited = '/' . implode('|', $referblacklist) . '/';
66
                if (preg_match($rbldelimited, $refer)) {
67
                    $blacklisted = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $blacklisted is dead and can be removed.
Loading history...
68
                    continue;
69
                }
70
            }
71
        }
72
73
        if (false === $blacklisted) {
74
            $referhits[$cnt]['refer'] = $refer;
75
            $referhits[$cnt]['hits']  = '';
76
            if (mb_strlen($total) < 3) {
77
                $hits = sprintf('%03d', $total);
0 ignored issues
show
Unused Code introduced by
The assignment to $hits is dead and can be removed.
Loading history...
78
            }
79
            for ($i = 0, $iMax = mb_strlen($total); $i < $iMax; ++$i) {
80
                //the <img src> tag
81
                $imgsrc                  = mb_substr($total, $i, 1);
82
                $referhits[$cnt]['hits'] .= '<img src="' . XOOPS_URL . '/modules/statistics/assets/images/' . $imgsrc . '.gif" border = "0">';
83
            }
84
85
            ++$cnt;
86
        }
87
88
        if ($cnt == $showhowmany) {
89
            break;
90
        }
91
    }
92
93
    $block['counterhead'] = B_STATS_REFERACCUM;
94
    $block['referhits']   = $referhits;
95
96
    return $block;
97
}
98
99
function b_referaccum_edit($options)
100
{
101
    $inputtag = "<input type='text' name='options[0]' value='" . $options[0] . "'>";
102
    $form     = sprintf(B_STATS_REFERDISPLAY, $inputtag);
103
    $temp     = sprintf(B_STATS_SELFREFERDISPLAY, $_SERVER['HTTP_HOST']);
104
    $form     .= '<hr>' . $temp . "<br><br><input type='radio' id='options[1]' name='options[1]' value='1'";
105
    if (1 == $options[2]) {
106
        $form .= ' checked';
107
    }
108
    $form .= '>&nbsp;' . _YES . "<br><input type='radio' id='options[1]' name='options[1]' value='0'";
109
    if (0 == $options[2]) {
110
        $form .= ' checked';
111
    }
112
    $form .= '>&nbsp;' . _NO;
113
114
    return $form;
115
}
116