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 ( 6e4eb2...dda63a )
by Hong
02:42
created

Factory::executeCommonBatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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