b_faqs_recent_show()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 78
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 78
rs 8.2448
cc 7
nc 4
nop 1

How to fix   Long Method   

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
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 * @param $options
8
 * @return array
9
 */
10
11
use XoopsModules\Smartfaq;
12
use XoopsModules\Smartfaq\Helper;
13
14
/**
15
 * @param $options
16
 * @return array
17
 */
18
function b_faqs_recent_show($options)
19
{
20
    //    require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
21
    $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
22
23
    /** @var \XoopsModules\Smartfaq\Helper $helper */
24
    $helper            = Helper::getInstance();
25
    $smartModule       = $helper->getModule();
26
    $smartModuleConfig = $helper->getConfig();
27
28
    $block = [];
29
30
    if (0 == $options[0]) {
31
        $categoryid = -1;
32
    } else {
33
        $categoryid = $options[0];
34
    }
35
36
    $sort              = $options[1];
37
    $limit             = $options[2];
38
    $maxQuestionLength = $options[3];
39
40
    // Creating the faq handler object
41
    /** @var \XoopsModules\Smartfaq\FaqHandler $faqHandler */
42
    $faqHandler = $helper->getHandler('Faq');
43
44
    // Creating the category handler object
45
    /** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */
46
    $categoryHandler = $helper->getHandler('Category');
47
48
    // Creating the last FAQs
49
    $faqsObj       = $faqHandler->getAllPublished($limit, 0, $categoryid, $sort);
50
    $allcategories = $categoryHandler->getObjects(null, true);
51
    if ($faqsObj) {
52
        $userids = [];
53
        foreach ($faqsObj as $key => $thisfaq) {
54
            $faqids[]                 = $thisfaq->getVar('faqid');
55
            $userids[$thisfaq->uid()] = 1;
56
        }
57
        /** @var \XoopsModules\Smartfaq\AnswerHandler $answerHandler */
58
        $answerHandler = $helper->getHandler('Answer');
59
        $allanswers    = $answerHandler->getLastPublishedByFaq($faqids);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $faqids seems to be defined by a foreach iteration on line 53. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
60
61
        foreach ($allanswers as $key => $thisanswer) {
62
            $userids[$thisanswer->uid()] = 1;
63
        }
64
65
        /** @var \XoopsMemberHandler $memberHandler */
66
        $memberHandler = xoops_getHandler('member');
67
        $users         = $memberHandler->getUsers(new \Criteria('uid', '(' . implode(',', array_keys($userids)) . ')', 'IN'), true);
68
        foreach ($faqsObj as $iValue) {
69
            $faqs['categoryid']   = $iValue->categoryid();
70
            $faqs['question']     = $iValue->question($maxQuestionLength);
71
            $faqs['faqid']        = $iValue->faqid();
72
            $faqs['categoryname'] = $allcategories[$iValue->categoryid()]->getVar('name');
73
74
            // Creating the answer object
75
            $faqid     = $iValue->faqid();
76
            $answerObj = $allanswers[$faqid];
77
78
            $faqs['date']   = $iValue->datesub();
79
            $faqs['poster'] = '';
80
            if (null !== $answerObj) {
81
                $faqs['poster'] = Smartfaq\Utility::getLinkedUnameFromId($answerObj->uid(), $smartModuleConfig['userealname'], $users);
82
            }
83
84
            $block['faqs'][] = $faqs;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $faqs seems to be defined later in this foreach loop on line 69. Are you sure it is defined here?
Loading history...
85
        }
86
87
        $block['lang_question'] = _MB_SF_FAQS;
88
        $block['lang_category'] = _MB_SF_CATEGORY;
89
        $block['lang_poster']   = _MB_SF_ANSWEREDBY;
90
        $block['lang_date']     = _MB_SF_DATE;
91
        $modulename             = htmlspecialchars($smartModule->getVar('name'), ENT_QUOTES | ENT_HTML5);
92
        $block['lang_visitfaq'] = _MB_SF_VISITFAQ . ' ' . $modulename;
93
    }
94
95
    return $block;
96
}
97
98
/**
99
 * @param $options
100
 * @return string
101
 */
102
function b_faqs_recent_edit($options)
103
{
104
    //    require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
105
106
    $form = Smartfaq\Utility::createCategorySelect($options[0]);
107
108
    $form .= '&nbsp;<br>' . _MB_SF_ORDER . "&nbsp;<select name='options[]'>";
109
110
    $form .= "<option value='datesub'";
111
    if ('datesub' === $options[1]) {
112
        $form .= ' selected';
113
    }
114
    $form .= '>' . _MB_SF_DATE . "</option>\n";
115
116
    $form .= "<option value='counter'";
117
    if ('counter' === $options[1]) {
118
        $form .= ' selected';
119
    }
120
    $form .= '>' . _MB_SF_HITS . "</option>\n";
121
122
    $form .= "<option value='weight'";
123
    if ('weight' === $options[1]) {
124
        $form .= ' selected';
125
    }
126
    $form .= '>' . _MB_SF_WEIGHT . "</option>\n";
127
128
    $form .= "</select>\n";
129
130
    $form .= '&nbsp;' . _MB_SF_DISP . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "'>&nbsp;" . _MB_SF_FAQS;
131
    $form .= '&nbsp;<br>' . _MB_SF_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[3] . "'>&nbsp;" . _MB_SF_LENGTH;
132
133
    return $form;
134
}
135