Completed
Push — dev ( 8c317d...76c4d4 )
by James Ekow Abaka
01:42
created

Bindings::setArrayBinding()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 11.4603

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 1
dl 0
loc 23
ccs 7
cts 15
cp 0.4667
crap 11.4603
rs 8.9297
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\panie;
4
5
/**
6
 * Holds all the bindings for the dependency injection.
7
 */
8
class Bindings
9
{
10
11
    /**
12
     * An array of all the bindings.
13
     *
14
     * @var array
15
     */
16
    private $bindings = [];
17
    
18
    /**
19
     * Key of the binding that would be altered with subsequent calls to a bindings object.
20
     *
21
     * @var string 
22
     */
23
    private $activeKey;
24
25
    /**
26
     * Set the key of the binding to be modified or accessed on subsequent calls.
27
     *
28
     * @param $activeKey
29
     * @return self
30
     */
31 10
    public function setActiveKey(string $activeKey) : self
32
    {
33 10
        $this->activeKey = $activeKey;
34 10
        return $this;
35
    }
36
37
    /**
38
     * Registers a method to be called when an instance is created for the current active binding.
39
     * This method could be a setter or some other optional initialization method. Parameters for methods registered
40
     * with call that have type hints are also injected.
41
     *
42
     * @param string $method
43
     * @param array $parameters
44
     * @return self
45
     */
46 2
    public function call(string $method, array $parameters = []) : self
47
    {
48 2
        $this->bindings[$this->activeKey]['calls'][] = [$method, $parameters];
49 2
        return $this;
50
    }
51
52
    /**
53
     * Provides a concrete class or factory function to which the currently selected binding should be linked.
54
     *
55
     * @param mixed $value
56
     * @return self
57
     */
58 11
    public function to($value)
59
    {
60 11
        if(isset($this->bindings[$this->activeKey])) {
61 1
            $this->bindings[$this->activeKey]['binding'] = $value;
62
        } else {
63 11
            $this->bindings[$this->activeKey] = ['binding' => $value, 'calls' => [], 'properties' => []];
64
        }
65 11
        return $this;
66
    }
67
68
    /**
69
     * Get the configuration of a binding.
70
     *
71
     * @param string $key
72
     * @return mixed
73
     */
74 11
    public function get(string $key)
75
    {
76 11
        $binding = $this->bindings[$key];
77 11
        if(!isset($binding['binding'])) {
78
            $binding['binding'] = $key;
79
        }
80 11
        return $binding;
81
    }
82
83
    /**
84
     * Checks if a binding exists.
85
     *
86
     * @param string $key
87
     * @return bool
88
     */
89 18
    public function has($key)
90
    {
91 18
        return isset($this->bindings[$key]);
92
    }
93
94
    /**
95
     * Register the current active binding as a singleton.
96
     *
97
     * @param bool $singleton
98
     */
99 2
    public function asSingleton($singleton = true)
100
    {
101 2
        $this->bindings[$this->activeKey]['singleton'] = $singleton;
102 2
    }
103
104
    /**
105
     * Setup bindings from an array.
106
     *
107
     * @param array $binding
108
     */
109 1
    private function setArrayBinding(array $binding)
110
    {
111 1
        if(isset($binding[0])) {
112 1
            $this->to($binding[0]);
113
        }
114
        
115 1
        if(isset($binding['calls'])){
116
            foreach($binding['calls'] as $call) {
117
                if(is_string($call)) {
118
                    $method = $call;
119
                    $parameters = [];
120
                } else if(is_array($call)) {
121
                    $method = array_keys($call)[0];
122
                    $parameters = $call[$method];
123
                }
124
                $this->call($method, $parameters);
0 ignored issues
show
Bug introduced by
The variable $method 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...
Bug introduced by
The variable $parameters 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...
125
            }
126
        } else {
127 1
            $this->bindings[$this->activeKey]['calls'] = [];
128
        }
129
130 1
        $this->asSingleton($binding['singleton'] ?? false);
131 1
    }
132
133
    /**
134
     * Merge current bindings with another configuration overwriting conflicting values with the new bindings.
135
     *
136
     * @param array $bindings
137
     */
138 4
    public function merge(array $bindings)
139
    {
140 4
        foreach($bindings as $key => $binding) {
141 4
            $this->activeKey = $key;
142 4
            if(is_array($binding)) {
143 1
                $this->setArrayBinding($binding);
144
            } else {
145 4
                $this->bindings[$key] = ['binding' => $binding, 'calls' => [], 'properties' => []];
146
            }
147
        }
148 4
    }
149
150
}
151