Completed
Push — master ( 16270d...f28230 )
by Michael
03:23
created

items_recent.php ➔ publisher_items_recent_edit()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 23
Ratio 95.83 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 1
dl 23
loc 24
rs 8.9713
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @package         Publisher
16
 * @subpackage      Blocks
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 * @author          The SmartFactory <www.smartfactory.ca>
20
 */
21
22
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
24
include_once dirname(__DIR__) . '/include/common.php';
25
26
/**
27
 * @param $options
28
 *
29
 * @return array
30
 */
31
function publisher_items_recent_show($options)
32
{
33
    $publisher = PublisherPublisher::getInstance();
34
    $myts      = MyTextSanitizer::getInstance();
35
36
    $block = array();
37
38
    $selectedcatids = explode(',', $options[0]);
39
40
    $allcats = false;
41
    if (in_array(0, $selectedcatids)) {
42
        $allcats = true;
43
    }
44
45
    $sort  = $options[1];
46
    $order = publisherGetOrderBy($sort);
47
    $limit = $options[2];
48
    $start = 0;
49
50
    // creating the ITEM objects that belong to the selected category
51 View Code Duplication
    if ($allcats) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
        $criteria = null;
53
    } else {
54
        $criteria = new CriteriaCompo();
55
        $criteria->add(new Criteria('categoryid', '(' . $options[0] . ')', 'IN'));
56
    }
57
    $itemsObj = $publisher->getHandler('item')->getItems($limit, $start, array(PublisherConstants::PUBLISHER_STATUS_PUBLISHED), -1, $sort, $order, '', true, $criteria, true);
58
59
    $totalItems = count($itemsObj);
60
61
    if ($itemsObj && $totalItems > 1) {
62 View Code Duplication
        for ($i = 0; $i < $totalItems; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $newItems['itemid']       = $itemsObj[$i]->itemid();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$newItems was never initialized. Although not strictly required by PHP, it is generally a good practice to add $newItems = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
64
            $newItems['title']        = $itemsObj[$i]->getTitle();
0 ignored issues
show
Bug introduced by
The variable $newItems does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
65
            $newItems['categoryname'] = $itemsObj[$i]->getCategoryName();
66
            $newItems['categoryid']   = $itemsObj[$i]->categoryid();
67
            $newItems['date']         = $itemsObj[$i]->getDatesub();
68
            $newItems['poster']       = $itemsObj[$i]->getLinkedPosterName();
69
            $newItems['itemlink']     = $itemsObj[$i]->getItemLink(false, isset($options[3]) ? $options[3] : 65);
70
            $newItems['categorylink'] = $itemsObj[$i]->getCategoryLink();
71
72
            $block['items'][] = $newItems;
73
        }
74
75
        $block['lang_title']     = _MB_PUBLISHER_ITEMS;
76
        $block['lang_category']  = _MB_PUBLISHER_CATEGORY;
77
        $block['lang_poster']    = _MB_PUBLISHER_POSTEDBY;
78
        $block['lang_date']      = _MB_PUBLISHER_DATE;
79
        $modulename              = $myts->displayTarea($publisher->getModule()->getVar('name'));
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $publisher->getModule() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
80
        $block['lang_visitItem'] = _MB_PUBLISHER_VISITITEM . ' ' . $modulename;
81
    }
82
83
    return $block;
84
}
85
86
/**
87
 * @param $options
88
 *
89
 * @return string
90
 */
91 View Code Duplication
function publisher_items_recent_edit($options)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
{
93
    include_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
94
    xoops_load('XoopsFormLoader');
95
96
    $form = new PublisherBlockForm();
97
98
    $catEle   = new XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, publisherCreateCategorySelect($options[0], 0, true, 'options[0]'));
99
    $orderEle = new XoopsFormSelect(_MB_PUBLISHER_ORDER, 'options[1]', $options[1]);
100
    $orderEle->addOptionArray(array(
101
                                  'datesub' => _MB_PUBLISHER_DATE,
102
                                  'counter' => _MB_PUBLISHER_HITS,
103
                                  'weight'  => _MB_PUBLISHER_WEIGHT
104
                              ));
105
    $dispEle  = new XoopsFormText(_MB_PUBLISHER_DISP, 'options[2]', 10, 255, $options[2]);
106
    $charsEle = new XoopsFormText(_MB_PUBLISHER_CHARS, 'options[3]', 10, 255, $options[3]);
107
108
    $form->addElement($catEle);
109
    $form->addElement($orderEle);
110
    $form->addElement($dispEle);
111
    $form->addElement($charsEle);
112
113
    return $form->render();
114
}
115