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 ( 616c58...8c3ee0 )
by Hong
02:38
created

UtilityTrait::getDefinition()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 10
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\Message\Message;
18
use Phossa2\Di\Exception\LogicException;
19
20
/**
21
 * UtilityTrait
22
 *
23
 * Collection of utility methods
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 UtilityTrait
31
{
32
    use ScopeTrait;
33
34
    /**
35
     * Is $var an object with '__invoke()' defined ?
36
     *
37
     * @param  mixed $var
38
     * @return bool
39
     * @access protected
40
     */
41
    protected function isInvocable($var)/*# : bool */
42
    {
43
        return is_object($var) && method_exists($var, '__invoke');
44
    }
45
46
    /**
47
     * Merge different sections of a node
48
     *
49
     * convert
50
     *   `['section1' => [[1], [2]], 'section2' => [[3], [4]]]`
51
     *
52
     * to
53
     *   `[[1], [2], [3], [4]]`
54
     *
55
     * @param  array $nodeData
56
     * @return array
57
     * @access protected
58
     */
59
    protected function mergeNodeInfo(array $nodeData)/*# : array */
60
    {
61
        // no merge
62
        if (isset($nodeData[0])) {
63
            return $nodeData;
64
        }
65
66
        // in sections
67
        $result = [];
68
        foreach ($nodeData as $data) {
69
            $result = array_merge($result, $data);
70
        }
71
        return $result;
72
    }
73
74
    /**
75
     * Prepend '#' to rawId, representing a service object id
76
     *
77
     * @param  string $rawId
78
     * @return string
79
     * @access protected
80
     */
81
    protected function getServiceId(/*# string */ $rawId)/*# : string */
82
    {
83
        return '#' . $rawId;
84
    }
85
86
    /**
87
     * Execute [tester, todo] pairs, both use $object and $container as arguments
88
     *
89
     * signatures
90
     * - tester: function($object, $container) { return $object instance of XXXX; }
91
     * - todoer: function($object, $container) { }
92
     *
93
     * @param  object $object
94
     * @param  array $methods
95
     * @access protected
96
     */
97
    protected function executeTester($object, array $methods)
98
    {
99
        foreach ($methods as $method) {
100
            if ($method[0]($object, $this)) {
101
                $method[1]($object, $this);
102
            }
103
        }
104
    }
105
106
    /**
107
     * Is $parameter same type as the $argument ?
108
     *
109
     * @param  \ReflectionParameter $parameter
110
     * @param  mixed $argument
111
     * @param  null|string $class
112
     * @return bool
113
     * @throws LogicException if type missmatch
114
     * @access protected
115
     */
116
    protected function isTypeMatched(
117
        \ReflectionParameter $parameter,
118
        $argument,
119
        $class
120
    )/*# : bool */ {
121
        if (null === $argument) {
122
            return false;
123
        } elseif (null !== $class) {
124
            return is_a($argument, $parameter->getClass()->getName());
125
        } else {
126
            return true;
127
        }
128
    }
129
130
    /**
131
     * Get callable parameters
132
     *
133
     * @param  callable $callable
134
     * @return \ReflectionParameter[]
135
     * @throws LogicException if something goes wrong
136
     * @access protected
137
     */
138
    protected function getCallableParameters(callable $callable)/*# : array */
139
    {
140
        // array type
141
        if (is_array($callable)) {
142
            $reflector = new \ReflectionClass($callable[0]);
143
            $method = $reflector->getMethod($callable[1]);
144
145
        } elseif ($this->isInvocable($callable)) {
146
            $reflector = new \ReflectionClass($callable);
147
            $method = $reflector->getMethod('__invoke');
148
149
            // simple function
150
        } else {
151
            $method = new \ReflectionFunction($callable);
152
        }
153
154
        return $method->getParameters();
155
    }
156
157
    /**
158
     * Match provided arguments with a method/function's reflection parameters
159
     *
160
     * @param  \ReflectionParameter[] $reflectionParameters
161
     * @param  array $providedArguments
162
     * @return array the resolved arguments
163
     * @throws LogicException
164
     * @throws NotFoundException
165
     * @access protected
166
     */
167
    protected function matchArguments(
168
        array $reflectionParameters,
169
        array $providedArguments
170
    )/*# : array */ {
171
        // result
172
        $resolvedArguments = [];
173
174
        // go thru each predefined parameter
175
        foreach ($reflectionParameters as $i => $param) {
176
            // arg to match with
177
            $argument = isset($providedArguments[0]) ? $providedArguments[0] : null;
178
179
            // check the class
180
            $class = $param->getClass();
181
182
            if ($this->isTypeMatched($param, $argument, $class)) {
183
                // type matched
184
                $resolvedArguments[$i] = array_shift($providedArguments);
185
186
            } elseif (null !== $class) {
187
                // not matched, but $param is an interface or class
188
                $resolvedArguments[$i] = $this->getObjectByClass($class->getName());
189
190
            } elseif ($param->isOptional()) {
191
                // $param is optional, $arg is null
192
                break;
193
            } else {
194
                throw new LogicException(
195
                    Message::get(Message::DI_PARAMETER_NOTFOUND, $param->getName()),
196
                    Message::DI_PARAMETER_NOTFOUND
197
                );
198
            }
199
        }
200
201
        // append remained arguments if any
202
        if (!empty($providedArguments)) {
203
            $resolvedArguments = array_merge($resolvedArguments, $providedArguments);
204
        }
205
206
        return $resolvedArguments;
207
    }
208
209
    /**
210
     * Get an object base on provided classname or interface name
211
     *
212
     * @param  string $classname class or interface name
213
     * @return object
214
     * @throws \Exception if something goes wrong
215
     * @access protected
216
     */
217
    protected function getObjectByClass(/*# string */ $classname)
218
    {
219
        // mapping exists
220
        if ($this->getResolver()->hasMapping($classname)) {
221
            $classname = $this->getResolver()->getMapping($classname);
222
            if (is_object($classname)) {
223
                return $classname;
224
            }
225
        }
226
        return $this->get($classname);
227
    }
228
229
    /**
230
     * Get service definition and fix it
231
     *
232
     * @param  string $rawId
233
     * @param  array $args
234
     * @return array
235
     * @access protected
236
     */
237
    protected function getDefinition(
238
        /*# string */ $rawId,
239
        array $args
240
    )/*# : array */ {
241
        // get the definition
242
        $def = $this->getResolver()->getService($rawId);
243
244
        // fix class
245
        if (!is_array($def)) {
246
            $def = ['class' => $def];
247
        }
248
249
        // fix arguments
250
        if (!empty($args) || !isset($def['args'])) {
251
            $this->resolve($args);
252
            $def['args'] = $args;
253
        }
254
255
        return $def;
256
    }
257
258
    /**
259
     * @param string $id Identifier of the entry to look for.
260
     * @return mixed Entry.
261
     */
262
    abstract public function get($id);
263
}
264