Completed
Pull Request — master (#9)
by Michael
01:20
created

partners.php ➔ b_xoopspartners_show()   C

Complexity

Conditions 10
Paths 18

Size

Total Lines 58
Code Lines 41

Duplication

Lines 9
Ratio 15.52 %

Importance

Changes 0
Metric Value
cc 10
eloc 41
nc 18
nop 1
dl 9
loc 58
rs 6.6515
c 0
b 0
f 0

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
 * 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
 * This program is distributed in the hope that it will be useful,
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
 *
10
 *-----------------------------------
11
 * Author: Raul Recio (AKA UNFOR)
12
 * Project: The XOOPS Project
13
 *-----------------------------------
14
 */
15
/**
16
 * Module: XoopsPartners - a partner affiliation links module
17
 *
18
 * @package      module\xoopspartners\blocks
19
 * @author       Raul Recio (aka UNFOR)
20
 * @author       XOOPS Module Development Team
21
 * @copyright    {@link https://xoops.org 2001-2016 XOOPS Project}
22
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
23
 * @link         https://xoops.org XOOPS
24
 */
25
use Xmf\Module\Helper;
26
27
/**
28
 *
29
 * Show partners in block
30
 * @param array $options from block preferences
31
 *
32
 *  $options:  0  - Put spaces between partners
33
 *             1  - Fade partners in/out
34
 *             2  - Randomize which partners to display in block
35
 *             3  - Number of partners to display
36
 *             4  - show images|text|both
37
 *             5  - display order id|hits|title|weight
38
 *             6  - order ASC|DESC
39
 *             7  - max title length (0 for unlimited)
40
 *
41
 * @return array block settings
42
 */
43
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...
44
{
45
    $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...
46
47
    $moduleDirName     = basename(dirname(__DIR__));
48
    $xpHelper          = Helper::getHelper($moduleDirName);
49
    $xpPartnersHandler = $xpHelper->getHandler('partners');
50
51
    $block             = array('xpDir' => $moduleDirName);
52
53
    $pFields         = array('id', 'url', 'image', 'title', 'description');
54
    $criteria        = new CriteriaCompo(new Criteria('status', 1, '='));
55
    $criteria->setLimit($options[3]);
56
    if ($options[2]) {
57
        $criteria->setSort('RAND()');
58
    } else {
59
        $criteria->setSort($options[5]);
60
        $criteria->setOrder($options[6]);
61
    }
62
    $pObjs = $xpPartnersHandler->getAll($criteria, $pFields);
63
    foreach ($pObjs as $pObj) {
64
        $partners    = array();
65
        $url         = $pObj->getVar('url');
66
        $origtitle   = $pObj->getVar('title');
67
        $title       = $origtitle;
68
        $description = $pObj->getVar('description');
69
        $image       = $pObj->getVar('image');
70
        //@TODO:  make display string length a config option
71
        if (!empty($options[7])) {
72
            $title = xoops_substr($origtitle, 0, (int)$options[7]);
73
        }
74
75
        //        $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...
76
        $partners['id']          = $pObj->getVar('id');
77
        $partners['url']         = $url;
78
        $partners['description'] = $description;
79
80 View Code Duplication
        if (!empty($image) && (1 == $options[4] || 3 == $options[4])) {
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...
81
            $partners['image'] = $image;
82
            $partners['image_ttl'] = $title;
83
        }
84 View Code Duplication
        if (empty($image) || (2 == $options[4]) || (3 == $options[4])) {
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
            $partners['title'] = $title;
86
        } else {
87
            $partners['title'] = '';
88
        }
89
        $block['partners'][] = $partners;
90
    }
91
    $block['insertBr']  = (1 == $options[0]);
92
    $block['fadeImage'] = (1 == $options[1]) ;
93
94
    //now load the stylesheet & jquery
95
    $GLOBALS['xoTheme']->addStylesheet($xpHelper->url('assets/css/style.css'));
96
    $GLOBALS['xoTheme']->addScript("browse.php?Frameworks/jquery/jquery.js");
97
    $GLOBALS['xoTheme']->renderMetas(null, true);
98
99
    return $block;
100
}
101
102
/**
103
 *
104
 * Edit Random Partners block preferences
105
 *
106
 * @param array $options from block preferences
107
 *
108
 * @return string HTML to display for edit form
109
 */
110
function b_xoopspartners_edit($options)
111
{
112
    if (0 == $options[0]) { //put spaces between partners
113
        $chk0no  = " checked";
114
        $chk0yes = '';
115
    } else {
116
        $chk0no  = '';
117
        $chk0yes = " checked";
118
    }
119
    if (0 == $options[1]) { //fade partners in/out
120
        $chk1no  = " checked";
121
        $chk1yes = '';
122
    } else {
123
        $chk1no  = '';
124
        $chk1yes = " checked";
125
    }
126
    if (0 == $options[2]) {  //randomize partners in block
127
        $chk2no  = " checked";
128
        $chk2yes = '';
129
    } else {
130
        $chk2no  = '';
131
        $chk2yes = " checked";
132
    }
133
    $form = "<table class='bnone'>\n"
134
        . "  <tr>\n"
135
        . "    <td>" . _MB_XOOPSPARTNERS_PSPACE . "</td>\n"
136
        . "    <td>"
137
        .        "<input type='radio' name='options[0]' id ='options0_0' value='0'{$chk0no}>"
138
        .        "<label for='options0_0'>" . _NO . "</label>&nbsp;"
139
        .        "<input type='radio' name='options[0]' id ='options0_1' value='1'{$chk0yes}>"
140
        .        "<label for='options0_1'>" . _YES . "</label>"
141
        .      "</td>\n"
142
        . "  </tr>\n"
143
        . "  <tr>\n"
144
        . "    <td>" . _MB_XOOPSPARTNERS_FADE . "</td>\n"
145
        . "    <td>"
146
        .        "<input type='radio' name='options[1]' id='options1_0' value='0'{$chk1no}>" . _NO
147
        .        "<label for='options1_0'>" . _NO . "</label>&nbsp;"
148
        .        "<input type='radio' name='options[1]' id='options1_1' value='1'{$chk1yes}>" . _YES
149
        .        "<label for='options1_1'>" . _YES . "</label>"
150
        .      "</td>\n"
151
        . "  </tr>\n"
152
        . "  <tr>\n"
153
        . "    <td>" . _MB_XOOPSPARTNERS_BRAND . "</td>\n"
154
        . "     <td>"
155
        .         "<input type='radio' name='options[2]' id='option2_0' value='0'{$chk2no}>" . _NO
156
        .         "<label for='options2_0'>" . _NO . "</label>"
157
        .         "<input type='radio' name='options[2]' id='options2_1' value='1'{$chk2yes}>" . _YES
158
        .         "<label for='options2_1'>" . _YES . "</label>"
159
        .       "</td>\n"
160
        . "  </tr>\n"
161
        . "  <tr>\n"
162
        . "    <td>" . _MB_XOOPSPARTNERS_BLIMIT . "</td>\n"
163
        . "    <td><input class='right' type='number' name='options[3]' size='5' value='{$options[3]}' min='0'></td>\n"
164
        . "  </tr>\n"
165
        . "  <tr>\n"
166
        . '    <td>' . _MB_XOOPSPARTNERS_BSHOW . "</td>\n"
167
        . "    <td>\n"
168
        . "      <select size='1' name='options[4]'>\n";
169
    $sel  = (1 == $options[4]) ? " selected" : '';
170
    $form .= "        <option value='1'{$sel}>" . _MB_XOOPSPARTNERS_IMAGES . "</option>\n";
171
172
    $sel = (2 == $options[4]) ? " selected" : '';
173
    $form .= "        <option value='2'{$sel}>" . _MB_XOOPSPARTNERS_TEXT . "</option>\n";
174
175
    $sel = (3 == $options[4]) ? " selected" : '';
176
    $form .= "        <option value='3'{$sel}>" . _MB_XOOPSPARTNERS_BOTH . "</option>\n"
177
           . "      </select>\n"
178
           . "    </td>\n"
179
           . "  </tr>\n"
180
           . "  <tr>\n"
181
           . "    <td>" . _MB_XOOPSPARTNERS_BSORT . "</td>\n"
182
           . "    <td>\n"
183
           . "      <select size='1' name='options[5]'>\n";
184
185
    $sel = ('id' === $options[5]) ? " selected" : '';
186
    $form .= "        <option value='id'{$sel}>" . _MB_XOOPSPARTNERS_ID . "</option>\n";
187
188
    $sel = ('hits' === $options[5]) ? " selected" : '';
189
    $form .= "        <option value='hits'{$sel}>" . _MB_XOOPSPARTNERS_HITS . "</option>\n";
190
191
    $sel = ('title' === $options[5]) ? " selected" : '';
192
    $form .= "        <option value='title'{$sel}>" . _MB_XOOPSPARTNERS_TITLE . "</option>\n";
193
194
    $sel = ('weight' === $options[5]) ? " selected" : '';
195
    $form .= "        <option value='weight'{$sel}>" . _MB_XOOPSPARTNERS_WEIGHT . "</option>\n"
196
           . "      </select>\n"
197
           . "      <select size='1' name='options[6]'>\n";
198
199
    $sel = ('ASC' === $options[6]) ? " selected" : '';
200
    $form .= "        <option value='ASC'{$sel}>" . _MB_XOOPSPARTNERS_ASC . "</option>\n";
201
202
    $sel = ('DESC' === $options[6]) ? " selected" : '';
203
    $form .= "        <option value='DESC'{$sel}>" . _MB_XOOPSPARTNERS_DESC . "</option>\n"
204
           . "      </select>\n"
205
           . "    </td>\n"
206
           . "  </tr>\n"
207
           . "  <tr>\n"
208
           . '    <td>' . _MB_XOOPSPARTNERS_TTL_LENGTH . "</td>\n"
209
           . "    <td>"
210
           .        "<input type='number' class='right' name='options[7]' size='5' value='{$options[7]}' min='0'>"
211
           .      "</td>\n"
212
           . "  </tr>\n"
213
           . "  <tr>\n"
214
           . "</table>\n";
215
    return $form;
216
}
217