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 ( 15625b...b1045e )
by Hong
02:42
created

FactoryHelperTrait::mergeMethods()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 1
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\Factory;
16
17
use Phossa2\Di\Message\Message;
18
use Phossa2\Di\Resolver\ObjectResolver;
19
use Phossa2\Di\Exception\LogicException;
20
use Phossa2\Di\Traits\ResolverAwareTrait;
21
22
/**
23
 * FactoryHelperTrait
24
 *
25
 * Create service instance here
26
 *
27
 * @package Phossa2\Di
28
 * @author  Hong Zhang <[email protected]>
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
trait FactoryHelperTrait
33
{
34
    use ResolverAwareTrait;
35
36
    /**
37
     * Get service definition
38
     *
39
     * @param  string $rawId
40
     * @param  array $args
41
     * @return array
42
     * @access protected
43
     */
44
    protected function getDefinition(
45
        /*# string */ $rawId,
46
        array $args
47
    )/*# : array */ {
48
        // get the definition
49
        $def = $this->getResolver()->getService($rawId);
50
51
        // fix class
52
        if (!is_array($def) || !isset($def['class'])) {
53
            $def = ['class' => $def];
54
        }
55
56
        // add arguments
57
        if (!empty($args)) {
58
            $def['args'] = $args;
59
        }
60
61
        return (array) $def;
62
    }
63
64
    /**
65
     * Instantiate service object from classname
66
     *
67
     * @param  string $class
68
     * @param  array $args
69
     * @return object
70
     * @throws LogicException if something goes wrong
71
     * @access protected
72
     */
73
    protected function constructObject(/*# string */ $class, array $args)
74
    {
75
        $reflector = new \ReflectionClass($class);
76
        $constructor = $reflector->getConstructor();
77
78
        // not constructor defined
79
        if (is_null($constructor)) {
80
            $obj = $reflector->newInstanceWithoutConstructor();
81
82
        // normal class with constructor
83
        } else {
84
            $args = $this->matchArguments($constructor->getParameters(), $args);
85
            $obj = $reflector->newInstanceArgs($args);
86
        }
87
88
        return $obj;
89
    }
90
91
    /**
92
     * Match provided arguments with a method/function's reflection parameters
93
     *
94
     * @param  \ReflectionParameter[] $reflectionParameters
95
     * @param  array $providedArguments
96
     * @return array the resolved arguments
97
     * @throws LogicException
98
     * @access protected
99
     */
100
    protected function matchArguments(
101
        array $reflectionParameters,
102
        array $providedArguments
103
    )/*# : array */ {
104
        // result
105
        $resolvedArguments = [];
106
        foreach ($reflectionParameters as $i => $param) {
107
            $class = $param->getClass();
108
109
            if ($this->isTypeMatched($class, $providedArguments)) {
110
                $resolvedArguments[$i] = array_shift($providedArguments);
111
112
            } elseif ($this->isRequiredClass($param, $providedArguments)) {
113
                $resolvedArguments[$i] = $this->getObjectByClass($class->getName());
114
            }
115
        }
116
        return array_merge($resolvedArguments, $providedArguments);
117
    }
118
119
    /**
120
     * Try best to guess parameter and argument are the same type
121
     *
122
     * @param  null|\ReflectionClass $class
123
     * @param  array $arguments
124
     * @return bool
125
     * @access protected
126
     */
127
    protected function isTypeMatched($class, array $arguments)/*# : bool */
128
    {
129
        if (empty($arguments)) {
130
            return false;
131
        } elseif (null !== $class) {
132
            return is_a($arguments[0], $class->getName());
133
        } else {
134
            return true;
135
        }
136
    }
137
138
    /**
139
     * Is $param required and is a class/interface
140
     *
141
     * @param  \ReflectionParameter $param
142
     * @param  array $arguments
143
     * @return bool
144
     * @throws LogicException if mismatched arguments
145
     * @access protected
146
     */
147
    protected function isRequiredClass(
148
        \ReflectionParameter $param,
149
        array $arguments
150
    )/*# : bool */ {
151
        $optional = $param->isOptional();
152
        if ($param->getClass()) {
153
            return !$optional || !empty($arguments);
154
        } else {
155
            return false;
156
        }
157
    }
158
159
    /**
160
     * Get callable parameters
161
     *
162
     * @param  callable $callable
163
     * @return \ReflectionParameter[]
164
     * @throws LogicException if something goes wrong
165
     * @access protected
166
     */
167
    protected function getCallableParameters(callable $callable)/*# : array */
168
    {
169
        // array type
170
        if (is_array($callable)) {
171
            $reflector = new \ReflectionClass($callable[0]);
172
            $method = $reflector->getMethod($callable[1]);
173
174
        // object with __invoke() defined
175
        } elseif ($this->isInvocable($callable)) {
176
            $reflector = new \ReflectionClass($callable);
177
            $method = $reflector->getMethod('__invoke');
178
179
        // simple function
180
        } else {
181
            $method = new \ReflectionFunction($callable);
182
        }
183
184
        return $method->getParameters();
185
    }
186
187
    /**
188
     * Is $var a non-closure object with '__invoke()' defined ?
189
     *
190
     * @param  mixed $var
191
     * @return bool
192
     * @access protected
193
     */
194
    protected function isInvocable($var)/*# : bool */
195
    {
196
        return is_object($var) &&
197
            !$var instanceof \Closure &&
198
            method_exists($var, '__invoke');
199
    }
200
201
    /**
202
     * Get an object base on provided classname or interface name
203
     *
204
     * @param  string $classname class or interface name
205
     * @return object
206
     * @throws \Exception if something goes wrong
207
     * @access protected
208
     */
209
    protected function getObjectByClass(/*# string */ $classname)
210
    {
211
        if ($this->getResolver()->hasService($classname)) {
212
            $serviceId = ObjectResolver::getServiceId($classname);
213
            return $this->getResolver()->get($serviceId);
214
        }
215
        throw new LogicException(
216
            Message::get(Message::DI_CLASS_UNKNOWN, $classname),
217
            Message::DI_CLASS_UNKNOWN
218
        );
219
    }
220
221
    /**
222
     * Returns [$object, $method] if it is a callable, otherwise returns $method
223
     *
224
     * @param  mixed $object
225
     * @param  mixed $method
226
     * @return bool
227
     * @access protected
228
     */
229
    protected function getObjectMethod($object, $method)/*# : bool */
230
    {
231
        if (is_string($method) && method_exists($object, $method)) {
232
            return [$object, $method];
233
        } elseif (is_callable($method)) {
234
            return $method;
235
        } else {
236
            throw new LogicException(
237
                Message::get(Message::DI_CALLABLE_BAD, $method),
238
                Message::DI_CALLABLE_BAD
239
            );
240
        }
241
    }
242
243
    /**
244
     * Merge different sections of a node
245
     *
246
     * convert
247
     *   `['section1' => [[1], [2]], 'section2' => [[3], [4]]]`
248
     *
249
     * to
250
     *   `[[1], [2], [3], [4]]`
251
     *
252
     * @param  array|null $nodeData
253
     * @return array
254
     * @access protected
255
     */
256
    protected function mergeMethods($nodeData)/*# : array */
257
    {
258
        // no merge
259
        if (empty($nodeData) || isset($nodeData[0])) {
260
            return (array) $nodeData;
261
        }
262
263
        // in sections
264
        $result = [];
265
        foreach ($nodeData as $data) {
266
            $result = array_merge($result, $data);
267
        }
268
        return $result;
269
    }
270
271
    /**
272
     * Get common methods
273
     *
274
     * @return array
275
     * @access protected
276
     */
277
    protected function getCommonMethods()/*# : array */
278
    {
279
        // di.common node
280
        $commNode = $this->getResolver()->getSectionId('', 'common');
281
282
        return $this->mergeMethods(
283
            $this->getResolver()->get($commNode)
284
        );
285
    }
286
}
287