mymodule3_RewriteUrl()   F
last analyzed

Complexity

Conditions 21
Paths 140

Size

Total Lines 80
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 21
eloc 57
nc 140
nop 3
dl 0
loc 80
rs 3.8333
c 1
b 1
f 1

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
 * My Module 3 module for xoops
14
 *
15
 * @copyright     2020 XOOPS Project (https://xooops.org)
16
 * @license        GPL 2.0 or later
17
 * @package        mymodule3
18
 * @since          1.0
19
 * @min_xoops      2.5.9
20
 * @author         TDM XOOPS - Email:<[email protected]> - Website:<http://xoops.org>
21
 */
22
23
/**
24
 * function add selected cats to block
25
 *
26
 * @param  $cats 
27
 * @return string
28
 */
29
function mymodule3_block_addCatSelect($cats)
30
{
31
	$cat_sql = '(';
32
	if (is_array($cats)) {
33
		$cat_sql .= current($cats);
34
		array_shift($cats);
35
		foreach($cats as $cat) {
36
			$cat_sql .= ',' . $cat;
37
		}
38
	}
39
	$cat_sql .= ')';
40
	return $cat_sql;
41
}
42
43
/**
44
 * Get the permissions ids 
45
 *
46
 * @param  $permtype 
47
 * @param  $dirname 
48
 * @return mixed $itemIds
49
 */
50
function mymodule3GetMyItemIds($permtype, $dirname)
51
{
52
	global $xoopsUser;
53
	static $permissions = [];
54
	if (is_array($permissions) && array_key_exists($permtype, $permissions)) {
55
		return $permissions[$permtype];
56
	}
57
	$moduleHandler = xoops_getHandler('module');
58
	$mymodule3Module = $moduleHandler->getByDirname($dirname);
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

58
	/** @scrutinizer ignore-call */ 
59
 $mymodule3Module = $moduleHandler->getByDirname($dirname);
Loading history...
59
	$groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
60
	$grouppermHandler = xoops_getHandler('groupperm');
61
	$itemIds = $grouppermHandler->getItemIds($permtype, $groups, $mymodule3Module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getItemIds() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

61
	/** @scrutinizer ignore-call */ 
62
 $itemIds = $grouppermHandler->getItemIds($permtype, $groups, $mymodule3Module->getVar('mid'));
Loading history...
62
	return $itemIds;
63
}
64
65
/**
66
 * Get the number of testfields from the sub categories of a category or sub topics of or topic
67
 * @param $mytree
68
 * @param $testfields
69
 * @param $entries
70
 * @param $cid
71
 * @return int
72
 */
73
function mymodule3NumbersOfEntries($mytree, $testfields, $entries, $cid)
74
{
75
    $count = 0;
76
    if(in_array($cid, $testfields)) {
77
        $child = $mytree->getAllChild($cid);
78
        foreach (array_keys($entries) as $i) {
79
            if ($entries[$i]->getVar('tf_id') == $cid){
80
                $count++;
81
            }
82
            foreach (array_keys($child) as $j) {
83
                if ($entries[$i]->getVar('tf_id') == $j){
84
                    $count++;
85
                }
86
            }
87
        }
88
    }
89
    return $count;
90
}
91
92
/**
93
 * Add content as meta tag to template
94
 * @param $content
95
 * @return void
96
 */
97
98
function mymodule3MetaKeywords($content)
99
{
100
    global $xoopsTpl, $xoTheme;
101
    $myts = MyTextSanitizer::getInstance();
102
    $content= $myts->undoHtmlSpecialChars($myts->displayTarea($content));
103
    if(isset($xoTheme) && is_object($xoTheme)) {
104
        $xoTheme->addMeta( 'meta', 'keywords', strip_tags($content));
105
    } else {    // Compatibility for old Xoops versions
106
        $xoopsTpl->assign('xoops_meta_keywords', strip_tags($content));
107
    }
108
}
109
110
/**
111
 * Add content as meta description to template
112
 * @param $content
113
 * @return void
114
 */
115
 
116
function mymodule3MetaDescription($content)
117
{
118
    global $xoopsTpl, $xoTheme;
119
    $myts = MyTextSanitizer::getInstance();
120
    $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content));
121
    if(isset($xoTheme) && is_object($xoTheme)) {
122
        $xoTheme->addMeta( 'meta', 'description', strip_tags($content));
123
    } else {    // Compatibility for old Xoops versions
124
        $xoopsTpl->assign('xoops_meta_description', strip_tags($content));
125
    }
126
}
127
128
/**
129
 * Rewrite all url
130
 *
131
 * @param string  $module  module name
132
 * @param array   $array   array
133
 * @param string  $type    type
134
 * @return null|string $type    string replacement for any blank case
135
 */
136
function mymodule3_RewriteUrl($module, $array, $type = 'content')
137
{
138
    $comment = '';
139
    $helper = \XoopsModules\Mymodule3\Helper::getInstance();
140
    $testfieldsHandler = $helper->getHandler('testfields');
0 ignored issues
show
Unused Code introduced by
The assignment to $testfieldsHandler is dead and can be removed.
Loading history...
141
    $lenght_id = $helper->getConfig('lenght_id');
142
    $rewrite_url = $helper->getConfig('rewrite_url');
143
144
    if ($lenght_id != 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $lenght_id of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
145
        $id = $array['content_id'];
146
        while (strlen($id) < $lenght_id) {
147
            $id = '0' . $id;
148
        }
149
    } else {
150
        $id = $array['content_id'];
151
    }
152
153
    if (isset($array['topic_alias']) && $array['topic_alias']) {
154
        $topic_name = $array['topic_alias'];
155
    } else {
156
        $topic_name = mymodule3_Filter(xoops_getModuleOption('static_name', $module));
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

156
        $topic_name = mymodule3_Filter(/** @scrutinizer ignore-deprecated */ xoops_getModuleOption('static_name', $module));
Loading history...
157
    }
158
159
    switch ($rewrite_url) {
160
161
        case 'none':
162
            if($topic_name) {
163
                 $topic_name = 'topic=' . $topic_name . '&amp;';
164
            }
165
            $rewrite_base = '/modules/';
166
            $page = 'page=' . $array['content_alias'];
167
            return XOOPS_URL . $rewrite_base . $module . '/' . $type . '.php?' . $topic_name . 'id=' . $id . '&amp;' . $page . $comment;
168
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
169
170
        case 'rewrite':
171
            if($topic_name) {
172
                $topic_name .= '/';
173
            }
174
            $rewrite_base = xoops_getModuleOption('rewrite_mode', $module);
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

174
            $rewrite_base = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_mode', $module);
Loading history...
175
            $rewrite_ext = xoops_getModuleOption('rewrite_ext', $module);
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

175
            $rewrite_ext = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_ext', $module);
Loading history...
176
            $module_name = '';
177
            if(xoops_getModuleOption('rewrite_name', $module)) {
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

177
            if(/** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_name', $module)) {
Loading history...
178
                $module_name = xoops_getModuleOption('rewrite_name', $module) . '/';
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

178
                $module_name = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_name', $module) . '/';
Loading history...
179
            }
180
            $page = $array['content_alias'];
181
            $type .= '/';
182
            $id .= '/';
183
            if ($type === 'content/') {
184
                $type = '';
185
            }
186
            if ($type === 'comment-edit/' || $type === 'comment-reply/' || $type === 'comment-delete/') {
187
                return XOOPS_URL . $rewrite_base . $module_name . $type . $id . '/';
188
            }
189
190
            return XOOPS_URL . $rewrite_base . $module_name . $type . $topic_name  . $id . $page . $rewrite_ext;
191
            break;
192
193
         case 'short':
194
            if($topic_name) {
195
                $topic_name .= '/';
196
            }
197
            $rewrite_base = xoops_getModuleOption('rewrite_mode', $module);
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

197
            $rewrite_base = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_mode', $module);
Loading history...
198
            $rewrite_ext = xoops_getModuleOption('rewrite_ext', $module);
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

198
            $rewrite_ext = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_ext', $module);
Loading history...
199
            $module_name = '';
200
            if(xoops_getModuleOption('rewrite_name', $module)) {
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

200
            if(/** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_name', $module)) {
Loading history...
201
                $module_name = xoops_getModuleOption('rewrite_name', $module) . '/';
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

201
                $module_name = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('rewrite_name', $module) . '/';
Loading history...
202
            }
203
            $page = $array['content_alias'];
204
            $type .= '/';
205
            if ($type === 'content/') {
206
                $type = '';
207
            }
208
            if ($type === 'comment-edit/' || $type === 'comment-reply/' || $type === 'comment-delete/') {
209
                return XOOPS_URL . $rewrite_base . $module_name . $type . $id . '/';
210
            }
211
212
            return XOOPS_URL . $rewrite_base . $module_name . $type . $topic_name . $page . $rewrite_ext;
213
            break;
214
    }
215
    return null;
216
}
217
/**
218
 * Replace all escape, character, ... for display a correct url
219
 *
220
 * @param string $url      string to transform
221
 * @param string $type     string replacement for any blank case
222
 * @return string $url
223
 */
224
function mymodule3_Filter($url, $type = '') {
225
226
    // Get regular expression from module setting. default setting is : `[^a-z0-9]`i
227
    $helper = \XoopsModules\Mymodule3\Helper::getInstance();
228
    $testfieldsHandler = $helper->getHandler('testfields');
0 ignored issues
show
Unused Code introduced by
The assignment to $testfieldsHandler is dead and can be removed.
Loading history...
229
    $regular_expression = $helper->getConfig('regular_expression');
230
231
    $url = strip_tags($url);
232
    $url .= preg_replace("`\[.*\]`U", '', $url);
233
    $url .= preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $url);
234
    $url .= htmlentities($url, ENT_COMPAT, 'utf-8');
235
    $url .= preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i", "\1", $url);
236
    $url .= preg_replace(array($regular_expression, "`[-]+`"), '-', $url);
237
    $url = ($url == '') ? $type : strtolower(trim($url, '-'));
238
    return $url;
239
}