Passed
Push — master ( 0d43af...be99e1 )
by Michael
02:54 queued 13s
created

Tagbar::getTagbar()   B

Complexity

Conditions 11
Paths 34

Size

Total Lines 46
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 46
rs 7.3166

How to fix   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
            $helper->loadLanguage('main'); // load Main lang file
48
            if (\file_exists($helper->path('assets/images/delimiter.gif'))) {
49
                $delimiter = "<img src='" . $helper->url('assets/images/delimiter.gif') . "' alt=''>";
50
            } else {
51
                $delimiter = "<img src='" . $GLOBALS['xoops']->url('www/images/pointer.gif') . "' alt=''>";
52
            }
53
            $loaded = 1;
54
        }
55
56
        // itemid
57
        if (\is_numeric($tags)) {
58
            if (empty($modid) && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)) {
59
                $modid = $GLOBALS['xoopsModule']->getVar('mid');
60
            }
61
            /** @var \XoopsModules\Tag\TagHandler $tagHandler */
62
            $tagHandler = $helper->getHandler('Tag');
63
            if (!$tags = $tagHandler->getByItem($tags, $modid, $catid)) {
64
                return [];
65
            }
66
            // if ready, do nothing
67
        } elseif (\is_array($tags)) {
68
            // parse
69
        } elseif (!$tags = Utility::tag_parse_tag($tags)) {
70
            return [];
71
        }
72
        $tags_data = [];
73
        foreach ($tags as $tag) {
74
            $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>';
75
        }
76
77
        return [
78
            'title'     => \_MD_TAG_TAGS,
79
            'delimiter' => $delimiter,
80
            'tags'      => $tags_data,
81
        ];
82
    }
83
}
84