Passed
Pull Request — master (#29)
by Michael
02:36
created

Tagbar::getTagbar()   B

Complexity

Conditions 11
Paths 34

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 29
c 2
b 0
f 0
nc 34
nop 3
dl 0
loc 54
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 declare(strict_types=1);
2
3
namespace XoopsModules\Tag;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * XOOPS tag management module
17
 *
18
 * @copyright      {@link https://sourceforge.net/projects/xoops/ The XOOPS Project}
19
 * @license        {@link https://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author         Taiwen Jiang <[email protected]>
21
 * @since          1.00
22
 */
23
(\defined('XOOPS_ROOT_PATH') && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)) || exit('Restricted access');
24
25
class Tagbar
26
{
27
    /**
28
     * Display tag list
29
     *
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
    public function getTagbar($tags, $catid = 0, $modid = 0): array
38
    {
39
        static $loaded, $delimiter;
40
41
        if (empty($tags)) {
42
            return [];
43
        }
44
45
        $helper = Helper::getInstance();
46
47
        if (null === $loaded) {
48
            require_once $helper->path('include/vars.php');
49
            //require_once $helper->path('include/functions.php');
50
            Utility::tag_define_url_delimiter();
51
            $helper->loadLanguage('main'); // load Main lang file
52
            /*
53
            if (!($GLOBALS['xoopsModule'] instanceof \XoopsModule)
54
                || ('tag' !== $GLOBALS['xoopsModule']->getVar('dirname'))) {
55
                $helper->loadLanguage('main');
56
            }
57
            */
58
            if (\file_exists($helper->path('assets/images/delimiter.gif'))) {
59
                $delimiter = "<img src='" . $helper->url('assets/images/delimiter.gif') . "' alt=''>";
60
            } else {
61
                $delimiter = "<img src='" . $GLOBALS['xoops']->url('www/images/pointer.gif') . "' alt=''>";
62
            }
63
            $loaded = 1;
64
        }
65
66
        // itemid
67
        if (\is_numeric($tags)) {
68
            if (empty($modid) && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)) {
69
                $modid = $GLOBALS['xoopsModule']->getVar('mid');
70
            }
71
            /** @var \XoopsModules\Tag\TagHandler $tagHandler */
72
            $tagHandler = $helper->getHandler('Tag');
73
            if (!$tags = $tagHandler->getByItem($tags, $modid, $catid)) {
74
                return [];
75
            }
76
            // if ready, do nothing
77
        } elseif (\is_array($tags)) {
0 ignored issues
show
introduced by
The condition is_array($tags) is always true.
Loading history...
78
            // parse
79
        } elseif (!$tags = Utility::tag_parse_tag($tags)) {
80
            return [];
81
        }
82
        $tags_data = [];
83
        foreach ($tags as $tag) {
84
            $tags_data[] = "<a href='" . $helper->url('view.tag.php' . URL_DELIMITER . \urlencode($tag)) . "' title='" . \htmlspecialchars($tag, \ENT_QUOTES | \ENT_HTML5) . "'>" . \htmlspecialchars($tag, \ENT_QUOTES | \ENT_HTML5) . '</a>';
85
        }
86
87
        return [
88
            'title'     => \_MD_TAG_TAGS,
89
            'delimiter' => $delimiter,
90
            'tags'      => $tags_data,
91
        ];
92
    }
93
}
94