b_xoopsfaq_category_show()   B
last analyzed

Complexity

Conditions 10
Paths 27

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 36
c 2
b 0
f 0
nc 27
nop 1
dl 0
loc 57
rs 7.6666

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 Category Block
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       \XoopsModules\Xoopsfaq\Helper
23
 * @see       \MyTextSanitizer
24
 */
25
26
use Xmf\Module\Helper\Permission;
27
use XoopsModules\Xoopsfaq\{
28
    CategoryHandler,
29
    ContentsHandler,
30
    Helper
31
};
32
33
/**
34
 * Show FAQ Categories Block
35
 *
36
 * Display the most recent FAQs added
37
 *
38
 * @param array $options for display parameters
39
 *                       [0] = only show cats with active FAQs
40
 *
41
 * @return array containing category titles and links
42
 */
43
function b_xoopsfaq_category_show(array $options)
44
{
45
    $moduleDirName = \basename(\dirname(__DIR__));
46
47
    $myts = \MyTextSanitizer::getInstance();
48
49
    /** @var CategoryHandler $categoryHandler */
50
    /** @var ContentsHandler $contentsHandler */
51
    /** @var Helper $helper */
52
    $helper          = Helper::getInstance();
53
    $permHelper      = new Permission($moduleDirName);
54
    $categoryHandler = $helper->getHandler('Category');
55
    $block           = [];
56
57
    // Get a list of all cats this user can access
58
    $catListArray = $categoryHandler->getList();
59
    $cTu          = $catsToUse = array_keys($catListArray);
60
    // Remove any cats this user doesn't have rights to view
61
    foreach ($cTu as $key => $thisCat) {
62
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
63
            unset($catsToUse[$key]);
64
        }
65
    }
66
    // catsToUse contains all categories user has rights to view
67
68
    $criteria = new \CriteriaCompo();
69
    $criteria->setSort('category_order ASC, category_title');
70
    $criteria->order = 'ASC';
71
    if (!isset($options[0]) || 0 === (int)$options[0]) { // only show cats with FAQs
72
        $contentsHandler = $helper->getHandler('Contents');
73
        $faqCountArray   = $contentsHandler->getCategoriesIdsWithContent();
74
        if (is_array($faqCountArray) && !empty($faqCountArray)) {
75
            $catsToShow = array_intersect($catsToUse, array_keys($faqCountArray));
76
            $criteria->add(new \Criteria('category_id', '(' . implode(',', $catsToShow) . ')', 'IN'));
77
        } else {
78
            // there are no categories to show
79
            return $block;
80
        }
81
        unset($contentsHandler, $faqCountArray, $catsToShow);
82
    } else {
83
        $criteria->add(new \Criteria('category_id', '(' . implode(',', $catsToUse) . ')', 'IN'));
84
    }
85
    $fieldsArray = ['category_id', 'category_title'];
86
    $catObjArray = $categoryHandler->getAll($criteria, $fieldsArray);
87
    $catCount    = is_array($catObjArray) ? count($catObjArray) : 0;
0 ignored issues
show
introduced by
The condition is_array($catObjArray) is always true.
Loading history...
88
    if ($catCount > 0) {
89
        $block['title'] = _MB_XOOPSFAQ_CATTITLE;
90
        foreach ($catObjArray as $cId => $catObj) {
91
            $block['cat'][] = [
92
                'title' => $myts->displayTarea($catObj->getVar('category_title')),
93
                'link'  => $helper->url('index.php?cat_id=' . $cId),
94
            ];
95
        }
96
    }
97
    unset($catObjArray, $categoryHandler);
98
99
    return $block;
100
}
101
102
/**
103
 * Edit Recent FAQ Block preferences
104
 *
105
 * @param array $options for display parameters
106
 *                       [0] = only show cats with active FAQs
107
 *
108
 * @return string HTML to display to get input from user
109
 */
110
function b_xoopsfaq_category_edit(array $options)
111
{
112
    $ychck = (isset($options[0]) && ($options[0] > 0)) ? ' checked' : '';
113
    $nchck = !empty($ychck) ? '' : ' checked';
114
    $form  = '<div class="line140">'
115
             . _MB_XOOPSFAQ_SHOW_EMPTY
116
             . '&nbsp;<label for="r0">'
117
             . _NO
118
             . '</label><input type="radio" name="options[0]" id="r0" value="0"'
119
             . $nchck
120
             . '>&nbsp;<label for="r1">'
121
             . _YES
122
             . '</label><input type="radio" name="options[0]" id="r1" value="1"'
123
             . $ychck
124
             . '></div>'
125
             . "\n";
126
127
    return $form;
128
}
129