Completed
Push — master ( 141c37...4e2b2d )
by Scott
04:28
created

DriverConfig::onLoadDriverSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php namespace Bedard\Shop\FormWidgets;
2
3
use Backend\Classes\FormWidgetBase;
4
use Bedard\Shop\Classes\DriverManager;
5
use Bedard\Shop\Interfaces\DriverInterface;
6
use Model;
7
8
/**
9
 * DriverConfig Form Widget.
10
 */
11
class DriverConfig extends FormWidgetBase
12
{
13
    use \Bedard\Shop\Traits\LangJsonable;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $defaultAlias = 'bedard_shop_driver_config';
19
20
    /**
21
     * @var \Bedard\Shop\Classes\DriverManager;
22
     */
23
    protected $manager;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function init()
29
    {
30
        $this->manager = DriverManager::instance();
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function render()
37
    {
38
        $this->prepareVars();
39
40
        return $this->makePartial('driverconfig');
41
    }
42
43
    /**
44
     * Prepares the form widget view data.
45
     */
46
    public function prepareVars()
47
    {
48
        $this->vars['drivers'] = $this->getShippingDriverDetails();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function loadAssets()
55
    {
56
        $this->addJs('/plugins/bedard/shop/assets/dist/vendor.js');
57
        $this->addJs('/plugins/bedard/shop/assets/dist/driverconfig.js', 'Bedard.Shop');
58
    }
59
60
    /**
61
     * Get the form for a driver.
62
     *
63
     * @param  DriverInterface $driver
64
     * @return object
65
     */
66
    protected function getDriverForm(DriverInterface $driver)
67
    {
68
        $model = new Model;
69
        // foreach (array_merge(array_keys($fields), array_keys($tabFields)) as $key) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
70
        //     $model->$key = $driver->getConfig($key);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
71
        // }
72
73
        $form = $this->makeConfigFromArray($driver->getFormFields());
74
        $form->model = $model;
75
76
        return $form;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getSaveValue($value)
83
    {
84
        return $value;
85
    }
86
87
    /**
88
     * Get the driver details.
89
     *
90
     * @return array
91
     */
92
    protected function getShippingDriverDetails()
93
    {
94
        foreach ($this->manager->getShippingDrivers() as $driver) {
95
            $details[]  = [
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...
96
                "class" => get_class($driver),
97
                "details" => $driver->driverDetails(),
98
            ];
99
        }
100
101
        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...
102
    }
103
104
    /**
105
     * Load the driver settings.
106
     *
107
     * @return string
108
     */
109
    public function onLoadDriverSettings()
110
    {
111
        $input = input('driver');
112
        $driver = new $input['class'];
113
114
        $form = $this->getDriverForm($driver);
115
116
        return $this->makePartial('popup', [
117
            'driver' => $driver,
118
            'form' => $this->makeWidget('Backend\Widgets\Form', $form)->render(),
119
        ]);
120
    }
121
}
122