b_news_topicsnav_show()   B
last analyzed

Complexity

Conditions 11
Paths 14

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 55
rs 7.3166
cc 11
nc 14
nop 1

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
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @author         XOOPS Development Team
16
 */
17
18
use XoopsModules\News;
19
use XoopsModules\News\Helper;
20
use XoopsModules\News\NewsTopic;
21
22
/**
23
 * @param $options
24
 *
25
 * @return array|string
26
 */
27
function b_news_topicsnav_show($options)
28
{
29
    /** @var Helper $helper */
30
    if (!class_exists(Helper::class)) {
31
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array|string.
Loading history...
32
    }
33
34
    $helper = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
35
36
    $myts             = \MyTextSanitizer::getInstance();
37
    $block            = [];
38
    $newscountbytopic = [];
39
    $perms            = '';
40
    $xt               = new NewsTopic();
41
    $restricted       = News\Utility::getModuleOption('restrictindex');
42
    if ($restricted) {
43
        global $xoopsUser;
44
        /** @var \XoopsModuleHandler $moduleHandler */
45
        $moduleHandler = xoops_getHandler('module');
46
        $newsModule    = $moduleHandler->getByDirname('news');
47
        $groups        = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
48
        /** @var \XoopsGroupPermHandler $grouppermHandler */
49
        $grouppermHandler = xoops_getHandler('groupperm');
50
        $topics           = $grouppermHandler->getItemIds('news_view', $groups, $newsModule->getVar('mid'));
51
        if (count($topics) > 0) {
52
            $topics = implode(',', $topics);
53
            $perms  = ' AND topic_id IN (' . $topics . ') ';
54
        } else {
55
            return '';
56
        }
57
    }
58
    $topics_arr = $xt->getChildTreeArray(0, 'topic_title', $perms);
59
    if (1 == $options[0]) {
60
        $newscountbytopic = $xt->getNewsCountByTopic();
61
    }
62
    if (is_array($topics_arr) && count($topics_arr)) {
63
        foreach ($topics_arr as $onetopic) {
64
            if (1 == $options[0]) {
65
                $count = 0;
66
                if (array_key_exists($onetopic['topic_id'], $newscountbytopic)) {
67
                    $count = $newscountbytopic[$onetopic['topic_id']];
68
                }
69
            } else {
70
                $count = '';
71
            }
72
            $block['topics'][] = [
73
                'id'          => $onetopic['topic_id'],
74
                'news_count'  => $count,
75
                'topic_color' => '#' . $onetopic['topic_color'],
76
                'title'       => $myts->displayTarea($onetopic['topic_title']),
77
            ];
78
        }
79
    }
80
81
    return $block;
82
}
83
84
/**
85
 * @param $options
86
 *
87
 * @return string
88
 */
89
function b_news_topicsnav_edit($options)
90
{
91
    $form = _MB_NEWS_SHOW_NEWS_COUNT . " <input type='radio' name='options[]' value='1'";
92
    if (1 == $options[0]) {
93
        $form .= ' checked';
94
    }
95
    $form .= '>' . _YES;
96
    $form .= "<input type='radio' name='options[]' value='0'";
97
    if (0 == $options[0]) {
98
        $form .= ' checked';
99
    }
100
    $form .= '>' . _NO;
101
102
    return $form;
103
}
104
105
/**
106
 * @param $options
107
 */
108
function b_news_topicsnav_onthefly($options): void
109
{
110
    $options = explode('|', $options);
111
    $block   = b_news_topicsnav_show($options);
112
113
    $tpl = new \XoopsTpl();
114
    $tpl->assign('block', $block);
115
    $tpl->display('db:news_block_topicnav.tpl');
116
}
117