GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#13)
by Jérémy
04:00 queued 02:10
created

Container::add()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
1
<?php
2
3
namespace CapMousse\ReactRestify\Container;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use ReflectionFunction;
8
use ReflectionFunctionAbstract;
9
10
class Container
11
{
12
    private static $instance;
13
14
    /**
15
     * Resolved types
16
     * @var array
17
     */
18
    protected $definitions = [];
19
20
    /**
21
     * Globally available container
22
     * 
23
     * @return static
24
     */
25
    public static function getInstance()
26
    {
27
        if (is_null(self::$instance)) {
28
            self::$instance = new static;
29
        }
30
31
        return self::$instance;
32
    }
33
34
    /**
35
     * Add item to the container
36
     * @param string $alias    
37
     * @param mixed|null $concrete
38
     */
39
    public function add($alias, $concrete = null)
40
    {
41
        if (is_null($concrete)) {
42
            $concrete = $alias;
43
        }
44
45
        $instance = $this->build($concrete);
46
47
        if (!$instance) {
48
            $this->definitions[$alias] = $instance;
49
        }
50
    }
51
52
    /**
53
     * Check if item is available in container
54
     * @param  string  $alias
55
     * @return boolean
56
     */
57
    public function has($alias)
58
    {
59
        if (array_key_exists($alias, $this->definitions)) {
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Get an item of the container
68
     * @param  string $alias 
69
     * @return mixed
70
     */
71
    public function get($alias)
72
    {
73
        return $this->definitions[$alias];
74
    }
75
76
    /**
77
     * Call method with given parameters
78
     * @param  string|callable  $action     
79
     * @param  array            $args 
80
     * @return mixed
81
     */
82
    public function call ($action, array $args = [])
83
    {
84
        $method = $action;
85
        $class = null;
86
87
        if (is_string($action)) {
88
            list($class, $method) = explode('@', $action);
89
        }
90
91
        $reflection = $this->getActionReflection($method, $class);
92
        $args       = $this->getParametersDictionary($args);
93
        $parameters = $this->getParameters($reflection, $args);
94
95
        if (is_callable($method)) {
96
            return $method(...$parameters);
97
        }
98
99
        $class = $this->build($class);
100
        return $class->{$method}(...$parameters);
101
    }
102
103
    /**
104
     * Get reflection from an action
105
     * @param string      $method
106
     * @param string|null $class
107
     * @return callable
108
     */
109
    public function getActionReflection($method, $class = null)
110
    {
111
        if(!is_null($class)) {
112
            return new ReflectionMethod($class, $method);
113
        }
114
115
        return new ReflectionFunction($method);
116
    }
117
118
    /**
119
     * Find object and set classname alias on argument list
120
     * @param  array  $args 
121
     * @return array
122
     */
123
    public function getParametersDictionary(array $args = [])
124
    {
125
        $dictionary = [];
126
127
        foreach ($args as $parameter) {
128
            if (!is_object($parameter)) continue;
129
            $dictionary[get_class($parameter)] = $parameter;
130
        }
131
132
        return array_merge($args, $dictionary);
133
    }
134
135
    /**
136
     * Get reflection parameters
137
     * @param  ReflectionFunctionAbstract $reflection 
138
     * @param  array                      $args       
139
     * @return array                                 
140
     */
141
    public function getParameters(ReflectionFunctionAbstract $reflection, array $args = [])
142
    {
143
        $dependencies = $reflection->getParameters();
144
        $parameters = [];
145
146
        foreach ($dependencies as $parameter) {
147
            $class = $parameter->getClass();
148
149
            if ($class && $this->has($class->name)) {
150
                $parameters[] = $this->get($class->name);
151
                continue;
152
            }
153
154
            if ($class && array_key_exists($class->name, $args)) {
155
                $parameters[] = $args[$class->name];
156
                continue;
157
            }
158
159
            if (array_key_exists($parameter->name, $args)) {
160
                $parameters[] = $args[$parameter->name];
161
            }
162
        }
163
164
        return $parameters;
165
    }
166
167
    /**
168
     * Create a new container for asked class
169
     * @param  string $class
170
     * @return Container
171
     */
172
    public function build ($class)
173
    {
174
        $reflection = new ReflectionClass($class);
175
        $parameters = [];
176
177
        if (!is_null($reflection->getConstructor())) {
178
            $parameters = $this->getParameters($reflection->getConstructor());
179
        }
180
181
        return new $class(...$parameters);
182
    }
183
}