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
03:31 queued 01:48
created

Container::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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 (isset($this->definitions[$alias])) return;
42
43
        if (is_null($concrete)) $concrete = $alias;
44
45
        $this->definitions[$alias] = $this->build($concrete);
46
    }
47
48
    /**
49
     * Check if item is available in container
50
     * @param  string  $alias
51
     * @return boolean
52
     */
53
    public function has($alias)
54
    {
55
        if (array_key_exists($alias, $this->definitions)) {
56
            return true;
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * Get an item of the container
64
     * @param  string $alias
65
     * @return mixed
66
     */
67
    public function get($alias)
68
    {
69
        return $this->definitions[$alias];
70
    }
71
72
    /**
73
     * Call method with given parameters
74
     * @param  string|callable  $action
75
     * @param  array            $args
76
     * @return mixed
77
     */
78
    public function call ($action, array $args = [])
79
    {
80
        $method = $action;
81
        $class = null;
82
83
        if (is_string($action)) {
84
            list($class, $method) = explode('@', $action);
85
        }
86
87
        $reflection = $this->getActionReflection($method, $class);
88
        $args       = $this->getParametersDictionary($args);
89
        $parameters = $this->getParameters($reflection, $args);
90
91
        if (is_callable($method)) {
92
            return $method(...$parameters);
93
        }
94
95
        $class = $this->build($class);
96
        return $class->{$method}(...$parameters);
97
    }
98
99
    /**
100
     * Get reflection from an action
101
     * @param string      $method
102
     * @param string|null $class
103
     * @return callable
104
     */
105
    public function getActionReflection($method, $class = null)
106
    {
107
        if(!is_null($class)) {
108
            return new ReflectionMethod($class, $method);
109
        }
110
111
        return new ReflectionFunction($method);
112
    }
113
114
    /**
115
     * Find object and set classname alias on argument list
116
     * @param  array  $args
117
     * @return array
118
     */
119
    public function getParametersDictionary(array $args = [])
120
    {
121
        $dictionary = [];
122
123
        foreach ($args as $parameter) {
124
            if (!is_object($parameter)) continue;
125
            $dictionary[get_class($parameter)] = $parameter;
126
        }
127
128
        return array_merge($args, $dictionary);
129
    }
130
131
    /**
132
     * Get reflection parameters
133
     * @param  ReflectionFunctionAbstract $reflection
134
     * @param  array                      $args
135
     * @return array
136
     */
137
    public function getParameters(ReflectionFunctionAbstract $reflection, array $args = [])
138
    {
139
        $dependencies = $reflection->getParameters();
140
        $parameters = [];
141
142
        foreach ($dependencies as $parameter) {
143
            $class = $parameter->getClass();
144
145
            if ($class && $this->has($class->name)) {
146
                $parameters[] = $this->get($class->name);
147
                continue;
148
            }
149
150
            if ($class && array_key_exists($class->name, $args)) {
151
                $parameters[] = $args[$class->name];
152
                continue;
153
            }
154
155
            if (array_key_exists($parameter->name, $args)) {
156
                $parameters[] = $args[$parameter->name];
157
            }
158
        }
159
160
        return $parameters;
161
    }
162
163
    /**
164
     * Create a new container for asked class
165
     * @param  string $class
166
     * @return Container
167
     */
168
    public function build ($class)
169
    {
170
        $reflection = new ReflectionClass($class);
171
        $parameters = [];
172
173
        if (!is_null($reflection->getConstructor())) {
174
            $parameters = $this->getParameters($reflection->getConstructor());
175
        }
176
177
        return new $class(...$parameters);
178
    }
179
}