random_offer.php ➔ b_random_offer_edit()   D
last analyzed

Complexity

Conditions 9
Paths 128

Size

Total Lines 107
Code Lines 37

Duplication

Lines 107
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
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_offer_show($options)
13
{
14
    include_once(XOOPS_ROOT_PATH . '/modules/smartpartner/include/common.php');
15
16
    // Creating the partner handler object
17
    $smartPartnerOfferHandler   = smartpartner_gethandler('offer');
18
    $smartPartnerPartnerHandler = smartpartner_gethandler('partner');
19
20
    include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobjectpermission.php';
21
    $smartPermissionsHandler = new SmartobjectPermissionHandler($smartPartnerPartnerHandler);
22
    //var_dump($smartPermissionsHandler->handler);exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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
    $grantedItems = $smartPermissionsHandler->getGrantedItems('full_view');
24
25
    if (!empty($grantedItems)) {
26
        $criteria = new CriteriaCompo();
27
        $criteria->add(new Criteria('partnerid', '(' . implode(', ', $grantedItems) . ')', 'IN'));
28
        $criteria->add(new Criteria('date_pub', time(), '<'));
29
        $criteria->add(new Criteria('date_end', time(), '>'));
30
        $criteria->add(new Criteria('status', _SPARTNER_STATUS_ONLINE));
31
32
        // Randomize
33
        $offersObj = $smartPartnerOfferHandler->getObjects($criteria);
34
        if (count($offersObj) > 0) {
35
            $key_arr  = array_keys($offersObj);
36
            $key_rand = array_rand($key_arr, 1);
37
            $offerObj = $offersObj[$key_rand];
38
        }
39
40
        $block = array();
41 View Code Duplication
        if (isset($offerObj) && is_object($offerObj)) {
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...
42
            $block['offers'][] = $offerObj->toArray('e');
43
44
            $smartConfig = smartpartner_getModuleConfig();
0 ignored issues
show
Unused Code introduced by
$smartConfig 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...
45
            //$image_info = smartpartner_imageResize($partnerObj->getImagePath(), $smartConfig['img_max_width'], $smartConfig['img_max_height']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
46
47
            if ($options[0] == 1) {
48
                $block['fadeImage'] = 'style="filter:alpha(opacity=20);" onmouseover="nereidFade(this,100,30,5)" onmouseout="nereidFade(this,50,30,5)"';
49
            }
50
51
            $block['see_all']          = $options[2];
52
            $block['lang_see_all']     = _MB_SPARTNER_LANG_SEE_ALL_OFFERS;
53
            $block['smartpartner_url'] = SMARTPARTNER_URL;
54
        }
55
    }
56
57
    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...
58
}
59
60
/**
61
 * @param $options
62
 * @return string
63
 */
64 View Code Duplication
function b_random_offer_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...
65
{
66
    $form = "<table border='0'>";
67
    /*$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...
68
     $chk   = "";
69
     if ($options[0] == 0) {
70
         $chk = " checked='checked'";
71
     }
72
     $form .= "<input type='radio' name='options[0]' value='0'".$chk." />"._NO."";
73
     $chk   = "";
74
     if ($options[0] == 1) {
75
         $chk = " checked='checked'";
76
     }
77
     $form .= "<input type='radio' name='options[0]' value='1'".$chk." />"._YES."</td></tr>";*/
78
    $form .= '<tr><td>' . _MB_SPARTNER_FADE . '</td><td>';
79
    $chk = '';
80
    if ($options[0] == 0) {
81
        $chk = " checked='checked'";
82
    }
83
    $form .= "<input type='radio' name='options[1]' value='0'" . $chk . ' />' . _NO . '';
84
    $chk = '';
85
    if ($options[0] == 1) {
86
        $chk = " checked='checked'";
87
    }
88
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
89
    /*$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...
90
     $chk   = "";
91
     if ($options[2] == 0) {
92
         $chk = " checked='checked'";
93
     }
94
     $form .= "<input type='radio' name='options[2]' value='0'".$chk." />"._NO."";
95
     $chk   = "";
96
     if ($options[2] == 1) {
97
         $chk = " checked='checked'";
98
     }
99
     $form .= "<input type='radio' name='options[2]' value='1'".$chk." />"._YES."</td></tr>";
100
     $form .= "<tr><td>"._MB_SPARTNER_BLIMIT."</td><td>";
101
     $form .= "<input type='text' name='options[3]' size='16' value='".$options[3]."' /></td></tr>";*/
102
    $form .= '<tr><td>' . _MB_SPARTNER_BSHOW . '</td><td>';
103
    $form .= "<select size='1' name='options[1]'>";
104
    $sel = '';
105
    if ($options[1] == 1) {
106
        $sel = " selected='selected'";
107
    }
108
    $form .= "<option value='1' " . $sel . '>' . _MB_SPARTNER_IMAGES . '</option>';
109
    $sel = '';
110
    if ($options[1] == 2) {
111
        $sel = " selected='selected'";
112
    }
113
    $form .= "<option value='2' " . $sel . '>' . _MB_SPARTNER_TEXT . '</option>';
114
    $sel = '';
115
    if ($options[1] == 3) {
116
        $sel = " selected='selected'";
117
    }
118
    $form .= "<option value='3' " . $sel . '>' . _MB_SPARTNER_BOTH . '</option>';
119
    $form .= '</select></td></tr>';
120
    /*$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...
121
     $form .= "<select size='1' name='options[5]'>";
122
     $sel = "";
123
     if ($options[5] == "id") {
124
         $sel = " selected='selected'";
125
     }
126
     $form .= "<option value='id' ".$sel.">"._MB_SPARTNER_ID."</option>";
127
     $sel = "";
128
     if ($options[5] == "hits") {
129
         $sel = " selected='selected'";
130
     }
131
     $form .= "<option value='hits' ".$sel.">"._MB_SPARTNER_HITS."</option>";
132
     $sel = "";
133
     if ($options[5] == "title") {
134
         $sel = " selected='selected'";
135
     }
136
     $form .= "<option value='title' ".$sel.">"._MB_SPARTNER_TITLE."</option>";
137
     if ($options[5] == "weight") {
138
         $sel = " selected='selected'";
139
     }
140
     $form .= "<option value='weight' ".$sel.">"._MB_SPARTNER_WEIGHT."</option>";
141
     $form .= "</select> ";
142
     $form .= "<select size='1' name='options[6]'>";
143
     $sel = "";
144
     if ($options[6] == "ASC") {
145
         $sel = " selected='selected'";
146
     }
147
     $form .= "<option value='ASC' ".$sel.">"._MB_SPARTNER_ASC."</option>";
148
     $sel = "";
149
     if ($options[6] == "DESC") {
150
         $sel = " selected='selected'";
151
     }
152
     $form .= "<option value='DESC' ".$sel.">"._MB_SPARTNER_DESC."</option>";
153
     $form .= "</select></td></tr>";
154
     */
155
    $form .= '<tr><td>' . _MB_SPARTNER_SEE_ALL . '</td><td>';
156
    $chk = '';
157
    if ($options[2] == 0) {
158
        $chk = " checked='checked'";
159
    }
160
    $form .= "<input type='radio' name='options[2]' value='0'" . $chk . ' />' . _NO . '';
161
    $chk = '';
162
    if (isset($options[7]) && $options[7] == 1) {
163
        $chk = " checked='checked'";
164
    }
165
    $form .= "<input type='radio' name='options[2]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
166
167
    $form .= '</table>';
168
169
    return $form;
170
}
171