Passed
Pull Request — master (#24)
by Michael
02:52
created

tagBar()   B

Complexity

Conditions 11
Paths 34

Size

Total Lines 62
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 35
c 1
b 0
f 0
nc 34
nop 3
dl 0
loc 62
rs 7.3166

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
 * XOOPS tag management module
14
 *
15
 * @package        XoopsModules\Tag
16
 * @copyright      {@link http://sourceforge.net/projects/xoops/ The XOOPS Project}
17
 * @license        {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
18
 * @author         Taiwen Jiang <[email protected]>
19
 * @since          1.00
20
 */
21
 
22
 use XoopsModules\Tag\Utility;
23
 
24
(defined('XOOPS_ROOT_PATH') && ($GLOBALS['xoopsModule'] instanceof XoopsModule)) || exit('Restricted access');
25
26
/**
27
 * Display tag list
28
 *
29
 * @todo move this to a namespaced class
30
 * @param  int|array $tags array of tag string
31
 *                         OR
32
 * @param  int       $catid
33
 * @param  int       $modid
34
 * @return array
35
 * {@internal param int $itemid }}
36
 */
37
function tagBar($tags, $catid = 0, $modid = 0)
38
{
39
    static $loaded, $delimiter;
40
41
    if (empty($tags)) {
42
        return [];
43
    }
44
45
    /** @var \XoopsModules\Tag\Helper $helper */
46
    $helper = \XoopsModules\Tag\Helper::getInstance();
47
48
    if (null === $loaded) {
49
        require_once $helper->path('include/vars.php');
50
        //require_once $helper->path('include/functions.php');
51
        Utility::tag_define_url_delimiter();
52
        $helper->loadLanguage('main'); // load Main lang file
53
        /*
54
        if (!($GLOBALS['xoopsModule'] instanceof \XoopsModule)
55
            || ('tag' !== $GLOBALS['xoopsModule']->getVar('dirname'))) {
56
            $helper->loadLanguage('main');
57
        }
58
        */
59
        if (file_exists($helper->path('assets/images/delimiter.gif'))) {
60
            $delimiter = "<img src='" . $helper->url('assets/images/delimiter.gif') . "' alt=''>";
61
        } else {
62
            $delimiter = "<img src='" . $GLOBALS['xoops']->url('www/images/pointer.gif') . "' alt=''>";
63
        }
64
        $loaded = 1;
65
    }
66
67
    // itemid
68
    if (is_numeric($tags)) {
69
        if (empty($modid) && ($GLOBALS['xoopsModule'] instanceof XoopsModule)) {
70
            $modid = $GLOBALS['xoopsModule']->getVar('mid');
71
        }
72
        /** @var \XoopsModules\Tag\TagHandler $tagHandler */
73
        $tagHandler = $helper->getHandler('Tag');
74
        if (!$tags = $tagHandler->getByItem($tags, $modid, $catid)) {
75
            return [];
76
        }
77
78
        // if ready, do nothing
79
    } elseif (is_array($tags)) {
0 ignored issues
show
introduced by
The condition is_array($tags) is always true.
Loading history...
80
        // parse
81
    } elseif (!$tags = Utility::tag_parse_tag($tags)) {
82
        return [];
83
    }
84
    $tags_data = [];
85
    foreach ($tags as $tag) {
86
        $tags_data[] = "<a href='"
87
                       . $helper->url('view.tag.php' . URL_DELIMITER . urlencode($tag))
88
                       . "' title='"
89
                       . htmlspecialchars($tag, ENT_QUOTES | ENT_HTML5)
90
                       . "'>"
91
                       . htmlspecialchars($tag, ENT_QUOTES | ENT_HTML5)
92
                       . '</a>';
93
    }
94
95
    return [
96
        'title'     => _MD_TAG_TAGS,
97
        'delimiter' => $delimiter,
98
        'tags'      => $tags_data,
99
    ];
100
}
101