scrolling_partner.php ➔ b_scrolling_partner_edit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
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_scrolling_partner_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 Comprehensibility introduced by
56% 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...
20
21
    // Randomize
22
    $partnersObj = $smartPartnerPartnerHandler->getPartners(0, 0, _SPARTNER_STATUS_ACTIVE);
23 View Code Duplication
    if (count($partnersObj) > 1) {
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...
24
        $key_arr  = array_keys($partnersObj);
25
        $key_rand = array_rand($key_arr, count($key_arr));
26
        for ($i = 0; ($i < count($partnersObj)) && (($options[0] == 0) || ($i < $options[0])); ++$i) {
27
            $newObjs[$i] = $partnersObj[$key_rand[$i]];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$newObjs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $newObjs = 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...
28
        }
29
        $partnersObj = $newObjs;
0 ignored issues
show
Bug introduced by
The variable $newObjs 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
    }
31
    /*  $cat_id = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
32
            foreach ($partnersObj as $partnerObj) {
33
                $p_cats = $partnerObj->categoryid();
34
                $p_cat_rand = array_rand($p_cats);
35
36
                if (!in_array($p_cats[$p_cat_rand],$cat_id)) {
37
                    $cat_id[] = $p_cats[$p_cat_rand];
38
                }
39
            }
40
    */
41
    $block = array();
42
    if ($partnersObj) {
43
        for ($i = 0, $iMax = count($partnersObj); $i < $iMax; ++$i) {
44
            if ($partnersObj[$i]->image() != '' && $partnersObj[$i]->image() !== 'blank.png') {
45
                //$partner['id'] = $partnersObj[$i]->id();
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...
46
                $partner['urllink']  = $partnersObj[$i]->getUrlLink('block');
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...
47
                $partner['image']    = $partnersObj[$i]->getImageUrl();
0 ignored issues
show
Bug introduced by
The variable $partner 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...
48
                $partner['title']    = $partnersObj[$i]->title();
49
                $smartConfig         = smartpartner_getModuleConfig();
50
                $image_info          = smartpartner_imageResize($partnersObj[$i]->getImagePath(), $smartConfig['img_max_width'], $smartConfig['img_max_height']);
0 ignored issues
show
Unused Code introduced by
$image_info 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...
51
                $block['partners'][] = $partner;
52
            }
53
        }
54
    }
55
    $block['width']      = $options[1];
56
    $block['height']     = $options[2];
57
    $block['speed']      = $options[3];
58
    $block['space']      = $options[4];
59
    $block['background'] = isset($options[5]) && $options[5] != '' ? $options[5] : 'FFFFFF';
60
61
    return $block;
62
}
63
64
/**
65
 * @param $options
66
 * @return string
67
 */
68
function b_scrolling_partner_edit($options)
69
{
70
    $form = "<table border='0'>";
71
    $form .= '<tr><td>' . _MB_SPARTNER_BLIMIT . '</td><td>';
72
    $form .= "<input type='text' name='options[0]' size='16' value='" . $options[0] . "' /></td></tr>";
73
    $form .= '<tr><td>' . _MB_SPARTNER_BWIDTH . '</td><td>';
74
    $form .= "<input type='text' name='options[1]' size='16' value='" . $options[1] . "' /></td></tr>";
75
    $form .= '<tr><td>' . _MB_SPARTNER_BHEIGHT . '</td><td>';
76
    $form .= "<input type='text' name='options[2]' size='16' value='" . $options[2] . "' /></td></tr>";
77
    $form .= '<tr><td>' . _MB_SPARTNER_BSPEED . '</td><td>';
78
    $form .= "<input type='text' name='options[3]' size='16' value='" . $options[3] . "' /></td></tr>";
79
    $form .= '<tr><td>' . _MB_SPARTNER_BSPACE . '</td><td>';
80
    $form .= "<input type='text' name='options[4]' size='16' value='" . $options[4] . "' /></td></tr>";
81
    $form .= '<tr><td>' . _MB_SPARTNER_BBG . '</td><td>';
82
    $form .= "<input type='text' name='options[5]' size='16' value='" . (isset($options[5]) ? $options[5] : '') . "' /></td></tr>";
83
84
    $form .= '</table>';
85
86
    return $form;
87
}
88