lookup()   B
last analyzed

Complexity

Conditions 10
Paths 16

Size

Total Lines 62
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 40
nc 16
nop 2
dl 0
loc 62
rs 7.6666
c 0
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
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
 * @param mixed $category
17
 * @param mixed $item_id
18
 */
19
20
/**
21
 * @param $category
22
 * @param $item_id
23
 *
24
 * @return mixed
25
 */
26
function lookup($category, $item_id)
27
{
28
    global $xoopsModule, $xoopsModuleConfig, $xoopsConfig;
29
    $moduleDirName = \basename(\dirname(__DIR__));
30
    if (empty($xoopsModule) || $xoopsModule->getVar('dirname') !== $moduleDirName) {
31
        /** @var \XoopsModuleHandler $moduleHandler */
32
        $moduleHandler = xoops_getHandler('module');
33
        $module        = $moduleHandler->getByDirname($moduleDirName);
34
        /** @var \XoopsConfigHandler $configHandler */
35
        $configHandler = xoops_getHandler('config');
36
        $config        = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
37
    } else {
38
        $module = $xoopsModule;
39
        $config = $xoopsModuleConfig;
40
    }
41
42
    if ('global' === $category) {
43
        $item['name'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.
Loading history...
44
        $item['url']  = '';
45
46
        return $item;
47
    }
48
    $item_id = (int)$item_id;
49
50
    global $xoopsDB;
51
    if ('dog' === $category) {
52
        // Assume we have a valid forum id
53
        $sql = 'SELECT pname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . ' WHERE id = ' . $item_id;
54
        if (!$result = $GLOBALS['xoopsDB']->query($sql)) {
55
            redirect_header('index.php', 2, _MD_ERRORFORUM);
0 ignored issues
show
Bug introduced by
The constant _MD_ERRORFORUM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
        }
57
        $result_array = $GLOBALS['xoopsDB']->fetchArray($result);
58
        $item['name'] = $result_array['pname'];
59
        $item['url']  = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/dog.php?id=' . $item_id;
60
61
        return $item;
62
    }
63
64
    if ('thread' === $category) {
65
        // Assume we have a valid topid id
66
        $sql = 'SELECT t.topic_title,f.forum_id,f.forum_name FROM ' . $GLOBALS['xoopsDB']->prefix('bb_topics') . ' t, ' . $GLOBALS['xoopsDB']->prefix('bb_forums') . ' f WHERE t.forum_id = f.forum_id AND t.topic_id = ' . $item_id . ' LIMIT 1';
67
        if (!$result = $GLOBALS['xoopsDB']->query($sql)) {
68
            redirect_header('index.php', 2, _MD_ERROROCCURED);
0 ignored issues
show
Bug introduced by
The constant _MD_ERROROCCURED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
        }
70
        $result_array = $GLOBALS['xoopsDB']->fetchArray($result);
71
        $item['name'] = $result_array['topic_title'];
72
        $item['url']  = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/viewtopic.php?forum=' . $result_array['forum_id'] . '&topic_id=' . $item_id;
73
74
        return $item;
75
    }
76
77
    if ('post' === $category) {
78
        // Assume we have a valid post id
79
        $sql = 'SELECT subject,topic_id,forum_id FROM ' . $GLOBALS['xoopsDB']->prefix('bb_posts') . ' WHERE post_id = ' . $item_id . ' LIMIT 1';
80
        if (!$result = $GLOBALS['xoopsDB']->query($sql)) {
81
            redirect_header('index.php', 2, _MD_ERROROCCURED);
82
        }
83
        $result_array = $GLOBALS['xoopsDB']->fetchArray($result);
84
        $item['name'] = $result_array['subject'];
85
        $item['url']  = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/viewtopic.php?forum= ' . $result_array['forum_id'] . '&amp;topic_id=' . $result_array['topic_id'] . '#forumpost' . $item_id;
86
87
        return $item;
88
    }
89
}
90