Completed
Push — master ( 32b6c8...141c37 )
by Scott
02:39
created

DriverConfig::getShippingDriverDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php namespace Bedard\Shop\FormWidgets;
2
3
use Backend\Classes\FormWidgetBase;
4
use Bedard\Shop\Classes\DriverManager;
5
6
/**
7
 * DriverConfig Form Widget.
8
 */
9
class DriverConfig extends FormWidgetBase
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected $defaultAlias = 'bedard_shop_driver_config';
15
16
    /**
17
     * @var \Bedard\Shop\Classes\DriverManager;
18
     */
19
    protected $manager;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function init()
25
    {
26
        $this->manager = DriverManager::instance();
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function render()
33
    {
34
        $this->prepareVars();
35
36
        return $this->makePartial('driverconfig');
37
    }
38
39
    /**
40
     * Prepares the form widget view data.
41
     */
42
    public function prepareVars()
43
    {
44
        $this->vars['drivers'] = $this->getShippingDriverDetails();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function loadAssets()
51
    {
52
        $this->addJs('/plugins/bedard/shop/assets/dist/vendor.js');
53
        $this->addJs('/plugins/bedard/shop/assets/dist/driverconfig.js', 'Bedard.Shop');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getSaveValue($value)
60
    {
61
        return $value;
62
    }
63
64
    /**
65
     * Get the driver details.
66
     *
67
     * @return array
68
     */
69
    protected function getShippingDriverDetails()
70
    {
71
        foreach ($this->manager->getShippingDrivers() as $driver) {
72
            $details[get_class($driver)] = $driver->driverDetails();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$details was never initialized. Although not strictly required by PHP, it is generally a good practice to add $details = 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...
73
        }
74
75
        return $details;
0 ignored issues
show
Bug introduced by
The variable $details 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...
76
    }
77
}
78