Completed
Push — master ( c383d4...4bbc60 )
by Scott
02:41
created

DriverManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 107
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A getPlugins() 0 8 2
A getShippingDrivers() 0 8 2
B registerShippingDrivers() 0 22 4
A validateDriverDetails() 0 10 4
1
<?php namespace Bedard\Shop\Classes;
2
3
use Exception;
4
use System\Classes\PluginManager;
5
6
class DriverManager
7
{
8
    use \October\Rain\Support\Traits\Singleton;
9
10
    /**
11
     * @var array   List of plugins.
12
     */
13
    private $plugins;
14
15
    /**
16
     * @var System\Classes\PluginManager
17
     */
18
    private $pluginManager;
19
20
    /**
21
     * @var array   List of payment drivers.
22
     */
23
    private $paymentDrivers;
0 ignored issues
show
Unused Code introduced by
The property $paymentDrivers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
    /**
26
     * @var array   List of shipping drivers.
27
     */
28
    private $shippingDrivers;
29
30
    /**
31
     * Initialize this singleton.
32
     */
33
    protected function init()
34
    {
35
        $this->pluginManager = PluginManager::instance();
36
    }
37
38
    /**
39
     * Return all plugins.
40
     *
41
     * @return array
42
     */
43
    protected function getPlugins()
44
    {
45
        if (! $this->plugins) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->plugins of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
46
            $this->plugins = $this->pluginManager->getPlugins();
47
        }
48
49
        return $this->plugins;
50
    }
51
52
    /**
53
     * Return a list of shipping drivers.
54
     *
55
     * @return array
56
     */
57
    public function getShippingDrivers()
58
    {
59
        if (! $this->shippingDrivers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->shippingDrivers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
60
            $this->registerShippingDrivers();
61
        }
62
63
        return $this->shippingDrivers;
64
    }
65
66
    /**
67
     * Register all shipping drivers.
68
     *
69
     * @return void
70
     */
71
    protected function registerShippingDrivers()
72
    {
73
        $plugins = $this->getPlugins();
74
75
        foreach ($plugins as $id => $plugin) {
76
            if (! method_exists($plugin, 'registerShippingDrivers')) {
77
                continue;
78
            }
79
80
            $pluginDrivers = $plugin->registerShippingDrivers();
0 ignored issues
show
Unused Code introduced by
$pluginDrivers 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...
81
82
            foreach ($plugin->registerShippingDrivers() as $driverClass) {
83
                $driver = new $driverClass;
84
                $details = $driver->driverDetails();
85
                $this->validateDriverDetails($details, $driverClass);
86
87
                $drivers[] = $driver;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$drivers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $drivers = 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...
88
            }
89
        }
90
91
        $this->shippingDrivers = $drivers;
0 ignored issues
show
Bug introduced by
The variable $drivers 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...
92
    }
93
94
    /**
95
     * Validate driver details.
96
     *
97
     * @param  array        $details
98
     * @param  string       $driverClass
99
     * @throws \Exception
100
     * @return void
101
     */
102
    protected function validateDriverDetails($details, $driverClass)
103
    {
104
        if (! is_array($details)) {
105
            throw new Exception('An array must be returned from the driverDetails() method in '. $driverClass . '.');
106
        }
107
108
        if (! array_key_exists('name', $details) || ! is_string($details['name'])) {
109
            throw new Exception('A valid name must be returned from the driverDetails() method in ' . $driverClass . '.');
110
        }
111
    }
112
}
113