Passed
Pull Request — master (#7)
by Michael
03:59
created

b_xoopsfaq_recent_show()   D

Complexity

Conditions 14
Paths 264

Size

Total Lines 67
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 46
c 1
b 0
f 0
nc 264
nop 1
dl 0
loc 67
rs 4.6333

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 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
 * @package   module\xoopsfaq\blocks
17
 * @author    ZySpec
18
 * @author    XOOPS Module Development Team
19
 * @copyright Copyright (c) 2001-2020 {@link https://xoops.org XOOPS Project}
20
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
21
 * @since     ::   1.25
22
 *
23
 * @see       Xmf\Module\Helper
24
 * @see       MyTextSanitizer
25
 */
26
27
use XoopsModules\Xoopsfaq;
28
29
/**
30
 * Display the most recent FAQs added
31
 *
32
 * @param array $options for display parameters
33
 *                       [0] = num of FAQs to show
34
 *                       [1] = num chars in answer(s) to show
35
 *                       [2] = display date added
36
 *                       [3] = FAQ categories to use
37
 *
38
 * @return array contains recent FAQ(s) parameters
39
 */
40
function b_xoopsfaq_recent_show($options)
41
{
42
    $moduleDirName = basename(dirname(__DIR__));
43
44
    $myts = \MyTextSanitizer::getInstance();
45
46
    /** @var Xoopsfaq\CategoryHandler $categoryHandler */ /** @var Xoopsfaq\ContentsHandler $contentsHandler */
47
    /** @var Xoopsfaq\Helper $helper */
48
    $helper          = \XoopsModules\Xoopsfaq\Helper::getInstance();
49
    $contentsHandler = $helper->getHandler('Contents');
50
    $permHelper      = new \Xmf\Module\Helper\Permission($moduleDirName);
51
    $block           = [];
52
53
    $criteria = new \CriteriaCompo();
54
    $criteria->add(new \Criteria('contents_active', Xoopsfaq\Constants::ACTIVE, '='));
55
    $criteria->setSort('contents_publish DESC, contents_weight');
56
    $criteria->order = 'ASC';
57
58
    $options[3] = isset($options[3]) ? $options[3] : [0];
59
    $cTu        = $catsToUse = (false === 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

59
    $cTu        = $catsToUse = (false === 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 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

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