b_lxentries_random_show()   C
last analyzed

Complexity

Conditions 9
Paths 104

Size

Total Lines 124
Code Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 96
nc 104
nop 0
dl 0
loc 124
rs 6.505
c 0
b 0
f 0

How to fix   Long Method   

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
/**
4
 * Module: Lexikon - glossary module
5
 * Author: hsalazar
6
 * Licence: GNU
7
 */
8
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
9
10
/**
11
 * @return array
12
 */
13
function b_lxentries_random_show()
14
{
15
    global $xoopsDB, $xoopsUser, $xoopsConfig, $xoopsModule;
16
    $myts = \MyTextSanitizer::getInstance();
17
18
    /** @var \XoopsModuleHandler $moduleHandler */
19
    $moduleHandler = xoops_getHandler('module');
20
    $lexikon       = $moduleHandler->getByDirname('lexikon');
21
22
    if (!isset($lxConfig)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lxConfig seems to never exist and therefore isset should always be false.
Loading history...
23
        /** @var \XoopsConfigHandler $configHandler */
24
        $configHandler = xoops_getHandler('config');
25
        $lxConfig      = $configHandler->getConfigsByCat(0, $lexikon->getVar('mid'));
26
    }
27
    $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
28
    /** @var \XoopsGroupPermHandler $grouppermHandler */
29
    $grouppermHandler = xoops_getHandler('groupperm');
30
    $module_id        = $lexikon->getVar('mid');
31
    $allowed_cats     = $grouppermHandler->getItemIds('lexikon_view', $groups, $module_id);
32
    $catids           = implode(',', $allowed_cats);
33
    $catperms         = " AND categoryID IN ($catids) ";
34
35
    $adminlinks     = '';
36
    $block          = [];
37
    $block['title'] = _MB_LEXIKON_RANDOMTITLE;
38
39
    [$numrows] = $xoopsDB->fetchRow($xoopsDB->query('SELECT COUNT(entryID) FROM ' . $xoopsDB->prefix('lxentries') . " WHERE offline= '0' AND block = '1' " . $catperms . ' '));
40
41
    if ($numrows > 1) {
42
        --$numrows;
43
        $entrynumber = random_int(0, $numrows);
44
    } else {
45
        $entrynumber = 0;
46
    }
47
48
    $result = $xoopsDB->query('SELECT entryID, categoryID, term, definition FROM ' . $xoopsDB->prefix('lxentries') . " WHERE offline = '0' AND block = '1' " . $catperms . " LIMIT $entrynumber, 1");
49
50
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
51
        //$entryID = (int)($entryID);
52
        $entryID = (int)$myrow['entryID'];
53
        $term    = ucfirst($myts->displayTarea($myrow['term']));
54
55
        if (XOOPS_USE_MULTIBYTES) {
56
            $deftemp    = xoops_substr($myrow['definition'], 0, $lxConfig['rndlength'] - 1);
57
            $definition = $myts->displayTarea($deftemp, 1, 1, 1, 1, 1);
58
        }
59
60
        $categoryID = $myrow['categoryID'];
61
        $result_cat = $xoopsDB->query('SELECT categoryID, name FROM ' . $xoopsDB->prefix('lxcategories') . " WHERE categoryID = $categoryID");
62
        [$categoryID, $name] = $xoopsDB->fetchRow($result_cat);
63
        $categoryname = $myts->displayTarea($name);
64
65
        //TODO switch to central icons repository
66
67
        if ($xoopsUser) {
68
            if ($xoopsUser->isAdmin()) {
69
                $adminlinks = '<a href="'
70
                              . XOOPS_URL
71
                              . '/modules/'
72
                              . $lexikon->dirname()
73
                              . '/admin/entry.php?op=mod&entryID='
74
                              . $entryID
75
                              . '" target="_blank"><img src="'
76
                              . XOOPS_URL
77
                              . '/modules/'
78
                              . $lexikon->dirname()
79
                              . '/assets/images/edit.gif" alt="'
80
                              . _MB_LEXIKON_EDITTERM
81
                              . '" width="16" height="16" ></a>&nbsp;<a href="'
82
                              . XOOPS_URL
83
                              . '/modules/'
84
                              . $lexikon->dirname()
85
                              . '/admin/entry.php?op=del&entryID='
86
                              . $entryID
87
                              . '" target="_self"><img src="'
88
                              . XOOPS_URL
89
                              . '/modules/'
90
                              . $lexikon->dirname()
91
                              . '/assets/images/delete.gif" alt="'
92
                              . _MB_LEXIKON_DELTERM
93
                              . '" width="16" height="16" ></a>&nbsp;';
94
            }
95
        }
96
        $userlinks = '<a href="'
97
                     . XOOPS_URL
98
                     . '/modules/'
99
                     . $lexikon->dirname()
100
                     . '/print.php?entryID='
101
                     . $entryID
102
                     . '" target="_blank"><img src="'
103
                     . XOOPS_URL
104
                     . '/modules/'
105
                     . $lexikon->dirname()
106
                     . '/assets/images/print.gif" alt="'
107
                     . _MB_LEXIKON_PRINTTERM
108
                     . '" width="16" height="16" ></a>&nbsp;<a href="mailto:?subject='
109
                     . sprintf(_MB_LEXIKON_INTENTRY, $xoopsConfig['sitename'])
110
                     . '&amp;body='
111
                     . sprintf(_MB_LEXIKON_INTENTRYFOUND, $xoopsConfig['sitename'])
112
                     . ':&nbsp;'
113
                     . XOOPS_URL
114
                     . '/modules/'
115
                     . $lexikon->dirname()
116
                     . '/entry.php?entryID='
117
                     . $entryID
118
                     . '" target="_blank"><img src="'
119
                     . XOOPS_URL
120
                     . '/modules/'
121
                     . $lexikon->dirname()
122
                     . '/assets/images/friend.gif" alt="'
123
                     . _MB_LEXIKON_SENDTOFRIEND
124
                     . '" width="16" height="16" ></a>&nbsp;';
125
126
        if (1 == $lxConfig['multicats']) {
127
            $block['content'] = '<div style="font-size: 12px; font-weight: bold; background-color: #ccc; padding: 4px; margin: 0;"><a href="' . XOOPS_URL . '/modules/' . $lexikon->dirname() . "/category.php?categoryID=$categoryID\">$categoryname</a></div>";
128
            $block['content'] .= "<div style=\"padding: 4px 0 0 0; color: #456;\"><h5 style=\"margin: 0;\">$adminlinks $userlinks <a href=\"" . XOOPS_URL . '/modules/' . $lexikon->dirname() . "/entry.php?entryID=$entryID\">$term</a></h5>$definition</div>";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $definition does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
        } else {
130
            $block['content'] = "<div style=\"padding: 4px; color: #456;\"><h5 style=\"margin: 0;\">$adminlinks $userlinks <a style=\"margin: 0;\" href=\"" . XOOPS_URL . '/modules/' . $lexikon->dirname() . "/entry.php?entryID=$entryID\">$term</a></h5>$definition</div>";
131
        }
132
    }
133
134
    $block['content'] .= '<div style="text-align: right; font-size: x-small;"><a href="' . XOOPS_URL . '/modules/' . $lexikon->dirname() . '/index.php">' . _MB_LEXIKON_SEEMORE . '</a></div>';
135
136
    return $block;
137
}
138