Completed
Push — master ( d1a68a...39bb73 )
by Michael
01:32
created

xoopsfaq_recent.php ➔ b_xoopsfaq_recent_edit()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 1
dl 0
loc 54
rs 8.6925
c 0
b 0
f 0

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
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]);
60 View Code Duplication
    if (in_array(0, $catsToUse) || empty($catsToUse)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    foreach ($cTu as $key => $thisCat) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
69
            unset($catsToUse[$key]);
70
        }
71
    }
72 View Code Duplication
    if (!empty($catsToUse)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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;
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
$moduleDirName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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