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
Push — master ( a4ceb7...b12335 )
by Hong
02:39
created

FactoryTrait::matchArguments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 16
rs 9.2
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Di
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Di\Traits;
16
17
use Phossa2\Di\Container;
18
use Phossa2\Di\Exception\LogicException;
19
20
/**
21
 * FactoryTrait
22
 *
23
 * Create service instance here
24
 *
25
 * @package Phossa2\Di
26
 * @author  Hong Zhang <[email protected]>
27
 * @version 2.0.0
28
 * @since   2.0.0 added
29
 */
30
trait FactoryTrait
31
{
32
    /**
33
     * @var    Container
34
     * @access protected
35
     */
36
    protected $master;
37
38
    /**
39
     * Get service definition (and fix it)
40
     *
41
     * @param  string $rawId
42
     * @param  array $args
43
     * @return array
44
     * @access protected
45
     */
46
    protected function getDefinition(
47
        /*# string */ $rawId,
48
        array $args
49
    )/*# : array */ {
50
        // get the definition
51
        $def = $this->master->getResolver()->getService($rawId);
52
53
        // fix class
54
        if (!is_array($def) || !isset($def['class'])) {
55
            $def = ['class' => $def];
56
        }
57
58
        // resolve arguments
59
        if (!empty($args)) {
60
            $this->master->resolve($args);
61
            $def['args'] = $args;
62
        }
63
64
        return (array) $def;
65
    }
66
67
    /**
68
     * Instantiate service object from classname
69
     *
70
     * @param  string $class
71
     * @param  array $args
72
     * @return object
73
     * @throws LogicException if something goes wrong
74
     * @access protected
75
     */
76
    protected function constructObject(/*# string */ $class, array $args)
77
    {
78
        $reflector = new \ReflectionClass($class);
79
        $constructor = $reflector->getConstructor();
80
81
        // not constructor defined
82
        if (is_null($constructor)) {
83
            $obj = $reflector->newInstanceWithoutConstructor();
84
85
        // normal class with constructor
86
        } else {
87
            $args = $this->matchArguments(
88
                $constructor->getParameters(),
89
                $args
90
            );
91
            $obj = $reflector->newInstanceArgs($args);
92
        }
93
94
        return $obj;
95
    }
96
97
    /**
98
     * Match provided arguments with a method/function's reflection parameters
99
     *
100
     * @param  \ReflectionParameter[] $reflectionParameters
101
     * @param  array $providedArguments
102
     * @return array the resolved arguments
103
     * @throws LogicException
104
     * @access protected
105
     */
106
    protected function matchArguments(
107
        array $reflectionParameters,
108
        array $providedArguments
109
    )/*# : array */ {
110
        // result
111
        $resolvedArguments = [];
112
        foreach ($reflectionParameters as $i => $param) {
113
            $class = $param->getClass();
114
            if ($this->isTypeMatched($param, $providedArguments, $class)) {
115
                $resolvedArguments[$i] = array_shift($providedArguments);
116
            } elseif (null !== $class) {
117
                $resolvedArguments[$i] = $this->getObjectByClass($class->getName());
118
            }
119
        }
120
        return array_merge($resolvedArguments, $providedArguments);
121
    }
122
123
    /**
124
     * Is $parameter same type as the $argument ?
125
     *
126
     * @param  \ReflectionParameter $parameter
127
     * @param  array $arguments
128
     * @param  null|string $class
129
     * @return bool
130
     * @access protected
131
     */
132
    protected function isTypeMatched(
133
        \ReflectionParameter $parameter,
134
        array $arguments,
135
        $class
136
    )/*# : bool */ {
137
        if (empty($arguments)) {
138
            return false;
139
        } elseif (null !== $class) {
140
            return is_a($arguments[0], $parameter->getClass()->getName());
141
        } else {
142
            return true;
143
        }
144
    }
145
146
    /**
147
     * Get an object base on provided classname or interface name
148
     *
149
     * @param  string $classname class or interface name
150
     * @return object
151
     * @throws \Exception if something goes wrong
152
     * @access protected
153
     */
154
    protected function getObjectByClass(/*# string */ $classname)
155
    {
156
        // mapping exists
157
        if ($this->master->getResolver()->hasMapping($classname)) {
158
            $classname = $this->master->getResolver()->getMapping($classname);
159
            if (is_object($classname)) {
160
                return $classname;
161
            }
162
        }
163
        return $this->master->getResolver()->get($classname);
164
    }
165
166
    /**
167
     * Get callable parameters
168
     *
169
     * @param  callable $callable
170
     * @return \ReflectionParameter[]
171
     * @throws LogicException if something goes wrong
172
     * @access protected
173
     */
174
    protected function getCallableParameters(callable $callable)/*# : array */
175
    {
176
        // array type
177
        if (is_array($callable)) {
178
            $reflector = new \ReflectionClass($callable[0]);
179
            $method = $reflector->getMethod($callable[1]);
180
181
        // object with __invoke() defined
182
        } elseif ($this->isInvocable($callable)) {
183
            $reflector = new \ReflectionClass($callable);
184
            $method = $reflector->getMethod('__invoke');
185
186
        // simple function
187
        } else {
188
            $method = new \ReflectionFunction($callable);
189
        }
190
191
        return $method->getParameters();
192
    }
193
194
    /**
195
     * Is $var an object with '__invoke()' defined ?
196
     *
197
     * @param  mixed $var
198
     * @return bool
199
     * @access protected
200
     */
201
    protected function isInvocable($var)/*# : bool */
202
    {
203
        return is_object($var) && method_exists($var, '__invoke');
204
    }
205
206
    /**
207
     * Merge different sections of a node
208
     *
209
     * convert
210
     *   `['section1' => [[1], [2]], 'section2' => [[3], [4]]]`
211
     *
212
     * to
213
     *   `[[1], [2], [3], [4]]`
214
     *
215
     * @param  array $nodeData
216
     * @return array
217
     * @access protected
218
     */
219
    protected function mergeMethods(array $nodeData)/*# : array */
220
    {
221
        // no merge
222
        if (empty($nodeData) || isset($nodeData[0])) {
223
            return $nodeData;
224
        }
225
226
        // in sections
227
        $result = [];
228
        foreach ($nodeData as $data) {
229
            $result = array_merge($result, $data);
230
        }
231
        return $result;
232
    }
233
234
    /**
235
     * Execute common methods defined in 'di.common' for all objects
236
     *
237
     * @param  object $object
238
     * @return $this
239
     * @access protected
240
     */
241
    protected function executeCommonBatch($object)
242
    {
243
        $methods = [];
244
245
        // get from 'di.common'
246
        if ($this->master->getResolver()->hasInSection('', 'common')) {
247
            $methods = $this->mergeMethods(
248
                $this->master->getResolver()->getInSection('', 'common')
249
            );
250
        }
251
252
        foreach ($methods as $method) {
253
            if (call_user_func($method[0], $object, $this->master)) {
254
                call_user_func($method[1], $object, $this->master);
255
            }
256
        }
257
        return $this;
258
    }
259
}
260