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 ( 98ccbf...73e071 )
by Hong
03:32
created

Factory::executeCommonBatch()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 11
nc 6
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
        $merged = $this->mergeMethods($methods);
95
96
        foreach ($merged 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
     * - ['function', [ arguments...]]
106
     *
107
     * - [ callable, [ arguments ...]]
108
     *
109
     * - ['method', [ arguments ...]]
110
     *   convert to [[$object, 'method'], [ ... ]]
111
     *
112
     * @param  mixed method
113
     * @param  object|null $object to construct callable
114
     * @throws LogicException if something goes wrong
115
     * @access protected
116
     */
117
    protected function executeMethod($method, $object = null)
118
    {
119
        $callable  = $method[0];
120
        $arguments = isset($method[1]) ? $method[1] : [];
121
122
        // rebuild callable from $object
123
        if (null !== $object &&
124
            is_string($callable) &&
125
            method_exists($object, $callable)
126
        ) {
127
            $callable = [$object, $callable];
128
        }
129
130
        $this->executeCallable($callable, $arguments);
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
        $methods = [];
169
170
        // get methods from 'di.common'
171
        if ($this->master->getResolver()->hasInSection('', 'common')) {
172
            $methods = $this->mergeMethods(
173
                $this->master->getResolver()->getInSection('', 'common')
174
            );
175
        }
176
177
        foreach ($methods as $method) {
178
            $tester = $method[0];
179
            if (is_string($tester) && is_a($object, $tester) ||
180
                call_user_func($tester, $object, $this->master)
181
            ) {
182
                $this->executeMethod($method[1], $object);
183
            }
184
        }
185
        return $this;
186
    }
187
}
188