recent_partners.php ➔ b_recent_partners_edit()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 96
Code Lines 24

Duplication

Lines 96
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 16
nop 1
dl 96
loc 96
rs 8.1954
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
// 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...
12
13
function b_recent_partners_show($options)
14
{
15
    include_once(XOOPS_ROOT_PATH . '/modules/smartpartner/include/common.php');
16
17
    // Creating the partner handler object
18
    $smartPartnerPartnerHandler  = smartpartner_gethandler('partner');
19
    $smartPartnerCategoryHandler = smartpartner_gethandler('category');
0 ignored issues
show
Unused Code introduced by
$smartPartnerCategoryHandler 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...
20
21
    // Randomize
22
    $partnersObj = $smartPartnerPartnerHandler->getPartners($options[2], 0, _SPARTNER_STATUS_ACTIVE, 'datesub', 'DESC');
23
24
    include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobjectpermission.php';
25
    $smartPermissionsHandler = new SmartobjectPermissionHandler($smartPartnerPartnerHandler);
26
    $grantedItems            = $smartPermissionsHandler->getGrantedItems('full_view');
27
28
    if ($partnersObj) {
29
        $block = array();
30
        foreach ($partnersObj as $partnerObj) {
31
            if (in_array($partnerObj->id(), $grantedItems)) {
32
                $block['partners'][] = $partnerObj->toArray();
33
            }
34
        }
35
    }
36
    if (!empty($block['partners'])) {
37
        if ($options[0] == 1) {
38
            $block['insertBr'] = true;
39
        }
40
        if ($options[1] == 1) {
41
            $block['fadeImage'] = 'style="filter:alpha(opacity=20);" onmouseover="nereidFade(this,100,30,5)" onmouseout="nereidFade(this,50,30,5)"';
42
        }
43
44
        //$block['see_all'] = $options[6];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
45
        $block['lang_see_all']     = _MB_SPARTNER_LANG_SEE_ALL;
46
        $block['smartpartner_url'] = SMARTPARTNER_URL;
47
    }
48
49
    return $block;
0 ignored issues
show
Bug introduced by
The variable $block 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...
50
}
51
52
/**
53
 * @param $options
54
 * @return string
55
 */
56 View Code Duplication
function b_recent_partners_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...
57
{
58
    $form = "<table border='0'>";
59
    $form .= '<tr><td>' . _MB_SPARTNER_PARTNERS_PSPACE . '</td><td>';
60
    $chk = '';
61
    if ($options[0] == 0) {
62
        $chk = " checked='checked'";
63
    }
64
    $form .= "<input type='radio' name='options[0]' value='0'" . $chk . ' />' . _NO . '';
65
    $chk = '';
66
    if ($options[0] == 1) {
67
        $chk = " checked='checked'";
68
    }
69
    $form .= "<input type='radio' name='options[0]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
70
    $form .= '<tr><td>' . _MB_SPARTNER_FADE . '</td><td>';
71
    $chk = '';
72
    if ($options[1] == 0) {
73
        $chk = " checked='checked'";
74
    }
75
    $form .= "<input type='radio' name='options[1]' value='0'" . $chk . ' />' . _NO . '';
76
    $chk = '';
77
    if ($options[1] == 1) {
78
        $chk = " checked='checked'";
79
    }
80
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
81
    $form .= '<tr><td>' . _MB_SPARTNER_BLIMIT . '</td><td>';
82
    $form .= "<input type='text' name='options[2]' size='16' value='" . $options[2] . "' /></td></tr>";
83
    /*$form .= "<tr><td>"._MB_SPARTNER_BSHOW."</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...
84
     $form .= "<select size='1' name='options[3]'>";
85
     $sel = "";
86
     if ($options[3] == 1) {
87
         $sel = " selected='selected'";
88
     }
89
     $form .= "<option value='1' ".$sel.">"._MB_SPARTNER_IMAGES."</option>";
90
     $sel = "";
91
     if ($options[3] == 2) {
92
         $sel = " selected='selected'";
93
     }
94
     $form .= "<option value='2' ".$sel.">"._MB_SPARTNER_TEXT."</option>";
95
     $sel = "";
96
     if ($options[3] == 3) {
97
         $sel = " selected='selected'";
98
     }
99
     $form .= "<option value='3' ".$sel.">"._MB_SPARTNER_BOTH."</option>";
100
     $form .= "</select></td></tr>";
101
     $form .= "<tr><td>"._MB_SPARTNER_BORDER."</td><td>";
102
     $form .= "<select size='1' name='options[5]'>";
103
     $sel = "";
104
     if ($options[4] == "id") {
105
         $sel = " selected='selected'";
106
     }
107
     $form .= "<option value='id' ".$sel.">"._MB_SPARTNER_ID."</option>";
108
     $sel = "";
109
     if ($options[4] == "hits") {
110
         $sel = " selected='selected'";
111
     }
112
     $form .= "<option value='hits' ".$sel.">"._MB_SPARTNER_HITS."</option>";
113
     $sel = "";
114
     if ($options[4] == "title") {
115
         $sel = " selected='selected'";
116
     }
117
     $form .= "<option value='title' ".$sel.">"._MB_SPARTNER_TITLE."</option>";
118
     if ($options[4] == "weight") {
119
         $sel = " selected='selected'";
120
     }
121
     $form .= "<option value='weight' ".$sel.">"._MB_SPARTNER_WEIGHT."</option>";
122
     $form .= "</select> ";
123
     $form .= "<select size='1' name='options[6]'>";
124
     $sel = "";
125
     if ($options[5] == "ASC") {
126
         $sel = " selected='selected'";
127
     }
128
     $form .= "<option value='ASC' ".$sel.">"._MB_SPARTNER_ASC."</option>";
129
     $sel = "";
130
     if ($options[5] == "DESC") {
131
         $sel = " selected='selected'";
132
     }
133
     $form .= "<option value='DESC' ".$sel.">"._MB_SPARTNER_DESC."</option>";
134
     $form .= "</select></td></tr>";
135
136
     $form .= "<tr><td>"._MB_SPARTNER_SEE_ALL."</td><td>";
137
     $chk   = "";
138
     if ($options[6] == 0) {
139
         $chk = " checked='checked'";
140
     }
141
     $form .= "<input type='radio' name='options[7]' value='0'".$chk." />"._NO."";
142
     $chk   = "";
143
     if ($options[6] == 1) {
144
         $chk = " checked='checked'";
145
     }
146
     $form .= "<input type='radio' name='options[7]' value='1'".$chk." />"._YES."</td></tr>";*/
147
148
    $form .= '</table>';
149
150
    return $form;
151
}
152