recent_offers.php ➔ b_recent_offers_edit()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 107
Code Lines 24

Duplication

Lines 107
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 16
nop 1
dl 107
loc 107
rs 8.1463
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_offers_show($options)
14
{
15
    include_once(XOOPS_ROOT_PATH . '/modules/smartpartner/include/common.php');
16
17
    // Creating the partner handler object
18
    $offerHandler   = smartpartner_gethandler('offer');
19
    $partnerHandler = smartpartner_gethandler('partner');
20
21
    include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobjectpermission.php';
22
    $smartPermissionsHandler = new SmartobjectPermissionHandler($partnerHandler);
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
        $criteria->setSort('date_sub');
32
        $criteria->setOrder('DESC');
33
        $criteria->setLimit($options[2]);
34
35
        $offersObj = $offerHandler->getObjects($criteria);
36
        $block     = array();
37 View Code Duplication
        if ($offersObj) {
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...
38
            foreach ($offersObj as $offerObj) {
39
                $block['offers'][] = $offerObj->toArray('e');
40
            }
41
            $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...
42
            //$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...
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']          = 1;
49
            $block['lang_see_all']     = _MB_SPARTNER_LANG_SEE_ALL_OFFERS;
50
            $block['smartpartner_url'] = SMARTPARTNER_URL;
51
        }
52
    }
53
54
    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...
55
}
56
57
/**
58
 * @param $options
59
 * @return string
60
 */
61 View Code Duplication
function b_recent_offers_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...
62
{
63
    $form = "<table border='0'>";
64
    $form .= '<tr><td>' . _MB_SPARTNER_PARTNERS_PSPACE . '</td><td>';
65
    $chk = '';
66
    if ($options[0] == 0) {
67
        $chk = " checked='checked'";
68
    }
69
    $form .= "<input type='radio' name='options[0]' value='0'" . $chk . ' />' . _NO . '';
70
    $chk = '';
71
    if ($options[0] == 1) {
72
        $chk = " checked='checked'";
73
    }
74
    $form .= "<input type='radio' name='options[0]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
75
    $form .= '<tr><td>' . _MB_SPARTNER_FADE . '</td><td>';
76
    $chk = '';
77
    if ($options[1] == 0) {
78
        $chk = " checked='checked'";
79
    }
80
    $form .= "<input type='radio' name='options[1]' value='0'" . $chk . ' />' . _NO . '';
81
    $chk = '';
82
    if ($options[1] == 1) {
83
        $chk = " checked='checked'";
84
    }
85
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' />' . _YES . '</td></tr>';
86
    /*$form .= "<tr><td>"._MB_SPARTNER_BRAND."</td><td>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
87
     $chk   = "";
88
     if ($options[2] == 0) {
89
         $chk = " checked='checked'";
90
     }*/
91
    /*$form .= "<input type='radio' name='options[2]' value='0'".$chk." />"._NO."";
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...
92
     $chk   = "";
93
     if ($options[2] == 1) {
94
         $chk = " checked='checked'";
95
     }
96
     $form .= "<input type='radio' name='options[2]' value='1'".$chk." />"._YES."</td></tr>";*/
97
    $form .= '<tr><td>' . _MB_SPARTNER_BLIMIT . '</td><td>';
98
    $form .= "<input type='text' name='options[2]' size='16' value='" . $options[2] . "' /></td></tr>";
99
    /*$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...
100
     $form .= "<select size='1' name='options[3]'>";
101
     $sel = "";
102
     if ($options[3] == 1) {
103
         $sel = " selected='selected'";
104
     }
105
     $form .= "<option value='1' ".$sel.">"._MB_SPARTNER_IMAGES."</option>";
106
     $sel = "";
107
     if ($options[3] == 2) {
108
         $sel = " selected='selected'";
109
     }
110
     $form .= "<option value='2' ".$sel.">"._MB_SPARTNER_TEXT."</option>";
111
     $sel = "";
112
     if ($options[3] == 3) {
113
         $sel = " selected='selected'";
114
     }
115
     $form .= "<option value='3' ".$sel.">"._MB_SPARTNER_BOTH."</option>";
116
     $form .= "</select></td></tr>";
117
     $form .= "<tr><td>"._MB_SPARTNER_BORDER."</td><td>";
118
     $form .= "<select size='1' name='options[5]'>";
119
     $sel = "";
120
     if ($options[4] == "id") {
121
         $sel = " selected='selected'";
122
     }
123
     $form .= "<option value='id' ".$sel.">"._MB_SPARTNER_ID."</option>";
124
     $sel = "";
125
     if ($options[4] == "hits") {
126
         $sel = " selected='selected'";
127
     }
128
     $form .= "<option value='hits' ".$sel.">"._MB_SPARTNER_HITS."</option>";
129
     $sel = "";
130
     if ($options[4] == "title") {
131
         $sel = " selected='selected'";
132
     }
133
     $form .= "<option value='title' ".$sel.">"._MB_SPARTNER_TITLE."</option>";
134
     if ($options[4] == "weight") {
135
         $sel = " selected='selected'";
136
     }
137
     $form .= "<option value='weight' ".$sel.">"._MB_SPARTNER_WEIGHT."</option>";
138
     $form .= "</select> ";
139
     $form .= "<select size='1' name='options[6]'>";
140
     $sel = "";
141
     if ($options[5] == "ASC") {
142
         $sel = " selected='selected'";
143
     }
144
     $form .= "<option value='ASC' ".$sel.">"._MB_SPARTNER_ASC."</option>";
145
     $sel = "";
146
     if ($options[5] == "DESC") {
147
         $sel = " selected='selected'";
148
     }
149
     $form .= "<option value='DESC' ".$sel.">"._MB_SPARTNER_DESC."</option>";
150
     $form .= "</select></td></tr>";
151
152
     $form .= "<tr><td>"._MB_SPARTNER_SEE_ALL."</td><td>";
153
     $chk   = "";
154
     if ($options[6] == 0) {
155
         $chk = " checked='checked'";
156
     }
157
     $form .= "<input type='radio' name='options[7]' value='0'".$chk." />"._NO."";
158
     $chk   = "";
159
     if ($options[6] == 1) {
160
         $chk = " checked='checked'";
161
     }
162
     $form .= "<input type='radio' name='options[7]' value='1'".$chk." />"._YES."</td></tr>";*/
163
164
    $form .= '</table>';
165
166
    return $form;
167
}
168