Completed
Push — master ( 53ea58...16270d )
by Michael
04:51
created

items_spot.php ➔ publisher_items_spot_show()   D

Complexity

Conditions 17
Paths 40

Size

Total Lines 84
Code Lines 66

Duplication

Lines 15
Ratio 17.86 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 17
eloc 66
c 2
b 0
f 0
nc 40
nop 1
dl 15
loc 84
rs 4.8425

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
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 29 and the first side effect is on line 22.

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
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
13
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
14
 * @package         Publisher
15
 * @subpackage      Blocks
16
 * @since           1.0
17
 * @author          trabis <[email protected]>
18
 * @author          The SmartFactory <www.smartfactory.ca>
19
 */
20
// 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...
21
22
include_once dirname(__DIR__) . '/include/common.php';
23
24
/**
25
 * @param $options
26
 *
27
 * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
28
 */
29
function publisher_items_spot_show($options)
0 ignored issues
show
Coding Style introduced by
publisher_items_spot_show uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
{
31
    //    global $xoTheme;
32
    $publisher         = PublisherPublisher::getInstance();
33
    $optDisplayLast    = $options[0];
34
    $optItemsCount     = $options[1];
35
    $optCategoryId     = $options[2];
36
    $selItems          = isset($options[3]) ? explode(',', $options[3]) : '';
37
    $optDisplayPoster  = $options[4];
38
    $optDisplayComment = $options[5];
39
    $optDisplayType    = $options[6];
40
    $optTruncate       = (int)$options[7];
41
    $optCatImage       = $options[8];
42
    if ($optCategoryId == 0) {
43
        $optCategoryId = -1;
44
    }
45
    $block = array();
46
    if ($optDisplayLast == 1) {
47
        $itemsObj   = $publisher->getHandler('item')->getAllPublished($optItemsCount, 0, $optCategoryId, $sort = 'datesub', $order = 'DESC', 'summary');
48
        $i          = 1;
49
        $itemsCount = count($itemsObj);
50
        if ($itemsObj) {
51
            if ($optCategoryId != -1 && $optCatImage) {
52
                $cat                     = $publisher->getHandler('category')->get($optCategoryId);
53
                $category['name']        = $cat->name();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$category was never initialized. Although not strictly required by PHP, it is generally a good practice to add $category = 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...
54
                $category['categoryurl'] = $cat->getCategoryUrl();
55 View Code Duplication
                if ($cat->getImage() !== 'blank.png') {
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...
56
                    $category['image_path'] = publisherGetImageDir('category', false) . $cat->getImage();
57
                } else {
58
                    $category['image_path'] = '';
59
                }
60
                $block['category'] = $category;
61
            }
62
            foreach ($itemsObj as $key => $thisitem) {
63
                $item = $thisitem->toArraySimple('default', 0, $optTruncate);
64 View Code Duplication
                if ($i < $itemsCount) {
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...
65
                    $item['showline'] = true;
66
                } else {
67
                    $item['showline'] = false;
68
                }
69
                if ($optTruncate > 0) {
70
                    $block['truncate'] = true;
71
                }
72
                $block['items'][] = $item;
73
                ++$i;
74
            }
75
        }
76
    } else {
77
        $i          = 1;
78
        $itemsCount = count($selItems);
79
        foreach ($selItems as $itemId) {
0 ignored issues
show
Bug introduced by
The expression $selItems of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
            $itemObj = $publisher->getHandler('item')->get($itemId);
81
            if (!$itemObj->notLoaded()) {
82
                $item             = $itemObj->toArraySimple();
83
                $item['who_when'] = sprintf(_MB_PUBLISHER_WHO_WHEN, $itemObj->posterName(), $itemObj->getDatesub());
84 View Code Duplication
                if ($i < $itemsCount) {
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...
85
                    $item['showline'] = true;
86
                } else {
87
                    $item['showline'] = false;
88
                }
89
                if ($optTruncate > 0) {
90
                    $block['truncate'] = true;
91
                    $item['summary']   = publisherTruncateTagSafe($item['summary'], $optTruncate);
92
                }
93
                $block['items'][] = $item;
94
                ++$i;
95
            }
96
        }
97
    }
98
    if (!isset($block['items']) || count($block['items']) == 0) {
99
        return false;
100
    }
101
    $block['lang_reads']           = _MB_PUBLISHER_READS;
102
    $block['lang_comments']        = _MB_PUBLISHER_COMMENTS;
103
    $block['lang_readmore']        = _MB_PUBLISHER_READMORE;
104
    $block['display_whowhen_link'] = $optDisplayPoster;
105
    $block['display_comment_link'] = $optDisplayComment;
106
    $block['display_type']         = $optDisplayType;
107
108
    $block['moduleUrl'] = PUBLISHER_URL;
109
    $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/' . PUBLISHER_DIRNAME . '/assets/css/publisher.css');
110
111
    return $block;
112
}
113
114
/**
115
 * @param $options
116
 *
117
 * @return string
118
 */
119
function publisher_items_spot_edit($options)
120
{
121
    include_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
122
    xoops_load('XoopsFormLoader');
123
    $form      = new PublisherBlockForm();
124
    $autoEle   = new XoopsFormRadioYN(_MB_PUBLISHER_AUTO_LAST_ITEMS, 'options[0]', $options[0]);
125
    $countEle  = new XoopsFormText(_MB_PUBLISHER_LAST_ITEMS_COUNT, 'options[1]', 2, 255, $options[1]);
126
    $catEle    = new XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, publisherCreateCategorySelect($options[2], 0, true, 'options[2]'));
127
    $publisher = PublisherPublisher::getInstance();
128
    $criteria  = new CriteriaCompo();
129
    $criteria->setSort('datesub');
130
    $criteria->setOrder('DESC');
131
    $itemsObj = $publisher->getHandler('item')->getList($criteria);
132
    $keys     = array_keys($itemsObj);
133
    unset($criteria);
134
    if (empty($options[3]) || ($options[3] == 0)) {
135
        $selItems = isset($keys[0]) ? $keys[0] : 0;
136
    } else {
137
        $selItems = explode(',', $options[3]);
138
    }
139
    $itemEle = new XoopsFormSelect(_MB_PUBLISHER_SELECT_ITEMS, 'options[3]', $selItems, 10, true);
140
    $itemEle->addOptionArray($itemsObj);
141
    $whoEle  = new XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_WHO_AND_WHEN, 'options[4]', $options[4]);
142
    $comEle  = new XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_COMMENTS, 'options[5]', $options[5]);
143
    $typeEle = new XoopsFormSelect(_MB_PUBLISHER_DISPLAY_TYPE, 'options[6]', $options[6]);
144
    $typeEle->addOptionArray(array(
145
                                 'block'  => _MB_PUBLISHER_DISPLAY_TYPE_BLOCK,
146
                                 'bullet' => _MB_PUBLISHER_DISPLAY_TYPE_BULLET));
147
    $truncateEle = new XoopsFormText(_MB_PUBLISHER_TRUNCATE, 'options[7]', 4, 255, $options[7]);
148
    $imageEle    = new XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_CATIMAGE, 'options[8]', $options[8]);
149
    $form->addElement($autoEle);
150
    $form->addElement($countEle);
151
    $form->addElement($catEle);
152
    $form->addElement($itemEle);
153
    $form->addElement($whoEle);
154
    $form->addElement($comEle);
155
    $form->addElement($typeEle);
156
    $form->addElement($truncateEle);
157
    $form->addElement($imageEle);
158
159
    return $form->render();
160
}
161