Passed
Pull Request — master (#29)
by Michael
05:40 queued 02:48
created

Tagbar::getTagbar()   B

Complexity

Conditions 11
Paths 34

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 29
nc 34
nop 3
dl 0
loc 53
rs 7.3166
c 2
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
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
     * @return array
33
     * {@internal param int $itemid }}
34
     */
35
    public function getTagbar($tags, int $catid = 0, int $modid = 0): array
36
    {
37
        static $loaded, $delimiter;
38
39
        if (empty($tags)) {
40
            return [];
41
        }
42
43
        $helper = Helper::getInstance();
44
        Utility::tag_define_url_delimiter();
45
        if (null === $loaded) {
46
            require_once $helper->path('include/vars.php');
47
            //require_once $helper->path('include/functions.php');
48
            $helper->loadLanguage('main'); // load Main lang file
49
            /*
50
            if (!($GLOBALS['xoopsModule'] instanceof \XoopsModule)
51
                || ('tag' !== $GLOBALS['xoopsModule']->getVar('dirname'))) {
52
                $helper->loadLanguage('main');
53
            }
54
            */
55
            if (\file_exists($helper->path('assets/images/delimiter.gif'))) {
56
                $delimiter = "<img src='" . $helper->url('assets/images/delimiter.gif') . "' alt=''>";
57
            } else {
58
                $delimiter = "<img src='" . $GLOBALS['xoops']->url('www/images/pointer.gif') . "' alt=''>";
59
            }
60
            $loaded = 1;
61
        }
62
63
        // itemid
64
        if (\is_numeric($tags)) {
65
            if (empty($modid) && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)) {
66
                $modid = $GLOBALS['xoopsModule']->getVar('mid');
67
            }
68
            /** @var \XoopsModules\Tag\TagHandler $tagHandler */
69
            $tagHandler = $helper->getHandler('Tag');
70
            if (!$tags = $tagHandler->getByItem($tags, $modid, $catid)) {
71
                return [];
72
            }
73
            // if ready, do nothing
74
        } elseif (\is_array($tags)) {
75
            // parse
76
        } elseif (!$tags = Utility::tag_parse_tag($tags)) {
77
            return [];
78
        }
79
        $tags_data = [];
80
        foreach ($tags as $tag) {
81
            $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>';
82
        }
83
84
        return [
85
            'title'     => \_MD_TAG_TAGS,
86
            'delimiter' => $delimiter,
87
            'tags'      => $tags_data,
88
        ];
89
    }
90
}
91