b_scrolling_term_edit()   F
last analyzed

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 60
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 47
nc 327680
nop 1
dl 0
loc 60
rs 0
c 0
b 0
f 0

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
/** entries_scrolling.php v.1
4
 * XOOPS - PHP Content Management System
5
 * Copyright (c) 2017 <https://xoops.org>
6
 *
7
 * Module: lexikon 1.5 beta
8
 * Author : Yerres
9
 * Licence : GPL
10
 */
11
12
use XoopsModules\Lexikon\{
13
    Utility
14
};
15
16
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
17
18
/**
19
 * @param $options
20
 * @return array
21
 */
22
function b_scrolling_term_show($options)
23
{
24
    global $xoopsDB, $xoopsUser;
25
    $myts = MyTextSanitizer:: getInstance();
26
    $utility = new Utility();
27
28
    /** @var \XoopsModuleHandler $moduleHandler */
29
    $moduleHandler = xoops_getHandler('module');
30
    $lexikon       = $moduleHandler->getByDirname('lexikon');
31
    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...
32
        /** @var \XoopsConfigHandler $configHandler */
33
        $configHandler = xoops_getHandler('config');
34
        $lxConfig      = $configHandler->getConfigsByCat(0, $lexikon->getVar('mid'));
35
    }
36
    require_once XOOPS_ROOT_PATH . '/modules/lexikon/class/Utility.php';
37
38
    $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
39
    /** @var \XoopsGroupPermHandler $grouppermHandler */
40
    $grouppermHandler = xoops_getHandler('groupperm');
41
    $module_id        = $lexikon->getVar('mid');
42
    $allowed_cats     = $grouppermHandler->getItemIds('lexikon_view', $groups, $module_id);
43
44
    $block                = [];
45
    $block['speed']       = isset($options[1]) && '' != $options[1] ? $options[1] : '';
46
    $block['bgcolor']     = isset($options[2]) && '' != $options[2] ? $options[2] : '#FFFFFF';
47
    $block['direction']   = $options[3];
48
    $block['alternate']   = isset($options[4]) ? 1 : 0;
49
    $block['includedate'] = isset($options[6]) ? 1 : 0;
50
    $block['style']       = $options[7];
51
52
    if (!empty($options[10])) {
53
        $categories = array_filter(array_slice($options, 10));
54
    } else {
55
        $categories = $allowed_cats;
56
    }
57
    $categories = array_intersect($categories, $allowed_cats);
58
    $categories = implode(',', $categories);
59
    if (0 == count($categories)) {
0 ignored issues
show
Bug introduced by
$categories of type string is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

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

59
    if (0 == count(/** @scrutinizer ignore-type */ $categories)) {
Loading history...
60
        return $block;
61
    }
62
63
    $sql    = $xoopsDB->query(
64
        '
65
      SELECT entryID, term, definition, datesub, html
66
      FROM ' . $xoopsDB->prefix('lxentries') . '
67
      WHERE datesub < ' . time() . " AND datesub > 0 AND offline = '0' AND submit = '0' AND request = '0' AND  categoryID IN (" . $categories . ')
68
      ORDER BY ' . $options[8] . ' ' . $options[9] . '
69
      LIMIT 0, ' . $options[0] . ' '
70
    );
71
    $totals = $xoopsDB->getRowsNum($sql);
72
73
    if ($totals > 1) {
74
        while (list($entryID, $term, $definition, $datesub, $html) = $xoopsDB->fetchRow($sql)) {
75
            $items         = [];
76
            $userlink      = '<a style="cursor:help;background-color: transparent;" href=\"' . XOOPS_URL . '/modules/' . $lexikon->dirname() . '/entry.php?entryID=' . (int)$entryID . '\">';
77
            $items['id']   = (int)$entryID;
78
            $items['term'] = htmlspecialchars($term, ENT_QUOTES | ENT_HTML5);
79
            if ($options[5] > 0) {
80
                $html                = 1 == $html ? 1 : 0;
81
                $definition          = preg_replace("/'/", '’', $definition);
82
                $items['definition'] = $utility::truncateTagSafe($myts->displayTarea($definition, $html), $options[5] + 3);
83
            } else {
84
                $items['definition'] = '';
85
            }
86
            if ('1' == $options[6]) {
87
                $items['date'] = formatTimestamp($datesub, $lxConfig['dateformat']);
88
            }
89
            $items['url']           = $userlink;
90
            $block['scrollitems'][] = $items;
91
        }
92
    }
93
94
    return $block;
95
}
96
97
/**
98
 * @param $options
99
 * @return string
100
 */
101
function b_scrolling_term_edit($options)
102
{
103
    global $xoopsDB;
104
    $myts = MyTextSanitizer:: getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
105
    $form = "<table style='width:100%' class='bg2'>";
106
    $form .= "<tr><th style='width:50%'>" . _OPTIONS . "</th><th style='width:50%'>" . _MB_LEXIKON_SETTINGS . '</th></tr>';
107
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_BLIMIT . "</td><td class='odd'><input type='text' name='options[0]' size='16' maxlength=3 value='" . $options[0] . "' ></td></tr>";
108
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_BSPEED . "</td><td class='odd'><input type='text' name='options[1]' size='16' maxlength=2 value='" . $options[1] . "' ></td></tr>";
109
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_BACKGROUNDCOLOR . "</td><td class='odd'><input type='text' name='options[2]' size='16'  value='" . $options[2] . "' ></td></tr>";
110
    //---
111
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_DIRECTION . "</td><td class='odd'><select name='options[3]'>";
112
    $form .= "<option value='up' " . (('up' === $options[3]) ? ' selected' : '') . '>' . _MB_LEXIKON_UP . "</option>\n";
113
    $form .= "<option value='down' " . (('down' === $options[3]) ? ' selected' : '') . '>' . _MB_LEXIKON_DOWN . "</option>\n";
114
    $form .= '</select></td></tr>';
115
    //---
116
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_ALTERNATE . "</td><td class='odd'>";
117
    $form .= "<input type='radio' name='options[4]' value='1'" . ((1 == $options[4]) ? ' checked' : '') . ' >' . _YES . '&nbsp;';
118
    $form .= "<input type='radio' name='options[4]' value='0'" . ((0 == $options[4]) ? ' checked' : '') . ' >' . _NO . '<br></td></tr>';
119
    //---
120
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_CHARS . " </td><td class='odd'><input type='text' name='options[5]' value='" . $options[5] . "' ></td></tr>";
121
    //---
122
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_TERMSTOSHOW . ' ' . _MB_LEXIKON_SHOWDATE . "</td><td class='odd'>";
123
    $form .= "<input type='radio' name='options[6]' value='1'" . ((1 == $options[6]) ? ' checked' : '') . ' >' . _YES . '&nbsp;';
124
    $form .= "<input type='radio' name='options[6]' value='0'" . ((0 == $options[6]) ? ' checked' : '') . ' >' . _NO . '<br></td></tr>';
125
    //---
126
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_DISP . "</td><td class='odd'><select name='options[7]'>";
127
    $form .= "<option value='0' " . (('0' == $options[7]) ? ' selected' : '') . '>' . _MB_LEXIKON_MARQUEE . "</option>\n";
128
    $form .= "<option value='1' " . (('1' == $options[7]) ? ' selected' : '') . '>' . _MB_LEXIKON_PAUSESCROLLER . "</option>\n";
129
    $form .= "<option value='2' " . (('2' == $options[7]) ? ' selected' : '') . '>' . _MB_LEXIKON_DOMTICKER . "</option>\n";
130
    $form .= '</select></td></tr>';
131
    //---
132
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_SORT . "</td><td class='odd'><select name='options[8]'>";
133
    $form .= "<option value='RAND()' " . (('RAND()' === $options[8]) ? ' selected' : '') . '>' . _MB_LEXIKON_RANDOM . "</option>\n";
134
    $form .= "<option value='datesub' " . (('datesub' === $options[8]) ? ' selected' : '') . '>' . _MB_LEXIKON_DATE . "</option>\n";
135
    $form .= "<option value='counter' " . (('counter' === $options[8]) ? ' selected' : '') . '>' . _MB_LEXIKON_HITS . "</option>\n";
136
    $form .= "<option value='term' " . (('term' === $options[8]) ? ' selected' : '') . '>' . _MB_LEXIKON_NAME . "</option>\n";
137
    $form .= '</select></td></tr>';
138
    //---
139
    $form .= "<tr><td class='even'>" . _MB_LEXIKON_ORDER . "</td><td class='odd'><select name='options[9]'>";
140
    $form .= "<option value='ASC' " . (('ASC' === $options[9]) ? ' selected' : '') . '>' . _ASCENDING . "</option>\n";
141
    $form .= "<option value='DESC' " . (('DESC' === $options[9]) ? ' selected' : '') . '>' . _DESCENDING . "</option>\n";
142
    $form .= '</select></td></tr>';
143
    //--- get allowed categories
144
    $isAll       = empty($options[10]);
145
    $options_cat = array_slice($options, 10);
146
    $form        .= "<tr><td class='even'>" . _MB_LEXIKON_CATEGORY . "</td><td class='odd'><select name=\"options[]\" multiple=\"multiple\">";
147
    $form        .= '<option value="0" ';
148
    if ($isAll) {
149
        $form .= ' selected="selected"';
150
    }
151
    $form      .= '>' . _ALL . '</option>';
152
    $resultcat = $xoopsDB->query('SELECT categoryID, name FROM ' . $xoopsDB->prefix('lxcategories') . ' ORDER BY categoryID ASC');
153
    while (list($categoryID, $name) = $xoopsDB->fetchRow($resultcat)) {
154
        $sel  = ($isAll || in_array($categoryID, $options_cat)) ? ' selected' : '';
155
        $form .= '<option value=' . $categoryID . " $sel>$categoryID : $name</option>\n";
156
    }
157
    $form .= '</select></td></tr>';
158
    $form .= '</table>';
159
    //--------
160
    return $form;
161
}
162