b_xoopsfaq_random_show()   B
last analyzed

Complexity

Conditions 10
Paths 108

Size

Total Lines 63
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 42
c 2
b 0
f 0
nc 108
nop 1
dl 0
loc 63
rs 7.6

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
 * Display/Edit Random Term Block file
15
 *
16
 * @author    hsalazar
17
 * @author    ZySpec
18
 * @author    XOOPS Module Development Team
19
 * @copyright Copyright (c) 2001-2017 {@link https://xoops.org XOOPS Project}
20
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
21
 * @since     ::   1.23
22
 *
23
 * @see       \XoopsModules\Xoopsfaq\Helper
24
 */
25
26
use Xmf\Module\Helper\Permission;
27
use XoopsModules\Xoopsfaq\{
28
    Constants,
29
    CategoryHandler,
30
    ContentsHandler,
31
    Helper
32
};
33
34
/**
35
 * Display Random FAQ Block
36
 *
37
 * @param array $options
38
 *              [0] - number of chars in title to show (0 = unlimited)
39
 *              [1] - comma separated list of categories to use/select from
40
 * @return array contains random FAQ parameters
41
 */
42
function b_xoopsfaq_random_show(array $options): array
43
{
44
    $moduleDirName = \basename(\dirname(__DIR__));
45
    $myts          = \MyTextSanitizer::getInstance();
46
47
    /** @var Helper $helper */
48
    $helper     = Helper::getInstance();
49
    $permHelper = new Permission($moduleDirName);
50
51
    /** @var ContentsHandler $contentsHandler */
52
    $contentsHandler = $helper->getHandler('Contents');
53
    $block           = [];
54
55
    $criteria = new \CriteriaCompo();
56
    $criteria->add(new \Criteria('contents_active', Constants::ACTIVE, '='));
57
58
    // Filter out cats based on group permissions
59
    $options[1] ??= [0];
60
    $cTu        = $catsToUse = (false === mb_strpos((string)$options[1], ',')) ? (array)$options[1] : explode(',', (string)$options[1]);
61
    if (in_array(0, $catsToUse, true) || empty($catsToUse)) {
62
        // Get a list of all cats
63
        /** @var CategoryHandler $categoryHandler */
64
        $categoryHandler = $helper->getHandler('Category');
65
        $catListArray    = $categoryHandler->getList();
66
        $cTu             = $catsToUse = array_keys($catListArray);
67
    }
68
    // Remove any cats this user doesn't have rights to view
69
    foreach ($cTu as $key => $thisCat) {
70
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
71
            unset($catsToUse[$key]);
72
        }
73
    }
74
    if (!empty($catsToUse)) {
75
        $criteria->add(new \Criteria('contents_cid', '(' . implode(',', $catsToUse) . ')', 'IN'));
76
    } else {
77
        return $block;
78
    }
79
    $xpFaqObjArray = $contentsHandler->getAll($criteria);
80
    $faqCount      = (is_array($xpFaqObjArray) && !empty($xpFaqObjArray)) ? count($xpFaqObjArray) : 0;
81
82
    if ($faqCount > 0) {
83
        $faqNum   = array_rand($xpFaqObjArray, 1);
84
        $xpFaqObj = $xpFaqObjArray[$faqNum];
85
        $faq      = $myts->displayTarea($xpFaqObj->getVar('contents_title'));
86
        $txtAns   = strip_tags((string)$xpFaqObj->getVar('contents_contents')); // get rid of html for block
87
        $faqAns   = $myts->displayTarea(xoops_substr($txtAns, 0, $options[0]), 0, 0, 0, 0, 0);
88
        /** @var CategoryHandler $categoryHandler */
89
        $categoryHandler = $helper->getHandler('Category');
90
        $catObj          = $categoryHandler->get($xpFaqObj->getVar('contents_cid'));
91
        $cid             = $catObj->getVar('category_id');
92
        $block           = [
93
            'title'    => _MB_XOOPSFAQ_RANDOM_TITLE,
94
            'faq'      => $faq,
95
            'faqans'   => $faqAns,
96
            'morelink' => $helper->url('index.php?cat_id=' . $cid . '#q' . $xpFaqObj->getVar('contents_id')),
97
            'linktxt'  => _MB_XOOPSFAQ_SEE_MORE,
98
            'catlink'  => $helper->url('index.php?cat_id=' . $cid),
99
            'cattxt'   => $catObj->getVar('category_title'),
100
        ];
101
        unset($xpFaqObj, $catObj);
102
    }
103
104
    return $block;
105
}
106
107
/**
108
 * Edit Random FAQ Block preferences
109
 *
110
 * @param array $options
111
 *              [0] - number of chars in title to show (0 = unlimited)
112
 *              [1] - comma separated list of categories to use/select from
113
 * @return string HTML entities to display for user input
114
 */
115
function b_xoopsfaq_rand_edit(array $options): string
116
{
117
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
118
    xoops_load('XoopsFormSelect');
119
120
    /** @var Helper $helper */
121
    $helper = Helper::getInstance();
122
    /** @var CategoryHandler $categoryHandler */
123
    $categoryHandler = $helper->getHandler('Category');
124
125
    $catList     = $categoryHandler->getList();
126
    $optionArray = array_merge([0 => _MB_XOOPSFAQ_ALL_CATS], $catList);
127
    $formSelect  = new \XoopsFormSelect('category', 'options[1]', null, 3, true);
128
    $formSelect->addOptionArray($optionArray);
129
    $selOptions = (false === mb_strpos((string)$options[1], ',')) ? $options[1] : explode(',', (string)$options[1]);
130
    $formSelect->setValue($selOptions);
131
    $selectCat = $formSelect->render();
132
133
    $form = '<div class="line140">' . _MB_XOOPSFAQ_CHARS . '&nbsp;' . '<input type="number" name="options[0]" value="' . $options[0] . '" style="width: 5em;" min="0" class="right">&nbsp;' . _MB_XOOPSFAQ_LENGTH . '<br><br>' . _MB_XOOPSFAQ_ALL_CATS_INTRO . '&nbsp;&nbsp;' . $selectCat . '</div>';
134
135
    return $form;
136
}
137