Utility   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 65
c 7
b 0
f 0
dl 0
loc 144
rs 10
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tag_define_url_delimiter() 0 12 4
A tag_get_delimiter() 0 16 3
B tag_load_config() 0 31 7
A tag_parse_tag() 0 10 2
B tag_parse_args() 0 27 6
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tag;
4
5
/**
6
 * Class Utility
7
 */
8
class Utility extends Common\SysUtility
9
{
10
    //--------------- Custom module methods -----------------------------
11
    /**
12
     * Load Tag module configs
13
     *
14
     * @return array|mixed|null
15
     */
16
    public static function tag_load_config()
17
    {
18
        static $moduleConfig;
19
20
        if (null === $moduleConfig) {
21
            $moduleConfig = [];
22
            $helper       = \XoopsModules\Tag\Helper::getInstance();
23
            if ($GLOBALS['xoopsModule'] instanceof \XoopsModule
24
                && !empty($GLOBALS['xoopsModuleConfig'])
25
                && ($helper->getDirname() === $GLOBALS['xoopsModule']->getVar('dirname', 'n'))) {
26
                $moduleConfig = $GLOBALS['xoopsModuleConfig'];
27
            } else {
28
                $mid = $helper->getModule()->getVar('mid');
29
                /** @var \XoopsConfigHandler $configHandler */
30
                $configHandler = \xoops_getHandler('config');
31
32
                $criteria = new \Criteria('conf_modid', $mid);
0 ignored issues
show
Bug introduced by
It seems like $mid can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                $criteria = new \Criteria('conf_modid', /** @scrutinizer ignore-type */ $mid);
Loading history...
33
                $configs  = $configHandler->getConfigs($criteria);
34
                /** @var \XoopsConfigItem $obj */
35
                foreach ($configs as $obj) {
36
                    $moduleConfig[$obj->getVar('conf_name')] = $obj->getConfValueForOutput();
37
                }
38
                unset($configs, $criteria);
39
            }
40
            if (\is_file($helper->path('include/plugin.php'))) {
41
                $customConfig = require $helper->path('include/plugin.php');
42
                $moduleConfig = \array_merge($moduleConfig, $customConfig);
43
            }
44
        }
45
46
        return $moduleConfig;
47
    }
48
49
    /**
50
     * Define URL_DELIMITER Constant
51
     *
52
     * return void
53
     */
54
    public static function tag_define_url_delimiter(): void
55
    {
56
        if (\defined('URL_DELIMITER')) {
57
            if (!\in_array(URL_DELIMITER, ['?', '/'], true)) {
58
                exit('Security Violation');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
59
            }
60
        } else {
61
            $moduleConfig = self::tag_load_config();
62
            if (empty($moduleConfig['do_urw'])) {
63
                \define('URL_DELIMITER', '?');
64
            } else {
65
                \define('URL_DELIMITER', '/');
66
            }
67
        }
68
    }
69
70
    /**
71
     * Get the tag delimiter
72
     *
73
     * @return array|mixed
74
     */
75
    public static function tag_get_delimiter()
76
    {
77
        \XoopsModules\Tag\Helper::getInstance()->loadLanguage('config');
78
        //xoops_loadLanguage('config', 'tag');
79
        $retVal = [',', ' ', '|', ';'];
80
81
        if (!empty($GLOBALS['tag_delimiter'])) {
82
            $retVal = $GLOBALS['tag_delimiter'];
83
        } else {
84
            $moduleConfig = self::tag_load_config();
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleConfig is dead and can be removed.
Loading history...
85
            if (!empty($GLOBALS['moduleConfig']['tag_delimiter'])) {
86
                $retVal = $GLOBALS['moduleConfig']['tag_delimiter'];
87
            }
88
        }
89
90
        return $retVal;
91
    }
92
93
    /**
94
     * Function to parse arguments for a page according to $_SERVER['REQUEST_URI']
95
     *
96
     *
97
     * @param mixed $args        array of indexed variables: name and value pass-by-reference
98
     * @param mixed $args_string array of string variable values pass-by-reference
99
     * @return bool true on args parsed
100
     */
101
102
    /* known issues:
103
     * - "/" in a string
104
     * - "&" in a string
105
     */
106
    public static function tag_parse_args(&$args, &$args_string): bool
107
    {
108
        $args_abb    = [
109
            'c' => 'catid',
110
            'm' => 'modid',
111
            's' => 'start',
112
            't' => 'tag',
113
        ];
114
        $args        = [];
115
        $args_string = [];
116
        if (\preg_match('/[^\?]*\.php[\/|\?]([^\?]*)/i', $_SERVER['REQUEST_URI'], $matches)) {
117
            $vars = \preg_split('/[\/|&]/', $matches[1]);
118
            $vars = \array_map('\trim', $vars);
119
            foreach ($vars as $var) {
120
                if (\is_numeric($var)) {
121
                    $args_string[] = $var;
122
                } elseif (false !== mb_strpos($var, '=')) {
123
                    \parse_str($var, $args);
124
                } elseif (\is_numeric(mb_substr($var, 1))) {
125
                    $args[$args_abb[mb_strtolower($var[0])]] = (int)mb_substr($var, 1);
126
                } else {
127
                    $args_string[] = \urldecode($var);
128
                }
129
            }
130
        }
131
132
        return !((0 == \count($args) + \count($args_string)));
133
    }
134
135
    /**
136
     * Function to parse tags(keywords) upon defined delimiters
137
     *
138
     * @param string $text_tag text to be parsed
139
     *
140
     * @return array containing parsed tags
141
     */
142
    public static function tag_parse_tag(string $text_tag): array
143
    {
144
        $tags = [];
145
        if (!empty($text_tag)) {
146
            $delimiters = self::tag_get_delimiter();
147
            $tags_raw   = \explode(',', \str_replace($delimiters, ',', $text_tag));
148
            $tags       = \array_filter(\array_map('\trim', $tags_raw)); // removes all array elements === false
149
        }
150
151
        return $tags;
152
    }
153
}
154