Completed
Push — master ( 658b35...b781ca )
by Richard
28s queued 23s
created

publisher_items_recent_show()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 36
nc 8
nop 1
dl 0
loc 53
rs 9.0328
c 0
b 0
f 0

How to fix   Long Method   

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
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
14
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package         Publisher
16
 * @subpackage      Blocks
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 * @author          The SmartFactory <www.smartfactory.ca>
20
 * @version         $Id$
21
 */
22
23
use Xoops\Core\Text\Sanitizer;
24
use Xoops\Form\BlockForm;
25
use Xoops\Form\Label;
26
use Xoops\Form\Select;
27
use Xoops\Form\Text;
28
use XoopsModules\Publisher;
29
use XoopsModules\Publisher\Helper;
30
31
require_once dirname(__DIR__) . '/include/common.php';
32
33
/**
34
 * @param $options
35
 * @return array
36
 */
37
function publisher_items_recent_show($options)
38
{
39
    $helper = Helper::getInstance();
40
    $myts   = Sanitizer::getInstance();
41
42
    $block = [];
43
44
    $selectedcatids = explode(',', $options[0]);
45
46
    if (in_array(0, $selectedcatids)) {
47
        $allcats = true;
48
    } else {
49
        $allcats = false;
50
    }
51
52
    $sort  = $options[1];
53
    $order = Publisher\Utils::getOrderBy($sort);
54
    $limit = $options[2];
55
    $start = 0;
56
57
    // creating the ITEM objects that belong to the selected category
58
    if ($allcats) {
59
        $criteria = null;
60
    } else {
61
        $criteria = new CriteriaCompo();
62
        $criteria->add(new Criteria('categoryid', '(' . $options[0] . ')', 'IN'));
63
    }
64
    $itemsObj = $helper->getItemHandler()->getItems($limit, $start, [_PUBLISHER_STATUS_PUBLISHED], -1, $sort, $order, '', true, $criteria, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $id_key of XoopsModules\Publisher\ItemHandler::getItems(). ( Ignorable by Annotation )

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

64
    $itemsObj = $helper->getItemHandler()->getItems($limit, $start, [_PUBLISHER_STATUS_PUBLISHED], -1, $sort, $order, '', true, $criteria, /** @scrutinizer ignore-type */ true);
Loading history...
Bug introduced by
It seems like $criteria can also be of type CriteriaCompo; however, parameter $otherCriteria of XoopsModules\Publisher\ItemHandler::getItems() does only seem to accept null, 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

64
    $itemsObj = $helper->getItemHandler()->getItems($limit, $start, [_PUBLISHER_STATUS_PUBLISHED], -1, $sort, $order, '', true, /** @scrutinizer ignore-type */ $criteria, true);
Loading history...
Bug introduced by
array(_PUBLISHER_STATUS_PUBLISHED) of type array<integer,integer> is incompatible with the type string expected by parameter $status of XoopsModules\Publisher\ItemHandler::getItems(). ( Ignorable by Annotation )

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

64
    $itemsObj = $helper->getItemHandler()->getItems($limit, $start, /** @scrutinizer ignore-type */ [_PUBLISHER_STATUS_PUBLISHED], -1, $sort, $order, '', true, $criteria, true);
Loading history...
65
66
    if ($itemsObj) {
67
        foreach ($itemsObj as $iValue) {
68
69
            $newItems['itemid']       = $iValue->getVar('itemid');
70
            $newItems['title']        = $iValue->title();
71
            $newItems['categoryname'] = $iValue->getCategoryName();
72
            $newItems['categoryid']   = $iValue->getVar('categoryid');
73
            $newItems['date']         = $iValue->datesub();
74
            $newItems['poster']       = $iValue->linkedPosterName();
75
            $newItems['itemlink']     = $iValue->getItemLink(false, $options[3] ?? 65);
76
            $newItems['categorylink'] = $iValue->getCategoryLink();
77
78
            $block['items'][] = $newItems;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newItems seems to be defined later in this foreach loop on line 69. Are you sure it is defined here?
Loading history...
79
        }
80
81
        $block['lang_title']     = _MB_PUBLISHER_ITEMS;
82
        $block['lang_category']  = _MB_PUBLISHER_CATEGORY;
83
        $block['lang_poster']    = _MB_PUBLISHER_POSTEDBY;
84
        $block['lang_date']      = _MB_PUBLISHER_DATE;
85
        $modulename              = $myts->displayTarea($helper->getModule()->getVar('name'));
0 ignored issues
show
Bug introduced by
It seems like $helper->getModule()->getVar('name') can also be of type string[]; however, parameter $text of Xoops\Core\Text\Sanitizer::displayTarea() 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

85
        $modulename              = $myts->displayTarea(/** @scrutinizer ignore-type */ $helper->getModule()->getVar('name'));
Loading history...
86
        $block['lang_visitItem'] = _MB_PUBLISHER_VISITITEM . ' ' . $modulename;
87
    }
88
89
    return $block;
90
}
91
92
/**
93
 * @param $options
94
 * @return string
95
 */
96
function publisher_items_recent_edit($options)
97
{
98
    $form = new BlockForm();
99
100
    $catEle   = new Label(_MB_PUBLISHER_SELECTCAT, Publisher\Utils::createCategorySelect($options[0], 0, true, 'options[0]'));
101
    $orderEle = new Select(_MB_PUBLISHER_ORDER, 'options[1]', $options[1]);
102
    $orderEle->addOptionArray([
103
                                  'datesub' => _MB_PUBLISHER_DATE,
104
                                  'counter' => _MB_PUBLISHER_HITS,
105
                                  'weight'  => _MB_PUBLISHER_WEIGHT,
106
                              ]);
107
    $dispEle  = new Text(_MB_PUBLISHER_DISP, 'options[2]', 2, 255, $options[2]);
108
    $charsEle = new Text(_MB_PUBLISHER_CHARS, 'options[3]', 2, 255, $options[3]);
109
110
    $form->addElement($catEle);
111
    $form->addElement($orderEle);
112
    $form->addElement($dispEle);
113
    $form->addElement($charsEle);
114
115
    return $form->render();
116
}
117