wfdownloads_top_by_cat_show()   B
last analyzed

Complexity

Conditions 10
Paths 33

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 36
nc 33
nop 1
dl 0
loc 56
rs 7.6666
c 0
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
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
 * Wfdownloads module
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package         wfdownload
18
 * @since           3.23
19
 * @author          Xoops Development Team
20
 */
21
22
/**
23
 * Function: b_mydownloads_top_by_cat_show
24
 * Input   : $options[0] = date for the most recent downloads
25
 *                     hits for the most popular downloads
26
 *            $block['content'] = The optional above content
27
 *            $options[1]   = How many downloads are displayes
28
 * Output  : Returns the most recent or most popular downloads
29
 */
30
31
use XoopsModules\Wfdownloads;
32
use XoopsModules\Wfdownloads\Helper;
33
34
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
35
36
require_once dirname(__DIR__) . '/include/common.php';
37
/**
38
 * @param $options
39
 *
40
 * @return array
41
 */
42
function wfdownloads_top_by_cat_show($options)
43
{
44
    if (!class_exists(Helper::class)) {
45
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
46
    }
47
    $helper = Helper::getInstance();
48
    $categoryHandler = new Wfdownloads\CategoryHandler($GLOBALS['xoopsDB']);
49
50
    /** @var \XoopsGroupPermHandler $grouppermHandler */
51
    $grouppermHandler         = xoops_getHandler('groupperm');
52
    $groups                   = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
53
    $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $helper->getModule()->mid());
54
55
    $block = [];
56
57
    // get downloads
58
    $criteria = new CriteriaCompo();
59
    $criteria->add(new Criteria('cid', '(' . implode(',', $allowedDownCategoriesIds) . ')', 'IN'));
60
    $criteria->add(new Criteria('offline', false));
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $value of Criteria::__construct(). ( Ignorable by Annotation )

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

60
    $criteria->add(new Criteria('offline', /** @scrutinizer ignore-type */ false));
Loading history...
61
    $criteria->setSort('date');
62
    $criteria->setOrder('DESC');
63
    $criteria->setLimit($options[1]);
64
    $downloadObjs = $helper->getHandler('Download')->getObjects($criteria);
65
66
    foreach ($downloadObjs as $downloadObj) {
67
        $download = $downloadObj->toArray();
68
        if (!in_array((int)$download['cid'], $allowedDownCategoriesIds)) {
69
            continue;
70
        }
71
        $download['title'] = xoops_substr($download['title'], 0, $options[2] - 1);
72
        $download['id']    = (int)$download['lid'];
73
        if ('published' === $options[0]) {
74
            $download['date'] = formatTimestamp($download['published'], $helper->getConfig('dateformat'));
75
        } else {
76
            $download['date'] = formatTimestamp($download['date'], $helper->getConfig('dateformat'));
77
        }
78
        $download['dirname']  = $helper->getModule()->dirname();
79
        $block['downloads'][] = $download;
80
    }
81
82
    $categoriesTopParentByCid = $helper->getHandler('Category')->getAllSubcatsTopParentCid();
83
84
    //    foreach ($helper->getHandler('Category')->topCategories as $cid) {
85
    if ($categoryHandler->topCategories && is_array($categoryHandler->topCategories)) {
0 ignored issues
show
introduced by
The condition is_array($categoryHandler->topCategories) is always false.
Loading history...
86
        foreach ($categoryHandler->topCategories as $cid) {
87
            $block['topcats'][$cid]['title']  = $helper->getHandler('Category')->allCategories[$cid]->getVar('title');
88
            $block['topcats'][$cid]['cid']    = $cid;
89
            $block['topcats'][$cid]['imgurl'] = $helper->getHandler('Category')->allCategories[$cid]->getVar('imgurl');
90
        }
91
    }
92
93
    foreach ($block['downloads'] as $value) {
94
        $block['topcats'][$categoriesTopParentByCid[$value['cid']]]['downloads'][] = $value;
95
    }
96
97
    return $block;
98
}
99
100
/**
101
 * @param $options
102
 *
103
 * @return string
104
 */
105
function wfdownloads_top_by_cat_edit($options)
106
{
107
    $form = '' . _MB_WFDOWNLOADS_DISP . '&nbsp;';
108
    $form .= "<input type='hidden' name='options[]' value='" . (('published' === $options[0]) ? 'published' : 'hits') . "'>";
109
    $form .= "<input type='text' name='options[]' value='" . $options[1] . "'>&nbsp;" . _MB_WFDOWNLOADS_FILES . '';
110
    $form .= '<br>';
111
    $form .= '' . _MB_WFDOWNLOADS_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "'>&nbsp;" . _MB_WFDOWNLOADS_LENGTH . '';
112
113
    return $form;
114
}
115