moving_partner.php ➔ b_random_partner_edit()   D
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 107
Code Lines 37

Duplication

Lines 107
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 128
nop 1
dl 107
loc 107
rs 4.606
c 0
b 0
f 0

How to fix   Long Method   

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
 * Module: SmartPartner
6
 * Author: The SmartFactory <www.smartfactory.ca>
7
 * Licence: GNU
8
 * @param $options
9
 * @return array
10
 */
11
12
function b_random_partner_show($options)
13
{
14
    include_once(XOOPS_ROOT_PATH . '/modules/smartpartner/include/common.php');
15
16
    // Creating the partner handler object
17
    $partnerHandler = smartpartner_gethandler('partner');
18
19
    // Randomize
20
    $partnersObj =& $partnerHandler->getPartners(0, 0, _SPARTNER_STATUS_ACTIVE);
21
    if (count($partnersObj) > 0) {
22
        $key_arr    = array_keys($partnersObj);
23
        $key_rand   = array_rand($key_arr, 1);
24
        $partnerObj = $partnersObj[$key_rand];
25
    }
26
27
    $block = array();
28
    if ($partnerObj) {
29
        $partner['id']      = $partnerObj->id();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$partner was never initialized. Although not strictly required by PHP, it is generally a good practice to add $partner = 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...
Bug introduced by
The variable $partnerObj 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...
30
        $partner['urllink'] = $partnerObj->getUrlLink('block');
31
        if ($partnerObj->image() && (($options[1] == 1) || ($options[1] == 3))) {
32
            $partner['image'] = $partnerObj->getImageUrl();
33
        }
34
        if ($partnerObj->image() && (($options[1] == 2) || ($options[1] == 3))) {
35
            $partner['title'] = $partnerObj->title();
36
        } else {
37
            $partner['title'] = '';
38
        }
39
        $smartConfig             = smartpartner_getModuleConfig();
40
        $image_info              = smartpartner_imageResize($partnerObj->getImagePath(), $smartConfig['img_max_width'], $smartConfig['img_max_height']);
41
        $partner['img_attr']     = $image_info[3];
42
        $partner['extendedInfo'] = $partnerObj->extentedInfo();
43
44
        if ($options[0] == 1) {
45
            $block['fadeImage'] = 'style="filter:alpha(opacity=20);" onmouseover="nereidFade(this,100,30,5)" onmouseout="nereidFade(this,50,30,5)"';
46
        }
47
48
        $block['see_all']          = $options[2];
49
        $block['lang_see_all']     = _MB_SPARTNER_LANG_SEE_ALL;
50
        $block['smartpartner_url'] = SMARTPARTNER_URL;
51
    }
52
53
    return $block;
54
}
55
56
/**
57
 * @param $options
58
 * @return string
59
 */
60 View Code Duplication
function b_random_partner_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...
61
{
62
    $form = "<table border='0'>";
63
    /*$form .= "<tr><td>"._MB_SPARTNER_PARTNERS_PSPACE."</td><td>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
64
     $chk   = "";
65
     if ($options[0] == 0) {
66
         $chk = " checked='checked'";
67
     }
68
     $form .= "<input type='radio' name='options[0]' value='0'".$chk." />"._NO."";
69
     $chk   = "";
70
     if ($options[0] == 1) {
71
         $chk = " checked='checked'";
72
     }
73
     $form .= "<input type='radio' name='options[0]' value='1'".$chk." />"._YES."</td></tr>";*/
74
    $form .= '<tr><td>' . _MB_SPARTNER_FADE . '</td><td>';
75
    $chk = '';
76
    if ($options[0] == 0) {
77
        $chk = " checked='checked'";
78
    }
79
    $form .= "<input type='radio' name='options[1]' value='0'" . $chk . ' />' . _NO . '';
80
    $chk = '';
81
    if ($options[0] == 1) {
82
        $chk = " checked='checked'";
83
    }
84
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
85
    /*$form .= "<tr><td>"._MB_SPARTNER_BRAND."</td><td>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
86
     $chk   = "";
87
     if ($options[2] == 0) {
88
         $chk = " checked='checked'";
89
     }
90
     $form .= "<input type='radio' name='options[2]' value='0'".$chk." />"._NO."";
91
     $chk   = "";
92
     if ($options[2] == 1) {
93
         $chk = " checked='checked'";
94
     }
95
     $form .= "<input type='radio' name='options[2]' value='1'".$chk." />"._YES."</td></tr>";
96
     $form .= "<tr><td>"._MB_SPARTNER_BLIMIT."</td><td>";
97
     $form .= "<input type='text' name='options[3]' size='16' value='".$options[3]."' /></td></tr>";*/
98
    $form .= '<tr><td>' . _MB_SPARTNER_BSHOW . '</td><td>';
99
    $form .= "<select size='1' name='options[1]'>";
100
    $sel = '';
101
    if ($options[1] == 1) {
102
        $sel = " selected='selected'";
103
    }
104
    $form .= "<option value='1' " . $sel . '>' . _MB_SPARTNER_IMAGES . '</option>';
105
    $sel = '';
106
    if ($options[1] == 2) {
107
        $sel = " selected='selected'";
108
    }
109
    $form .= "<option value='2' " . $sel . '>' . _MB_SPARTNER_TEXT . '</option>';
110
    $sel = '';
111
    if ($options[1] == 3) {
112
        $sel = " selected='selected'";
113
    }
114
    $form .= "<option value='3' " . $sel . '>' . _MB_SPARTNER_BOTH . '</option>';
115
    $form .= '</select></td></tr>';
116
    /*$form .= "<tr><td>"._MB_SPARTNER_BORDER."</td><td>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
117
     $form .= "<select size='1' name='options[5]'>";
118
     $sel = "";
119
     if ($options[5] == "id") {
120
         $sel = " selected='selected'";
121
     }
122
     $form .= "<option value='id' ".$sel.">"._MB_SPARTNER_ID."</option>";
123
     $sel = "";
124
     if ($options[5] == "hits") {
125
         $sel = " selected='selected'";
126
     }
127
     $form .= "<option value='hits' ".$sel.">"._MB_SPARTNER_HITS."</option>";
128
     $sel = "";
129
     if ($options[5] == "title") {
130
         $sel = " selected='selected'";
131
     }
132
     $form .= "<option value='title' ".$sel.">"._MB_SPARTNER_TITLE."</option>";
133
     if ($options[5] == "weight") {
134
         $sel = " selected='selected'";
135
     }
136
     $form .= "<option value='weight' ".$sel.">"._MB_SPARTNER_WEIGHT."</option>";
137
     $form .= "</select> ";
138
     $form .= "<select size='1' name='options[6]'>";
139
     $sel = "";
140
     if ($options[6] == "ASC") {
141
         $sel = " selected='selected'";
142
     }
143
     $form .= "<option value='ASC' ".$sel.">"._MB_SPARTNER_ASC."</option>";
144
     $sel = "";
145
     if ($options[6] == "DESC") {
146
         $sel = " selected='selected'";
147
     }
148
     $form .= "<option value='DESC' ".$sel.">"._MB_SPARTNER_DESC."</option>";
149
     $form .= "</select></td></tr>";
150
     */
151
    $form .= '<tr><td>' . _MB_SPARTNER_SEE_ALL . '</td><td>';
152
    $chk = '';
153
    if ($options[2] == 0) {
154
        $chk = " checked='checked'";
155
    }
156
    $form .= "<input type='radio' name='options[2]' value='0'" . $chk . ' />' . _NO . '';
157
    $chk = '';
158
    if ($options[7] == 1) {
159
        $chk = " checked='checked'";
160
    }
161
    $form .= "<input type='radio' name='options[2]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
162
163
    $form .= '</table>';
164
165
    return $form;
166
}
167