Completed
Push — master ( d674d6...6c0c4d )
by Michael
04:12 queued 02:03
created

partners.php ➔ b_xoopspartners_edit()   F

Complexity

Conditions 13
Paths 4096

Size

Total Lines 56
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 44
c 1
b 0
f 0
nc 4096
nop 1
dl 0
loc 56
rs 3.3579

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
4
  ------------------------------------------------------------------------
5
                XOOPS - PHP Content Management System
6
                    Copyright (c) 2000 XOOPS.org
7
                       <http://www.xoops.org/>
8
  ------------------------------------------------------------------------
9
  This program is free software; you can redistribute it and/or modify
10
  it under the terms of the GNU General Public License as published by
11
  the Free Software Foundation; either version 2 of the License, or
12
  (at your option) any later version.
13
14
  You may not change or alter any portion of this comment or credits
15
  of supporting developers from this source code or any supporting
16
  source code which is considered copyrighted (c) material of the
17
  original comment or credit authors.
18
19
  This program is distributed in the hope that it will be useful,
20
  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
  GNU General Public License for more details.
23
24
  You should have received a copy of the GNU General Public License
25
  along with this program; if not, write to the Free Software
26
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
27
  ------------------------------------------------------------------------
28
 Author: Raul Recio (AKA UNFOR)
29
 Project: The XOOPS Project
30
 -------------------------------------------------------------------------
31
 */
32
/**
33
 * Module: XoopsPartners - a partner affiliation links module
34
 *
35
 * @category     Module
36
 * @package      xoopspartners
37
 * @subpackage   admin
38
 * @author       ::     Raul Recio (aka UNFOR)
39
 * @author       XOOPS Module Development Team
40
 * @copyright    {@link http://xoops.org 2001-2016 XOOPS Project}
41
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
42
 * @link         http://xoops.org XOOPS
43
 */
44
45
/**
46
 *
47
 * Show partners in block
48
 * @param array $options from block preferences
49
 *
50
 *  $options:  0  - Put spaces between partners
51
 *             1  - Fade partners in/out
52
 *             2  - Randomize which partners to display in block
53
 *             3  - Number of partners to display
54
 *             4  - show images|text|both
55
 *             5  - display order id|hits|title|weight
56
 *             6  - order ASC|DESC
57
 *             7  - max title length (0 for unlimited)
58
 *
59
 * @return array block settings
60
 */
61
function b_xoopspartners_show($options)
0 ignored issues
show
Coding Style introduced by
b_xoopspartners_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...
62
{
63
    $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
$myts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65
    $block          = array();
66
    $moduleDirname  = basename(dirname(__DIR__));
67
    $block['xpDir'] = $moduleDirname;
68
69
    $partnerHandler = xoops_getModuleHandler('partners', $moduleDirname);
70
    $pFields         = array('id', 'url', 'image', 'title', 'description');
71
    $criteria        = new CriteriaCompo();
72
    $criteria->setLimit($options[3]);
73
    if ($options[2]) {
74
        $criteria->setSort('RAND()');
75
    } else {
76
        $criteria->setSort($options[5]);
77
        $criteria->setOrder($options[6]);
78
    }
79
    $pObjs = $partnerHandler->getAll($criteria, $pFields);
80
    foreach ($pObjs as $pObj) {
81
        $url         = $pObj->getVar('url');
82
        $origtitle   = $pObj->getVar('title');
83
        $title       = $origtitle;
84
        $description = $pObj->getVar('description');
85
        $image       = $pObj->getVar('image');
86
        //@TODO:  make display string length a config option
87
        if (!empty($options[7])) {
88
            $title = xoops_substr($origtitle, 0, (int)$options[7]);
89
        }
90
91
        //        $title = (mb_strlen($origtitle) > 19) ? xoops_substr($title, 0, 19) : $title;
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
92
        $partners['id']          = $pObj->getVar('id');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$partners was never initialized. Although not strictly required by PHP, it is generally a good practice to add $partners = 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...
93
        $partners['url']         = $url;
94
        $partners['description'] = $description;
95
        if (!empty($image) && (1 == $options[4] || 3 == $options[4])) {
96
            $partners['image'] = $image;
97
        }
98
        if (empty($image) || (2 == $options[4]) || (3 == $options[4])) {
99
            $partners['title'] = $title;
100
        } else {
101
            $partners['title'] = '';
102
        }
103
        $block['partners'][] = $partners;
104
    }
105
    $block['insertBr']  = (1 == $options[0]);
106
    $block['fadeImage'] = (1 == $options[1]) ;
107
108
    //now load the stylesheet
109
    $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . "/modules/{$moduleDirname}/assets/css/style.css");
110
111
    return $block;
112
}
113
114
/**
115
 *
116
 * Edit Random Partners block preferences
117
 *
118
 * @param array $options from block preferences
119
 *
120
 * @return string HTML to display for edit form
121
 */
122
function b_xoopspartners_edit($options)
123
{
124
    if (0 == $options[0]) { //put spaces between partners
125
        $chk0no  = " checked='checked'";
126
        $chk0yes = '';
127
    } else {
128
        $chk0no  = '';
129
        $chk0yes = " checked='checked'";
130
    }
131
    if (0 == $options[1]) { //fade partners in/out
132
        $chk1no  = " checked='checked'";
133
        $chk1yes = '';
134
    } else {
135
        $chk1no  = '';
136
        $chk1yes = " checked='checked'";
137
    }
138
    if (0 == $options[2]) {  //randomize partners in block
139
        $chk2no  = " checked='checked'";
140
        $chk2yes = '';
141
    } else {
142
        $chk2no  = '';
143
        $chk2yes = " checked='checked'";
144
    }
145
    $form =
146
        "<table style='border-width: 0px;'>\n" . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_PSPACE . "</td>\n" . '    <td>' . "<input type='radio' name='options[0]' value='0'{$chk0no}>" . _NO . '' . "<input type='radio' name='options[0]' value='1'{$chk0yes}>" . _YES . '' . "    </td>\n" . "  </tr>\n"
147
        . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_FADE . "</td>\n" . '    <td>' . "<input type='radio' name='options[1]' value='0'{$chk1no}>" . _NO . '' . "<input type='radio' name='options[1]' value='1'{$chk1yes}>" . _YES . "</td>\n" . "  </tr>\n" . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_BRAND
148
        . "</td>\n" . '     <td>' . "<input type='radio' name='options[2]' value='0'{$chk2no}>" . _NO . '' . "<input type='radio' name='options[2]' value='1'{$chk2yes}>" . _YES . "</td>\n" . "  </tr>\n" . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_BLIMIT . "</td>\n"
149
        . "    <td><input class='txtright' type='number' name='options[3]' size='5' value='{$options[3]}' min='0'></td>\n" . "  </tr>\n" . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_BSHOW . "</td>\n" . "    <td>\n" . "      <select size='1' name='options[4]'>\n";
150
    $sel  = (1 == $options[4]) ? " selected='selected'" : '';
151
    $form .= "        <option value='1'{$sel}>" . _MB_XPARTNERS_IMAGES . "</option>\n";
152
153
    $sel = (2 == $options[4]) ? " selected='selected'" : '';
154
    $form .= "        <option value='2'{$sel}>" . _MB_XPARTNERS_TEXT . "</option>\n";
155
156
    $sel = (3 == $options[4]) ? " selected='selected'" : '';
157
    $form .= "        <option value='3'{$sel}>" . _MB_XPARTNERS_BOTH . "</option>\n" . "      </select>\n" . "    </td>\n" . "  </tr>\n" . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_BSORT . "</td>\n" . "    <td>\n" . "      <select size='1' name='options[5]'>";
158
    $sel = ('id' === $options[5]) ? " selected='selected'" : '';
159
    $form .= "        <option value='id'{$sel}>" . _MB_XPARTNERS_ID . "</option>\n";
160
161
    $sel = ('hits' === $options[5]) ? " selected='selected'" : '';
162
    $form .= "        <option value='hits'{$sel}>" . _MB_XPARTNERS_HITS . "</option>\n";
163
164
    $sel = ('title' === $options[5]) ? " selected='selected'" : '';
165
    $form .= "        <option value='title'{$sel}>" . _MB_XPARTNERS_TITLE . "</option>\n";
166
167
    $sel = ('weight' === $options[5]) ? " selected='selected'" : '';
168
    $form .= "        <option value='weight'{$sel}>" . _MB_XPARTNERS_WEIGHT . "</option>\n" . "      </select>\n" . "      <select size='1' name='options[6]'>\n";
169
170
    $sel = ('ASC' === $options[6]) ? " selected='selected'" : '';
171
    $form .= "        <option value='ASC'{$sel}>" . _MB_XPARTNERS_ASC . "</option>\n";
172
173
    $sel = ('DESC' === $options[6]) ? " selected='selected'" : '';
174
    $form .= "        <option value='DESC'{$sel}>" . _MB_XPARTNERS_DESC . "</option>\n" . "      </select>\n" . "    </td>\n" . "  </tr>\n" . "  <tr>\n" . '    <td>' . _MB_XPARTNERS_TTL_LENGTH . "</td>\n"
175
             . "    <td><input type='number' class='txtright' name='options[7]' size='5' value='{$options[7]}' min='0'></td>\n" . "  </tr>\n" . "  <tr>\n" . "</table>\n";
176
    return $form;
177
}
178