Completed
Push — master ( 472b13...fcc7b0 )
by James Ekow Abaka
01:33
created

Bindings::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     * @var array
14
     */
15
    private $bindings = [];
16
    
17
    /**
18
     * An active key that would be altered with subsequent calls to the bindings object.
19
     * @var string 
20
     */
21
    private $activeKey;
22
23 5
    public function setActiveKey($activeKey)
24
    {
25 5
        $this->activeKey = $activeKey;
26 5
        return $this;
27
    }
28
29
    public function call($method, $parameters = [])
30
    {
31
        $this->bindings[$this->activeKey]['calls'][$method] = $parameters;
32
    }
33
34
    public function setProperty($property, $binding)
35
    {
36
        $this->bindings[$this->activeKey]['sets'][$property] = $binding;
37
    }
38
39 5
    public function to($value)
40
    {
41 5
        if(isset($this->bindings[$this->activeKey])) {
42
            $this->bindings[$this->activeKey]['binding'] = $value;
43
        } else {
44 5
            $this->bindings[$this->activeKey] = ['binding' => $value, 'calls' => [], 'properties' => []];
45
        }
46 5
        return $this;
47
    }
48
49 5
    public function get($key)
50
    {
51 5
        return $this->bindings[$key];
52
    }
53
54 7
    public function has($key)
55
    {
56 7
        return isset($this->bindings[$key]);
57
    }
58
59 1
    public function asSingleton($singleton = true)
60
    {
61 1
        $this->bindings[$this->activeKey]['singleton'] = $singleton;
62 1
    }    
63
    
64
    private function setArrayBinding($binding)
65
    {
66
        if(isset($binding[0])) {
67
            $this->to($binding[0]);
68
        }
69
        
70 View Code Duplication
        if(isset($binding['calls'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            foreach($binding['calls'] as $call => $parameters) {
72
                if(is_numeric($call)) {
73
                    $call = $parameters;
74
                    $parameters = [];
75
                } 
76
                $this->call($call, $parameters);
77
            }
78
        } else {
79
            $this->bindings[$this->activeKey]['calls'] = [];
80
        }
81
        
82 View Code Duplication
        if(isset($binding['sets'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            foreach($binding['sets'] as $property => $parameters) {
84
                if(is_numeric($property)) {
85
                    $property = $parameters;
86
                    $parameters = [];
87
                } 
88
                $this->setProperty($property, $parameters);
89
            }
90
        } else {
91
            $this->bindings[$this->activeKey]['sets'] = [];
92
        }
93
94
        $this->asSingleton($binding['singleton'] ?? false);
95
    }
96
97
    public function merge($bindings)
98
    {
99
        foreach($bindings as $key => $binding) {
100
            $this->activeKey = $key;
1 ignored issue
show
Documentation Bug introduced by
It seems like $key can also be of type integer. However, the property $activeKey is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
101
            if(is_array($binding)) {
102
                $this->setArrayBinding($binding);
103
            } else {
104
                $this->bindings[$key] = ['binding' => $binding, 'calls' => [], 'properties' => []];
105
            }
106
        }
107
    }
108
109
}
110