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

items_columns.php ➔ publisher_items_columns_show()   F

Complexity

Conditions 18
Paths 592

Size

Total Lines 104
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 18
eloc 65
c 3
b 0
f 0
nc 592
nop 1
dl 0
loc 104
rs 2.4048

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 33 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          Bandit-x
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
 * Function To Show Publisher Items From Categories In Their Own Columns
28
 *
29
 * @param    array $options Block Options
30
 *
31
 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
 */
33
function publisher_items_columns_show($options)
0 ignored issues
show
Coding Style introduced by
publisher_items_columns_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...
34
{
35
    //    global $xoTheme;
36
    $publisher = PublisherPublisher::getInstance();
37
38
    //Column Settings
39
    $optNumColumns  = isset($options[0]) ? (int)$options[0] : '2';
40
    $selCategories  = isset($options[1]) ? explode(',', $options[1]) : array();
41
    $optCatItems    = (int)$options[2];
42
    $optCatTruncate = isset($options[3]) ? (int)$options[3] : '0';
43
44
    $block                  = array();
45
    $block['lang_reads']    = _MB_PUBLISHER_READS;
46
    $block['lang_comments'] = _MB_PUBLISHER_COMMENTS;
47
    $block['lang_readmore'] = _MB_PUBLISHER_READMORE;
48
49
    $selCategoriesObj = array();
50
51
    //get permited categories only once
52
    $categoriesObj = $publisher->getHandler('category')->getCategories(0, 0, -1);
53
54
    //if not selected 'all', let's get the selected ones
55
    if (!in_array(0, $selCategories)) {
56
        foreach ($categoriesObj as $key => $value) {
57
            if (in_array($key, $selCategories)) {
58
                $selCategoriesObj[$key] = $value;
59
            }
60
        }
61
    } else {
62
        $selCategoriesObj =& $categoriesObj;
63
    }
64
    unset($key, $value);
65
66
    $ccount = count($selCategoriesObj);
67
68
    if ($ccount === 0) {
69
        return false;
70
    }
71
72
    if ($ccount < $optNumColumns) {
73
        $optNumColumns = $ccount;
74
    }
75
76
    $k       = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
77
    $columns = array();
78
79
    foreach ($selCategoriesObj as $categoryId => $mainitemCatObj) {
80
        $categoryItemsObj = $publisher->getHandler('item')->getAllPublished($optCatItems, 0, $categoryId);
81
        $scount           = count($categoryItemsObj);
82
        if ($scount > 0 && is_array($categoryItemsObj)) {
83
            reset($categoryItemsObj);
84
            //First Item
85
            list($itemid, $thisitem) = each($categoryItemsObj);
0 ignored issues
show
Unused Code introduced by
The assignment to $itemid is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
86
87
            $mainitem['item_title']      = $thisitem->getTitle();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$mainitem was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mainitem = 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...
88
            $mainitem['item_cleantitle'] = strip_tags($thisitem->getTitle());
0 ignored issues
show
Bug introduced by
The variable $mainitem 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...
89
            $mainitem['item_link']       = $thisitem->itemid();
90
            $mainitem['itemurl']         = $thisitem->getItemUrl();
91
            $mainImage                   = $thisitem->getMainImage();
92
93
            // check to see if GD function exist
94
            $mainitem['item_image'] = $mainImage['image_path'];
95
            if (!empty($mainImage['image_path']) && function_exists('imagecreatetruecolor')) {
96
                $mainitem['item_image'] = PUBLISHER_URL . '/thumb.php?src=' . $mainImage['image_path'] . '&amp;w=100';
97
            }
98
99
            $mainitem['item_summary'] = $thisitem->getBlockSummary($optCatTruncate);
100
101
            $mainitem['item_cat_name']        = $mainitemCatObj->name();
102
            $mainitem['item_cat_description'] = $mainitemCatObj->description() !== '' ? $mainitemCatObj->description() : $mainitemCatObj->name();
103
            $mainitem['item_cat_link']        = $mainitemCatObj->getCategoryLink();
104
            $mainitem['categoryurl']          = $mainitemCatObj->getCategoryUrl();
105
106
            //The Rest
107
            if ($scount > 1) {
108
                while ((list($itemid, $thisitem) = each($categoryItemsObj)) !== false) {
109
                    $subitem['title']      = $thisitem->getTitle();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$subitem was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subitem = 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...
110
                    $subitem['cleantitle'] = strip_tags($thisitem->getTitle());
0 ignored issues
show
Bug introduced by
The variable $subitem 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...
111
                    $subitem['link']       = $thisitem->getItemLink();
112
                    $subitem['itemurl']    = $thisitem->getItemUrl();
113
                    $subitem['summary']    = $thisitem->getBlockSummary($optCatTruncate);
114
                    $mainitem['subitem'][] = $subitem;
115
                    unset($subitem);
116
                }
117
            }
118
            $columns[$k][] = $mainitem;
119
            unset($thisitem, $mainitem);
120
            ++$k;
121
122
            if ($k == $optNumColumns) {
123
                $k = 0;
124
            }
125
        }
126
    }
127
    unset($categoryId, $mainitemCatObj);
128
129
    $block['template']    = $options[4];
130
    $block['columns']     = $columns;
131
    $block['columnwidth'] = (int)(100 / $optNumColumns);
132
133
    $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/' . PUBLISHER_DIRNAME . '/assets/css/publisher.css');
134
135
    return $block;
136
}
137
138
/***
139
 * Edit Function For Multi-Column Category Items Display Block
140
 *
141
 * @param    array $options Block Options
142
 *
143
 * @return string
144
 */
145
function publisher_items_columns_edit($options)
146
{
147
    include_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
148
    xoops_load('XoopsFormLoader');
149
150
    $form   = new PublisherBlockForm();
151
    $colEle = new XoopsFormSelect(_MB_PUBLISHER_NUMBER_COLUMN_VIEW, 'options[0]', $options[0]);
152
    $colEle->addOptionArray(array(
153
                                '1' => 1,
154
                                '2' => 2,
155
                                '3' => 3,
156
                                '4' => 4,
157
                                '5' => 5));
158
    $catEle      = new XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, publisherCreateCategorySelect($options[1], 0, true, 'options[1]'));
159
    $cItemsEle   = new XoopsFormText(_MB_PUBLISHER_NUMBER_ITEMS_CAT, 'options[2]', 4, 255, $options[2]);
160
    $truncateEle = new XoopsFormText(_MB_PUBLISHER_TRUNCATE, 'options[3]', 4, 255, $options[3]);
161
162
    $tempEle = new XoopsFormSelect(_MB_PUBLISHER_TEMPLATE, 'options[4]', $options[4]);
163
    $tempEle->addOptionArray(array(
164
                                 'normal'   => _MB_PUBLISHER_TEMPLATE_NORMAL,
165
                                 'extended' => _MB_PUBLISHER_TEMPLATE_EXTENDED));
166
167
    $form->addElement($colEle);
168
    $form->addElement($catEle);
169
    $form->addElement($cItemsEle);
170
    $form->addElement($truncateEle);
171
    $form->addElement($tempEle);
172
173
    return $form->render();
174
}
175