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.
Passed
Push — master ( 3c5463...84f915 )
by Hong
16:33
created

Factory::executeCallable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
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\Factory;
16
17
use Phossa2\Di\Message\Message;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Di\Exception\LogicException;
20
use Phossa2\Di\Traits\ResolverAwareTrait;
21
use Phossa2\Di\Interfaces\FactoryInterface;
22
use Phossa2\Di\Interfaces\ResolverInterface;
23
24
/**
25
 * Factory
26
 *
27
 * Wrapper of factorying methods for container
28
 *
29
 * @package Phossa2\Di
30
 * @author  Hong Zhang <[email protected]>
31
 * @see     FactoryInterface
32
 * @version 2.0.0
33
 * @since   2.0.0 added
34
 */
35
class Factory extends ObjectAbstract implements FactoryInterface
36
{
37
    use ResolverAwareTrait, FactoryHelperTrait;
38
39
    /**
40
     * @param  ResolverInterface
41
     * @access public
42
     */
43
    public function __construct(ResolverInterface $resolver)
44
    {
45
        $this->setResolver($resolver);
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function createInstance(/*# string */ $rawId, array $arguments)
52
    {
53
        // get service definition
54
        $def = $this->getDefinition($rawId, $arguments);
55
56
        // arguments
57
        $args = isset($def['args']) ? $def['args'] : [];
58
59
        if (is_string($def['class'])) {
60
            $obj = $this->constructObject($def['class'], $args);
61
62
        } else {
63
            $obj = $this->executeCallable($def['class'], $args);
64
        }
65
66
        // execute after-creation methods
67
        $this->afterCreation($obj, $def);
68
69
        return $obj;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function executeCallable($callable, array $arguments = [])
76
    {
77
        // not callable
78
        if (!is_callable($callable)) {
79
            return $callable;
80
        }
81
82
        if (!empty($arguments)) {
83
            $params = $this->getCallableParameters($callable);
84
            $args = $this->matchArguments($params, $arguments);
85
            return call_user_func_array($callable, $args);
86
        } else {
87
            return call_user_func($callable);
88
        }
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function executeMethodBatch(array $methods, $object = null)
95
    {
96
        foreach ($this->mergeMethods($methods) as $method) {
97
            $this->executeMethod($method, $object);
98
        }
99
    }
100
101
    /**
102
     * if $object provided, build callable like [$object, $method] and execute it.
103
     *
104
     * method:
105
     *
106
     * - callable
107
     *
108
     * - array ['function', [ arguments...]]
109
     *
110
     * - array [callable, [ arguments ...]]
111
     *
112
     * - array ['method', [ arguments ...]]
113
     *   will be converted to [[$object, 'method'], [ ... ]]
114
     *
115
     * @param  array|callable method
116
     * @param  object|null $object to construct callable
117
     * @throws LogicException if something goes wrong
118
     * @access protected
119
     */
120
    protected function executeMethod($method, $object = null)
121
    {
122
        // is callable
123
        if (is_callable($method)) {
124
            return $this->executeCallable($method);
125
126
        // is [ method, arguments ]
127
        } elseif (isset($method[0])) {
128
            return $this->executeCallable(
129
                $this->getObjectMethod($object, $method[0]), // callable
130
                isset($method[1]) ? $method[1] : [] // arguments
131
            );
132
        }
133
    }
134
135
    /**
136
     * Things to do after an object created.
137
     *
138
     * @param  object $object
139
     * @param  array $definition service definition for $object
140
     * @access protected
141
     */
142
    protected function afterCreation($object, array $definition)
143
    {
144
        // execute methods of THIS object
145
        if (isset($definition['methods'])) {
146
            $this->executeMethodBatch($definition['methods'], $object);
147
        }
148
149
        // execute common methods for all objects
150
        if (!isset($definition['skip']) || !$definition['skip']) {
151
            $methods = $this->getCommonMethods();
152
            $this->executeMethodBatch($methods, $object);
153
        }
154
    }
155
156
    /**
157
     * Get common methods
158
     *
159
     * @return array
160
     * @access protected
161
     */
162
    protected function getCommonMethods()/*# : array */
163
    {
164
        // di.common node
165
        $commNode = $this->getResolver()->getSectionId('', 'common');
166
167
        return $this->mergeMethods(
168
            $this->getResolver()->get($commNode)
169
        );
170
    }
171
172
    /**
173
     * Merge different sections of a node
174
     *
175
     * convert
176
     *   `['section1' => [[1], [2]], 'section2' => [[3], [4]]]`
177
     *
178
     * to
179
     *   `[[1], [2], [3], [4]]`
180
     *
181
     * @param  array|null $nodeData
182
     * @return array
183
     * @access protected
184
     */
185
    protected function mergeMethods($nodeData)/*# : array */
186
    {
187
        // no merge
188
        if (empty($nodeData) || isset($nodeData[0])) {
189
            return (array) $nodeData;
190
        }
191
192
        // in sections
193
        $result = [];
194
        foreach ($nodeData as $data) {
195
            $result = array_merge($result, $data);
196
        }
197
        return $result;
198
    }
199
200
    /**
201
     * Returns [$object, $method] if it is a callable, otherwise returns $method
202
     *
203
     * @param  mixed $object
204
     * @param  mixed $method
205
     * @return bool
206
     * @access protected
207
     */
208
    protected function getObjectMethod($object, $method)/*# : bool */
209
    {
210
        if (is_string($method) && method_exists($object, $method)) {
211
            return [$object, $method];
212
        } elseif (is_callable($method)) {
213
            return $method;
214
        } else {
215
            throw new LogicException(
216
                Message::get(Message::DI_CALLABLE_BAD, $method),
217
                Message::DI_CALLABLE_BAD
218
            );
219
        }
220
    }
221
}
222