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

b_xoopsfaq_random_show()   C

Complexity

Conditions 11
Paths 216

Size

Total Lines 60
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 42
c 1
b 0
f 0
nc 216
nop 1
dl 0
loc 60
rs 6.2833

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
 * Display/Edit Random Term Block file
15
 *
16
 * @package   module\xoopsfaq\blocks
17
 * @author    hsalazar
18
 * @author    ZySpec
19
 * @author    XOOPS Module Development Team
20
 * @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project}
21
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
22
 * @since     ::   1.23
23
 *
24
 * @see       \XoopsModules\Xoopsfaq\Helper
25
 */
26
27
use \XoopsModules\Xoopsfaq;
28
29
/**
30
 * Display Random FAQ Block
31
 *
32
 * @param array $options
33
 *              [0] - number of chars in title to show (0 = unlimited)
34
 *              [1] - comma separated list of categories to use/select from
35
 * @return array contains random FAQ parameters
36
 */
37
function b_xoopsfaq_random_show($options)
38
{
39
    $moduleDirName = basename(dirname(__DIR__));
40
    $myts          = \MyTextSanitizer::getInstance();
41
42
    /** @var Xoopsfaq\CategoryHandler $categoryHandler */ /** @var Xoopsfaq\ContentsHandler $contentsHandler */
43
    /** @var Xoopsfaq\Helper $helper */
44
    $helper          = \XoopsModules\Xoopsfaq\Helper::getInstance();
45
    $permHelper      = new \Xmf\Module\Helper\Permission($moduleDirName);
46
    $contentsHandler = $helper->getHandler('Contents');
47
    $block           = [];
48
49
    $criteria = new \CriteriaCompo();
50
    $criteria->add(new \Criteria('contents_active', Xoopsfaq\Constants::ACTIVE, '='));
51
52
    // Filter out cats based on group permissions
53
    $options[1] = isset($options[1]) ? $options[1] : [0];
54
    $cTu        = $catsToUse = (false === strpos($options[1], ',')) ? (array)$options[1] : explode(',', $options[1]);
0 ignored issues
show
Bug introduced by
It seems like $options[1] 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

54
    $cTu        = $catsToUse = (false === strpos(/** @scrutinizer ignore-type */ $options[1], ',')) ? (array)$options[1] : explode(',', $options[1]);
Loading history...
Bug introduced by
It seems like $options[1] 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

54
    $cTu        = $catsToUse = (false === strpos($options[1], ',')) ? (array)$options[1] : explode(',', /** @scrutinizer ignore-type */ $options[1]);
Loading history...
55
    if (in_array(0, $catsToUse) || empty($catsToUse)) {
56
        // Get a list of all cats
57
        $categoryHandler = $helper->getHandler('Category');
58
        $catListArray    = $categoryHandler->getList();
59
        $cTu             = $catsToUse = array_keys($catListArray);
60
    }
61
    // Remove any cats this user doesn't have rights to view
62
    foreach ($cTu as $key => $thisCat) {
63
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
64
            unset($catsToUse[$key]);
65
        }
66
    }
67
    if (!empty($catsToUse)) {
68
        $criteria->add(new \Criteria('contents_cid', '(' . implode(',', $catsToUse) . ')', 'IN'));
69
    } else {
70
        return $block;
71
    }
72
    $xpFaqObjArray = $contentsHandler->getAll($criteria);
73
    $faqCount      = (is_array($xpFaqObjArray) && !empty($xpFaqObjArray)) ? count($xpFaqObjArray) : 0;
74
75
    if ($faqCount > 0) {
76
        $faqNum   = array_rand($xpFaqObjArray, 1);
77
        $xpFaqObj = $xpFaqObjArray[$faqNum];
78
        $faq      = $myts->displayTarea($xpFaqObj->getVar('contents_title'));
79
        $txtAns   = strip_tags($xpFaqObj->getVar('contents_contents')); // get rid of html for block
80
        $faqAns   = $myts->displayTarea(xoops_substr($txtAns, 0, $options[0]), 0, 0, 0, 0, 0);
81
82
        $categoryHandler = $helper->getHandler('Category');
83
        $catObj          = $categoryHandler->get($xpFaqObj->getVar('contents_cid'));
84
        $cid             = $catObj->getVar('category_id');
85
        $block           = [
86
            'title'    => _MB_XOOPSFAQ_RANDOM_TITLE,
87
            'faq'      => $faq,
88
            'faqans'   => $faqAns,
89
            'morelink' => $helper->url('index.php?cat_id=' . $cid . '#q' . $xpFaqObj->getVar('contents_id')),
90
            'linktxt'  => _MB_XOOPSFAQ_SEE_MORE,
91
            'catlink'  => $helper->url('index.php?cat_id=' . $cid),
92
            'cattxt'   => $catObj->getVar('category_title'),
93
        ];
94
        unset($xpFaqObj, $catObj);
95
    }
96
    return $block;
97
}
98
99
/**
100
 * Edit Random FAQ Block preferences
101
 *
102
 * @param array $options
103
 *              [0] - number of chars in title to show (0 = unlimited)
104
 *              [1] - comma separated list of categories to use/select from
105
 * @return string HTML entities to display for user input
106
 */
107
function b_xoopsfaq_rand_edit($options)
108
{
109
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
110
    xoops_load('XoopsFormSelect');
111
112
    /** @var Xoopsfaq\CategoryHandler $categoryHandler */
113
    /** @var Xoopsfaq\Helper $helper */
114
    $helper          = \XoopsModules\Xoopsfaq\Helper::getInstance();
115
    $categoryHandler = $helper->getHandler('Category');
116
117
    $catList     = $categoryHandler->getList();
118
    $optionArray = array_merge([0 => _MB_XOOPSFAQ_ALL_CATS], $catList);
119
    $formSelect  = new \XoopsFormSelect('category', 'options[1]', null, 3, true);
120
    $formSelect->addOptionArray($optionArray);
121
    $selOptions = (false === strpos($options[1], ',')) ? $options[1] : explode(',', $options[1]);
122
    $formSelect->setValue($selOptions);
123
    $selectCat = $formSelect->render();
124
125
    $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>';
126
    return $form;
127
}
128