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

FactoryTrait::executeCommonMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
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\Traits;
16
17
use Phossa2\Di\Message\Message;
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
    use UtilityTrait;
33
34
    /**
35
     * for loop detection
36
     *
37
     * @var    array
38
     * @access protected
39
     */
40
    protected $loop = [];
41
42
    /**
43
     * a counter
44
     *
45
     * @var    int
46
     * @access protected
47
     */
48
    protected $counter = 0;
49
50
    /**
51
     * Full scope info
52
     *
53
     * @param  string $id
54
     * @return array
55
     * @access protected
56
     */
57
    protected function fullScopeInfo(/*# string */ $id)/*# : array */
58
    {
59
        list($rawId, $scope) = $this->scopedInfo($id);
60
61
        // special treatment if $scope is a '#service_id'
62
        if (isset($this->loop[$scope])) {
63
            $scope .= '_' . $this->loop[$scope];
64
        }
65
66
        return [$rawId, $this->scopedId($rawId, $scope), $scope];
67
    }
68
69
    /**
70
     * Create the instance with loop detection
71
     *
72
     * @param  string $rawId
73
     * @param  array $args arguments for the constructor if any
74
     * @return object
75
     * @throws LogicException if instantiation goes wrong or loop detected
76
     * @access protected
77
     */
78
    protected function createInstance(/*# string */ $rawId, array $args)
79
    {
80
        // conver 'service_id' to '#service_id'
81
        $serviceId = $this->getServiceId($rawId);
82
83
        // loop detected
84
        if (isset($this->loop[$serviceId])) {
85
            throw new LogicException(
86
                Message::get(Message::DI_LOOP_DETECTED, $rawId),
87
                Message::DI_LOOP_DETECTED
88
            );
89
        }
90
91
        // set loop marker
92
        $this->loop[$serviceId] = ++$this->counter;
93
94
        // create the service instance
95
        $obj = $this->createFromId($rawId, $args);
96
97
        // remove current marker
98
        unset($this->loop[$serviceId]);
99
100
        return $obj;
101
    }
102
103
    /**
104
     * Create object base on the raw id
105
     *
106
     * @param  string $rawId
107
     * @param  array $arguments
108
     * @return object
109
     * @throws LogicException if instantiation goes wrong
110
     * @access protected
111
     */
112
    protected function createFromId(/*# string */ $rawId, array $arguments)
113
    {
114
        // get definition
115
        $def = $this->getDefinition($rawId, $arguments);
116
117
        if (is_string($def['class'])) {
118
            // classname
119
            $obj = $this->constructObject($def['class'], $def['args']);
120
121
        } else {
122
            // object or callable etc.
123
            $obj = $this->executeCallable($def['class'], $def['args']);
124
        }
125
126
        // after creation
127
        $this->afterCreation($obj, $def);
128
129
        return $obj;
130
    }
131
132
    /**
133
     * Instantiate service object from classname
134
     *
135
     * @param  string $class
136
     * @param  array $args
137
     * @return object
138
     * @throws LogicException if something goes wrong
139
     * @access protected
140
     */
141
    protected function constructObject(/*# string */ $class, array $args)
142
    {
143
        $reflector = new \ReflectionClass($class);
144
        $constructor = $reflector->getConstructor();
145
146
        // not constructor defined
147
        if (is_null($constructor)) {
148
            $obj = $reflector->newInstanceWithoutConstructor();
149
150
        // normal class with constructor
151
        } else {
152
            $args = $this->matchArguments(
153
                $constructor->getParameters(),
154
                $args
155
            );
156
            $obj = $reflector->newInstanceArgs($args);
157
        }
158
159
        return $obj;
160
    }
161
162
    /**
163
     * Execute a (pseudo) callable with arguments
164
     *
165
     * @param  callable|array|object $callable callable or pseudo callable
166
     * @param  array $arguments
167
     * @return mixed
168
     * @throws LogicException if something goes wrong
169
     * @access protected
170
     */
171
    protected function executeCallable($callable, array $arguments = [])
172
    {
173
        // not callable
174
        if (!is_callable($callable)) {
175
            return $callable;
176
        }
177
178
        if (!empty($arguments)) {
179
            $params = $this->getCallableParameters($callable);
180
            $args = $this->matchArguments($params, $arguments);
181
            return call_user_func_array($callable, $args);
182
        } else {
183
            return call_user_func($callable);
184
        }
185
    }
186
187
    /**
188
     * Things to do after object created.
189
     *
190
     * @param  object $object
191
     * @param  array $definition service definition for $object
192
     * @access protected
193
     */
194
    protected function afterCreation($object, array $definition)
195
    {
196
        // execute methods of this object
197
        $this->executeObjectMethods($object, $definition);
198
199
        // execute common methods for all objects
200
        $this->executeCommonMethods($object);
201
    }
202
203
    /**
204
     * Execute objects's own methods defined in its 'node.methods'
205
     *
206
     * @param  object $object
207
     * @return $this
208
     * @access protected
209
     */
210
    protected function executeObjectMethods($object, array $definition)
211
    {
212
        if (isset($definition['methods'])) {
213
            foreach ($definition['methods'] as $method) {
214
                $this->executeMethod($method, $object);
215
            }
216
        }
217
        return $this;
218
    }
219
220
    /**
221
     * Execute common methods defined in 'di.common' for objects
222
     *
223
     * @param  object $object
224
     * @return $this
225
     * @access protected
226
     */
227
    protected function executeCommonMethods($object)
228
    {
229
        if ($this->getResolver()->has('', 'common')) {
230
            $methods = $this->mergeNodeInfo($this->getResolver()->get('', 'common'));
231
            $this->executeTester($object, $methods);
232
        }
233
        return $this;
234
    }
235
236
    /**
237
     * Rebuild callable base methodName and object
238
     *
239
     * method:
240
     * - ['function', [ arguments...]]
241
     *
242
     * - [ callable, [ arguments ...]]
243
     *
244
     * - ['method', [ arguments ...]]
245
     *   convert to [[$object, 'method'], [ ... ]]
246
     *
247
     * @param  mixed method
248
     * @param  object|null $object to construct callable
249
     * @throws LogicException if something goes wrong
250
     * @access protected
251
     */
252
    protected function executeMethod($method, $object = null)
253
    {
254
        $callable  = $method[0];
255
        $arguments = isset($method[1]) ? $method[1] : [];
256
257
        // rebuild callable from $object
258
        if (null !== $object) {
259
            $callable = [$object, $callable];
260
        }
261
262
        $this->executeCallable($callable, $arguments);
263
    }
264
}
265