b_sitemap_newbb()   C
last analyzed

Complexity

Conditions 13
Paths 72

Size

Total Lines 85
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 57
nc 72
nop 0
dl 0
loc 85
rs 6.6166
c 1
b 0
f 0

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
// FILE        ::    newbb.php
4
// AUTHOR    ::    Ryuji AMANO <[email protected]>
5
// WEB        ::    Ryu's Planning <https://ryus.biz>
6
7
// NewBB plugin: D.J., https://xoops.org.cn
8
9
use XoopsModules\Newbb\{
10
    CategoryHandler,
11
    Helper,
12
    ForumHandler
13
};
14
15
/** @var Helper $helper */
16
/** @var ForumHandler $forumHandler */
17
/** @var CategoryHandler $categoryHandler */
18
19
/**
20
 * @return (((mixed|string)[][]|int|mixed|string)[][]|mixed|string)[][][]
21
 *
22
 * @psalm-return array{parent?: array<array{id?: mixed, cid?: mixed, url?: string, title?: mixed, fchild?: array<array{id: mixed, url: string, title: mixed}>, child?: array<array{id?: mixed, cid?: mixed, url?: string, title?: mixed, fchild?: array<array{id: mixed, url: string, title: mixed}>, image?: 2|3}>}>}
23
 */
24
function b_sitemap_newbb(): array
25
{
26
    global $sitemap_configs;
27
    $sitemap = [];
28
29
    $forumHandler = Helper::getInstance()->getHandler('Forum');
30
    /* Allowed forums */
31
    $forums_allowed = $forumHandler->getIdsByPermission();
32
33
    /* fetch top forums */
34
    $forums_top_id = [];
35
    if (!empty($forums_allowed)) {
36
        $crit_top = new \CriteriaCompo(new \Criteria('parent_forum', '0'));
37
        //$crit_top->add(new \Criteria("cat_id", "(".implode(", ", array_keys($categories)).")", "IN"));
38
        $crit_top->add(new \Criteria('forum_id', '(' . implode(', ', $forums_allowed) . ')', 'IN'));
39
        $forums_top_id = $forumHandler->getIds($crit_top);
40
    }
41
42
    $forums_sub_id = [];
43
    if ($forums_top_id && $sitemap_configs['show_subcategoris']) {
44
        $crit_sub = new \CriteriaCompo(new \Criteria('parent_forum', '(' . implode(', ', $forums_top_id) . ')', 'IN'));
45
        $crit_sub->add(new \Criteria('forum_id', '(' . implode(', ', $forums_allowed) . ')', 'IN'));
46
        $forums_sub_id = $forumHandler->getIds($crit_sub);
47
    }
48
49
    /* Fetch forum data */
50
    $forums_available = array_merge($forums_top_id, $forums_sub_id);
51
    $forums_array     = [];
52
    if ($forums_available !== []) {
53
        $crit_forum = new \Criteria('forum_id', '(' . implode(', ', $forums_available) . ')', 'IN');
54
        $crit_forum->setSort('cat_id ASC, parent_forum ASC, forum_order');
55
        $crit_forum->setOrder('ASC');
56
        $forums_array = $forumHandler->getAll($crit_forum, ['forum_name', 'parent_forum', 'cat_id'], false);
57
    }
58
59
    $forums = [];
60
    foreach ($forums_array as $forumid => $forum) {
61
        if ($forum['parent_forum']) {
62
            $forums[$forum['parent_forum']]['fchild'][$forumid] = [
63
                'id'    => $forumid,
64
                'url'   => 'viewforum.php?forum=' . $forumid,
65
                'title' => $forum['forum_name'],
66
            ];
67
        } else {
68
            $forums[$forumid] = [
69
                'id'    => $forumid,
70
                'cid'   => $forum['cat_id'],
71
                'url'   => 'viewforum.php?forum=' . $forumid,
72
                'title' => $forum['forum_name'],
73
            ];
74
        }
75
    }
76
77
    if ($sitemap_configs['show_subcategoris']) {
78
        $categoryHandler = Helper::getInstance()->getHandler('Category');
79
        $categories      = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $categories is dead and can be removed.
Loading history...
80
        $categories      = $categoryHandler->getByPermission('access', ['cat_id', 'cat_title'], false);
81
82
        foreach ($categories as $key => $category) {
83
            $cat_id                         = $category['cat_id'];
84
            $i                              = $cat_id;
85
            $sitemap['parent'][$i]['id']    = $cat_id;
86
            $sitemap['parent'][$i]['title'] = $category['cat_title'];
87
            $sitemap['parent'][$i]['url']   = XOOPS_URL . '/modules/newbb/index.php?cat=' . $cat_id;
88
        }
89
        foreach ($forums as $id => $forum) {
90
            $cid                                            = $forum['cid'];
91
            $sitemap['parent'][$cid]['child'][$id]          = $forum;
92
            $sitemap['parent'][$cid]['child'][$id]['image'] = 2;
93
            if (empty($forum['fchild'])) {
94
                continue;
95
            }
96
97
            foreach ($forum['fchild'] as $_id => $_forum) {
98
                $sitemap['parent'][$cid]['child'][$_id]          = $_forum;
99
                $sitemap['parent'][$cid]['child'][$_id]['image'] = 3;
100
            }
101
        }
102
    } else {
103
        foreach ($forums as $id => $forum) {
104
            $sitemap['parent'][$id] = $forum;
105
        }
106
    }
107
108
    return $sitemap;
109
}
110