Completed
Push — master ( 88111b...b3fce7 )
by Michael
05:58
created

Plugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartobject;
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    XOOPS Project https://xoops.org/
15
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @package
17
 * @since
18
 * @author     XOOPS Development Team
19
 */
20
21
use XoopsModules\Smartobject;
22
23
/**
24
 * Class SmartPlugin
25
 */
26
class Plugin
27
{
28
    public $_infoArray;
29
30
    /**
31
     * SmartPlugin constructor.
32
     * @param $array
33
     */
34
    public function __construct($array)
35
    {
36
        $this->_infoArray = $array;
37
    }
38
39
    /**
40
     * @param $item
41
     * @return bool
42
     */
43
    public function getItemInfo($item)
44
    {
45
        if (isset($this->_infoArray['items'][$item])) {
46
            return $this->_infoArray['items'][$item];
47
        } else {
48
            return false;
49
        }
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function getItemList()
56
    {
57
        $itemsArray = $this->_infoArray['items'];
58
        foreach ($itemsArray as $k => $v) {
59
            $ret[$k] = $v['caption'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = 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...
60
        }
61
62
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret 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...
63
    }
64
65
    /**
66
     * @return bool|int|string
67
     */
68
    public function getItem()
69
    {
70
        $ret = false;
71
        foreach ($this->_infoArray['items'] as $k => $v) {
72
            $search_str = str_replace('%u', '', $v['url']);
73
            if (strpos($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'], $search_str) > 0) {
74
                $ret = $k;
75
                break;
76
            }
77
        }
78
79
        return $ret;
80
    }
81
82
    /**
83
     * @param $item
84
     * @return mixed
85
     */
86
    public function getItemIdForItem($item)
87
    {
88
        return $_REQUEST[$this->_infoArray['items'][$item]['request']];
89
    }
90
}
91