b_xoopsfaq_recent_show()   C
last analyzed

Complexity

Conditions 13
Paths 132

Size

Total Lines 70
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 49
c 3
b 0
f 0
nc 132
nop 1
dl 0
loc 70
rs 6.35

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 declare(strict_types=1);
2
/*
3
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * Recent Term Block file
15
 *
16
 * @author    ZySpec
17
 * @author    XOOPS Module Development Team
18
 * @copyright Copyright (c) 2001-2020 {@link https://xoops.org XOOPS Project}
19
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
20
 * @since     ::   1.25
21
 *
22
 * @see       Xmf\Module\Helper
23
 * @see       MyTextSanitizer
24
 */
25
26
use Xmf\Module\Helper\Permission;
27
use XoopsModules\Xoopsfaq\{
28
    Constants,
29
    CategoryHandler,
30
    Helper
31
};
32
33
/**
34
 * Display the most recent FAQs added
35
 *
36
 * @param array $options for display parameters
37
 *                       [0] = num of FAQs to show
38
 *                       [1] = num chars in answer(s) to show
39
 *                       [2] = display date added
40
 *                       [3] = FAQ categories to use
41
 *
42
 * @return array contains recent FAQ(s) parameters
43
 */
44
function b_xoopsfaq_recent_show(array $options)
45
{
46
    $moduleDirName = \basename(\dirname(__DIR__));
47
48
    $myts = \MyTextSanitizer::getInstance();
49
50
    /** @var Helper $helper */
51
    $helper          = Helper::getInstance();
52
    $contentsHandler = $helper->getHandler('Contents');
53
    $permHelper      = new Permission($moduleDirName);
54
    $block           = [];
55
56
    $criteria = new \CriteriaCompo();
57
    $criteria->add(new \Criteria('contents_active', Constants::ACTIVE, '='));
58
    $criteria->setSort('contents_publish DESC, contents_weight');
59
    $criteria->order = 'ASC';
60
    $criteria->setLimit($options[0]);
61
62
    $options[3] = $options[3] ?? [0];
63
    $cTu        = $catsToUse = (false === mb_strpos($options[3], ',')) ? (array)$options[3] : explode(',', $options[3]);
0 ignored issues
show
Bug introduced by
It seems like $options[3] can also be of type array<integer,integer>; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
    $cTu        = $catsToUse = (false === mb_strpos($options[3], ',')) ? (array)$options[3] : explode(',', /** @scrutinizer ignore-type */ $options[3]);
Loading history...
Bug introduced by
It seems like $options[3] can also be of type array<integer,integer>; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
    $cTu        = $catsToUse = (false === mb_strpos(/** @scrutinizer ignore-type */ $options[3], ',')) ? (array)$options[3] : explode(',', $options[3]);
Loading history...
64
    if (in_array(0, $catsToUse, true) || empty($catsToUse)) {
65
        // Get a list of all cats
66
        $categoryHandler = $helper->getHandler('Category');
67
        $catListArray    = $categoryHandler->getList();
68
        $cTu             = $catsToUse = array_keys($catListArray);
69
    }
70
    // Remove any cats this user doesn't have rights to view
71
    foreach ($cTu as $key => $thisCat) {
72
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
73
            unset($catsToUse[$key]);
74
        }
75
    }
76
    if (!empty($catsToUse)) {
77
        $criteria->add(new \Criteria('contents_cid', '(' . implode(',', $catsToUse) . ')', 'IN'));
78
    } else {
79
        return $block;
80
    }
81
82
    $fieldsArray = ['contents_cid', 'contents_title', 'contents_contents'];
83
    $faqObjArray = $contentsHandler->getAll($criteria, $fieldsArray);
84
    $faqCount    = is_array($faqObjArray) ? count($faqObjArray) : 0;
0 ignored issues
show
introduced by
The condition is_array($faqObjArray) is always true.
Loading history...
85
    if ($faqCount > 0) {
86
        $block['title']     = _MB_XOOPSFAQ_RECENT_TITLE;
87
        $block['show_date'] = (isset($options[2]) && $options[2] > 0) ? 1 : 0;
88
        foreach ($faqObjArray as $fId => $faqObj) {
89
            $faqTitle = $myts->displayTarea($faqObj->getVar('contents_title'));
90
            if (!empty($options[1])) {
91
                $txtAns = strip_tags($faqObj->getVar('contents_contents')); // get rid of html for block
92
                $faqAns = $myts->displayTarea(xoops_substr($txtAns, 0, $options[1]), 0, 0, 0, 0, 0);
93
            } else {
94
                $faqAns = $myts->displayTarea(
95
                    $faqObj->getVar('contents_contents'),
96
                    (int)$faqObj->getVar('dohtml'),
97
                    (int)$faqObj->getVar('dosmiley'),
98
                    (int)$faqObj->getVar('doxcode'),
99
                    1,
100
                    (int)$faqObj->getVar('dobr')
101
                );
102
            }
103
            $block['faq'][] = [
104
                'title'     => $faqTitle,
105
                'ans'       => $faqAns,
106
                'published' => $faqObj->getPublished(_SHORTDATESTRING),
107
                'id'        => $faqObj->getVar('contents_id'),
108
                'cid'       => $faqObj->getVar('contents_cid'),
109
            ];
110
        }
111
    }
112
113
    return $block;
114
}
115
116
/**
117
 * Edit the most recent FAQs block parameters
118
 *
119
 * @param array $options for display parameters
120
 *                       [0] = num of FAQs to show
121
 *                       [1] = num chars in answer(s) to show
122
 *                       [2] = display date added
123
 *                       [3] = FAQ categories to use
124
 *
125
 * @return string HTML to display to get input from user
126
 */
127
function b_xoopsfaq_recent_edit(array $options)
128
{
129
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
130
    xoops_load('XoopsFormSelect');
131
132
    /** @var CategoryHandler $categoryHandler */
133
    /** @var Helper $helper */
134
    $helper          = Helper::getInstance();
135
    $categoryHandler = $helper->getHandler('Category');
136
137
    $catList     = $categoryHandler->getList();
138
    $optionArray = array_merge([0 => _MB_XOOPSFAQ_ALL_CATS], $catList);
139
    $formSelect  = new \XoopsFormSelect('category', 'options[3]', null, 3, true);
140
    $formSelect->addOptionArray($optionArray);
141
    $selOptions = (false === mb_strpos($options[3], ',')) ? $options[3] : explode(',', $options[3]);
142
    $formSelect->setValue($selOptions);
143
    $selectCat = $formSelect->render();
144
145
    $ychck = (isset($options[2]) && ($options[2] > 0)) ? ' checked' : '';
146
    $nchck = !empty($ychck) ? '' : ' checked';
147
148
    $form = '<div class="line140">'
149
            . _MB_XOOPSFAQ_NUM_FAQS
150
            . '&nbsp;'
151
            . '<input type="number" name="options[0]" value="'
152
            . $options[0]
153
            . '" style="width: 5em;" min="0" class="right"><br>'
154
            . _MB_XOOPSFAQ_CHARS
155
            . '&nbsp;<input type="number" name="options[1]" value="'
156
            . $options[1]
157
            . '" style="width: 5em;" min="0" class="right">&nbsp;'
158
            . _MB_XOOPSFAQ_LENGTH
159
            . '<br>'
160
            . _MB_XOOPSFAQ_SHOW_DATE
161
            . '&nbsp;'
162
            . '<label for="r0">'
163
            . _NO
164
            . '</label>'
165
            . '<input type="radio" name="options[2]" id="r0" value="0"'
166
            . $nchck
167
            . '>&nbsp;'
168
            . '<label for="r1">'
169
            . _YES
170
            . '</label>'
171
            . '<input type="radio" name="options[2]" id="r1" value="1"'
172
            . $ychck
173
            . '>'
174
            . '<br><br>'
175
            . _MB_XOOPSFAQ_ALL_CATS_INTRO
176
            . '&nbsp;&nbsp;'
177
            . $selectCat
178
            . '</div>';
179
180
    return $form;
181
}
182