Passed
Pull Request — master (#88)
by Michael
02:56
created

Utility::getPathTreeUrl()   B

Complexity

Conditions 10
Paths 60

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 34
nc 60
nop 8
dl 0
loc 57
rs 7.6666
c 1
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads;
4
5
/*
6
 Utility Class Definition
7
8
 You may not change or alter any portion of this comment or credits of
9
 supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit
11
 authors.
12
13
 This program is distributed in the hope that it will be useful, but
14
 WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
18
/**
19
 * Class Utility
20
 */
21
class Utility extends Common\SysUtility
22
{
23
    //--------------- Custom module methods -----------------------------
24
    /**
25
     * @param $permtype
26
     * @param $dirname
27
     * @return mixed
28
     */
29
    public function getItemIds($permtype, $dirname)
30
    {
31
        global $xoopsUser;
32
33
        static $permissions = [];
34
35
        if (\is_array($permissions) && \array_key_exists($permtype, $permissions)) {
36
            return $permissions[$permtype];
37
        }
38
39
        /** @var \XoopsModuleHandler $moduleHandler */
40
41
        $moduleHandler = \xoops_getHandler('module');
42
43
        $tdmModule = $moduleHandler->getByDirname($dirname);
44
45
        $groups = \is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
46
47
        /** @var \XoopsGroupPermHandler $grouppermHandler */
48
        $grouppermHandler = \xoops_getHandler('groupperm');
49
50
        return $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid'));
51
    }
52
53
    /**
54
     * retourne le nombre de téléchargements dans le catégories enfants d'une catégorie
55
     * @param \XoopsModules\Tdmdownloads\Tree        $mytree
56
     * @param                                        $categories
57
     * @param                                        $entries
58
     * @param                                        $cid
59
     * @return int
60
     */
61
    public function getNumbersOfEntries($mytree, $categories, $entries, $cid)
62
    {
63
        $count = 0;
64
65
        $child_arr = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $child_arr is dead and can be removed.
Loading history...
66
67
        if (\in_array($cid, $categories)) {
68
            $child = $mytree->getAllChild($cid);
69
70
            foreach (\array_keys($entries) as $i) {
71
                /** @var \XoopsModules\Tdmdownloads\Downloads[] $entries */
72
73
                if ($entries[$i]->getVar('cid') == $cid) {
74
                    $count++;
75
                }
76
77
                foreach (\array_keys($child) as $j) {
78
                    if ($entries[$i]->getVar('cid') == $j) {
79
                        $count++;
80
                    }
81
                }
82
            }
83
        }
84
85
        return $count;
86
    }
87
88
    /**
89
     * retourne une image "nouveau" ou "mise à jour"
90
     * @param $time
91
     * @param $status
92
     * @return string
93
     */
94
    public function getStatusImage($time, $status)
95
    {
96
        global $xoopsModuleConfig;
97
98
        $count = 7;
99
100
        $new = '';
101
102
        $startdate = \time() - (86400 * $count);
103
104
        if (1 == $xoopsModuleConfig['showupdated']) {
105
            if ($startdate < $time) {
106
                $language = $GLOBALS['xoopsConfig']['language'];
107
108
                if (!\is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) {
109
                    $language = 'english';
110
                }
111
112
                $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/';
113
114
                $img_url = XOOPS_URL . '/modules/tdmdownloads/language/' . $language . '/';
115
116
                if (1 == $status) {
117
                    if (\is_readable($img_path . 'new.png')) {
118
                        $new = '&nbsp;<img src="' . $img_url . 'new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">';
119
                    } else {
120
                        $new = '&nbsp;<img src="' . XOOPS_URL . '/modules/tdmdownloads/language/english/new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">';
121
                    }
122
                } elseif (2 == $status) {
123
                    if (\is_readable($img_path . 'updated.png')) {
124
                        $new = '&nbsp;<img src="' . $img_url . 'updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
125
                    } else {
126
                        $new = '&nbsp;<img src="' . XOOPS_URL . '/modules/tdmdownloads/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
127
                    }
128
                }
129
            }
130
        }
131
132
        return $new;
133
    }
134
135
    /**
136
     * retourne une image "populaire"
137
     * @param $hits
138
     * @return string
139
     */
140
    public function getPopularImage($hits)
141
    {
142
        global $xoopsModuleConfig;
143
144
        $pop = '';
145
146
        if ($hits >= $xoopsModuleConfig['popular']) {
147
            $language = $GLOBALS['xoopsConfig']['language'];
148
149
            if (!\is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) {
150
                $language = 'english';
151
            }
152
153
            $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/';
154
155
            $img_url = XOOPS_URL . '/modules/tdmdownloads/language/' . $language . '/';
156
157
            if (\is_readable($img_path . 'popular.png')) {
158
                $pop = '&nbsp;<img src="' . $img_url . 'popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
159
            } else {
160
                $pop = '&nbsp;<img src ="' . XOOPS_URL . '/modules/tdmdownloads/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
161
            }
162
        }
163
164
        return $pop;
165
    }
166
167
    /**
168
     * @param int $size
169
     * @return string
170
     */
171
    public static function prettifyBytes($size)
172
    {
173
        if ($size > 0) {
174
            $mb = 1024 * 1024;
175
176
            if ($size > $mb) {
177
                $mysize = \sprintf('%01.2f', $size / $mb) . ' MB';
178
            } elseif ($size >= 1024) {
179
                $mysize = \sprintf('%01.2f', $size / 1024) . ' KB';
180
            } else {
181
                $mysize = \sprintf(_AM_TDMDOWNLOADS_NUMBYTES, $size);
182
            }
183
184
            return $mysize;
185
        }
186
187
        return '';
188
    }
189
190
    /**
191
     * @param        $global
192
     * @param        $key
193
     * @param string $default
194
     * @param string $type
195
     * @return mixed|string
196
     */
197
    public static function cleanVars($global, $key, $default = '', $type = 'int')
198
    {
199
        switch ($type) {
200
            case 'string':
201
                $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_MAGIC_QUOTES) : $default;
0 ignored issues
show
introduced by
The constant FILTER_SANITIZE_MAGIC_QUOTES has been deprecated: 7.4 ( Ignorable by Annotation )

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

201
                $ret = isset($global[$key]) ? \filter_var($global[$key], /** @scrutinizer ignore-deprecated */ \FILTER_SANITIZE_MAGIC_QUOTES) : $default;
Loading history...
202
                break;
203
            case 'int':
204
            default:
205
                $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_NUMBER_INT) : $default;
206
                break;
207
        }
208
209
        if (false === $ret) {
210
            return $default;
211
        }
212
213
        return $ret;
214
    }
215
216
    /**
217
     * @param        $mytree
218
     * @param        $key
219
     * @param        $category_array
220
     * @param        $title
221
     * @param string $prefix
222
     * @return string
223
     */
224
    public static function getPathTree($mytree, $key, $category_array, $title, $prefix = '')
225
    {
226
        /** @var \XoopsObjectTree $mytree */
227
228
        $categoryParent = $mytree->getAllParent($key);
229
230
        $categoryParent = \array_reverse($categoryParent);
231
232
        $path = '';
233
234
        foreach (\array_keys($categoryParent) as $j) {
235
            /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */
236
237
            $path .= $categoryParent[$j]->getVar($title) . $prefix;
238
        }
239
240
        if (\array_key_exists($key, $category_array)) {
241
            /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */
242
243
            $firstCategory = $category_array[$key]->getVar($title);
244
        } else {
245
            $firstCategory = '';
246
        }
247
248
        $path .= $firstCategory;
249
250
        return $path;
251
    }
252
253
    /**
254
     * @param \XoopsModules\Tdmdownloads\Tree $mytree
255
     * @param                                 $key
256
     * @param                                 $category_array
257
     * @param                                 $title
258
     * @param string                          $prefix
259
     * @param bool                            $link
260
     * @param string                          $order
261
     * @param bool                            $lasturl
262
     * @return string
263
     */
264
    public static function getPathTreeUrl($mytree, $key, $category_array, $title, $prefix = '', $link = false, $order = 'ASC', $lasturl = false)
265
    {
266
        global $xoopsModule;
267
268
        $categoryParent = $mytree->getAllParent($key);
269
270
        if ('ASC' === $order) {
271
            $categoryParent = \array_reverse($categoryParent);
272
273
            if ($link) {
274
                $path = '<a href="index.php">' . $xoopsModule->name() . '</a>' . $prefix;
275
            } else {
276
                $path = $xoopsModule->name() . $prefix;
277
            }
278
        } else {
279
            if (\array_key_exists($key, $category_array)) {
280
                /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */
281
282
                $firstCategory = $category_array[$key]->getVar($title);
283
            } else {
284
                $firstCategory = '';
285
            }
286
287
            $path = $firstCategory . $prefix;
288
        }
289
290
        foreach (\array_keys($categoryParent) as $j) {
291
            /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */
292
293
            if ($link) {
294
                $path .= '<a href="viewcat.php?cid=' . $categoryParent[$j]->getVar('cat_cid') . '">' . $categoryParent[$j]->getVar($title) . '</a>' . $prefix;
295
            } else {
296
                $path .= $categoryParent[$j]->getVar($title) . $prefix;
297
            }
298
        }
299
300
        if ('ASC' === $order) {
301
            if (\array_key_exists($key, $category_array)) {
302
                if ($lasturl) {
303
                    $firstCategory = '<a href="viewcat.php?cid=' . $category_array[$key]->getVar('cat_cid') . '">' . $category_array[$key]->getVar($title) . '</a>';
304
                } else {
305
                    $firstCategory = $category_array[$key]->getVar($title);
306
                }
307
            } else {
308
                $firstCategory = '';
309
            }
310
311
            $path .= $firstCategory;
312
        } else {
313
            if ($link) {
314
                $path .= '<a href="index.php">' . $xoopsModule->name() . '</a>';
315
            } else {
316
                $path .= $xoopsModule->name();
317
            }
318
        }
319
320
        return $path;
321
    }
322
323
    /**
324
     * Utility::convertStringToSize()
325
     *
326
     * @param mixed $stringSize
327
     * @return mixed|int
328
     */
329
    public static function convertStringToSize($stringSize)
330
    {
331
        if ('' != $stringSize) {
332
            $kb = 1024;
333
334
            $mb = 1024 * 1024;
335
336
            $gb = 1024 * 1024 * 1024;
337
338
            $size_value_arr = \explode(' ', $stringSize);
339
340
            if ('B' == $size_value_arr[1]) {
341
                $mysize = $size_value_arr[0];
342
            } elseif ('K' == $size_value_arr[1]) {
343
                $mysize = $size_value_arr[0] * $kb;
344
            } elseif ('M' == $size_value_arr[1]) {
345
                $mysize = $size_value_arr[0] * $mb;
346
            } else {
347
                $mysize = $size_value_arr[0] * $gb;
348
            }
349
350
            return $mysize;
351
        }
352
353
        return 0;
354
    }
355
356
    /**
357
     * Utility::convertSizeToString()
358
     *
359
     * @param mixed $sizeString
360
     * @return mixed|string
361
     */
362
    public static function convertSizeToString($sizeString)
363
    {
364
        $mysizeString = '';
365
366
        if ('' != $sizeString) {
367
            $size_value_arr = \explode(' ', $sizeString);
368
369
            if (\array_key_exists(0, $size_value_arr) && \array_key_exists(1, $size_value_arr)) {
370
                if ('' != $size_value_arr[0]) {
371
                    $mysizeString = '';
372
373
                    switch ($size_value_arr[1]) {
374
                        case 'B':
375
                            $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_BYTES;
376
                            break;
377
                        case 'K':
378
                            $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_KBYTES;
379
                            break;
380
                        case 'M':
381
                            $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_MBYTES;
382
                            break;
383
                        case 'G':
384
                            $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_GBYTES;
385
                            break;
386
                        case 'T':
387
                            $mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_TBYTES;
388
                            break;
389
                    }
390
391
                    return $mysizeString;
392
                }
393
            }
394
        }
395
396
        return $mysizeString;
397
    }
398
399
    /**
400
     * Utility::getFileSize()
401
     *
402
     * @param mixed $url
403
     * @return mixed|string
404
     */
405
    public static function getFileSize($url)
406
    {
407
        if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init($url))) {
408
            \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
409
410
            \curl_setopt($curlHandle, \CURLOPT_HEADER, true);
411
412
            \curl_setopt($curlHandle, \CURLOPT_NOBODY, true);
413
414
            \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
415
416
            $curlReturn = \curl_exec($curlHandle);
417
418
            if (false === $curlReturn) {
419
                \trigger_error(\curl_error($curlHandle));
420
421
                $size = 0;
422
            } else {
423
                $size = \curl_getinfo($curlHandle, \CURLINFO_CONTENT_LENGTH_DOWNLOAD);
424
            }
425
426
            \curl_close($curlHandle);
427
428
            if ($size <= 0) {
429
                return 0;
430
            }
431
432
            return self::convertFileSize($size);
433
        }
434
435
        return 0;
436
    }
437
438
    /**
439
     * Utility::convertFileSize()
440
     *
441
     * @param mixed $size
442
     * @return mixed|string
443
     */
444
    public static function convertFileSize($size)
445
    {
446
        if ($size > 0) {
447
            $kb = 1024;
448
449
            $mb = 1024 * 1024;
450
451
            $gb = 1024 * 1024 * 1024;
452
453
            if ($size >= $gb) {
454
                $mysize = \sprintf('%01.2f', $size / $gb) . ' ' . 'G';
455
            } elseif ($size >= $mb) {
456
                $mysize = \sprintf('%01.2f', $size / $mb) . ' ' . 'M';
457
            } elseif ($size >= $kb) {
458
                $mysize = \sprintf('%01.2f', $size / $kb) . ' ' . 'K';
459
            } else {
460
                $mysize = \sprintf('%01.2f', $size) . ' ' . 'B';
461
            }
462
463
            return $mysize;
464
        }
465
466
        return '';
467
    }
468
469
    /**
470
     * @param $val
471
     * @return float|int
472
     */
473
    public static function returnBytes($val)
474
    {
475
        switch (\mb_substr($val, -1)) {
476
            case 'K':
477
            case 'k':
478
                return (int)$val * 1024;
479
            case 'M':
480
            case 'm':
481
                return (int)$val * 1048576;
482
            case 'G':
483
            case 'g':
484
                return (int)$val * 1073741824;
485
            default:
486
                return $val;
487
        }
488
    }
489
}
490