Passed
Pull Request — master (#7)
by Alexander
01:18
created

Injector::resolveDependencies()   F

Complexity

Conditions 27
Paths 2093

Size

Total Lines 82
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 27

Importance

Changes 0
Metric Value
cc 27
eloc 54
nc 2093
nop 2
dl 0
loc 82
ccs 52
cts 52
cp 1
crap 27
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Injector;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use ReflectionException;
11
12
/**
13
 * Injector is able to analyze callable dependencies based on
14
 * type hinting and inject them from any PSR-11 compatible container.
15
 */
16
final class Injector
17
{
18
    private ContainerInterface $container;
19
20 41
    public function __construct(ContainerInterface $container)
21
    {
22 41
        $this->container = $container;
23
    }
24
25
    /**
26
     * Invoke a callback with resolving dependencies based on parameter types.
27
     *
28
     * This methods allows invoking a callback and let type hinted parameter names to be
29
     * resolved as objects of the Container. It additionally allow calling function passing named arguments.
30
     *
31
     * For example, the following callback may be invoked using the Container to resolve the formatter dependency:
32
     *
33
     * ```php
34
     * $formatString = function($string, \Yiisoft\I18n\MessageFormatterInterface $formatter) {
35
     *    // ...
36
     * }
37
     *
38
     * $injector = new Yiisoft\Injector\Injector($container);
39
     * $injector->invoke($formatString, ['string' => 'Hello World!']);
40
     * ```
41
     *
42
     * This will pass the string `'Hello World!'` as the first argument, and a formatter instance created
43
     * by the DI container as the second argument.
44
     *
45
     * @param callable $callable callable to be invoked.
46
     * @param array $arguments The array of the function arguments.
47
     * This can be either a list of arguments, or an associative array where keys are argument names.
48
     * @return mixed the callable return value.
49
     * @throws MissingRequiredArgumentException if required argument is missing.
50
     * @throws ContainerExceptionInterface if a dependency cannot be resolved or if a dependency cannot be fulfilled.
51
     * @throws ReflectionException
52
     */
53 30
    public function invoke(callable $callable, array $arguments = [])
54
    {
55 30
        $callable = \Closure::fromCallable($callable);
56 30
        $reflection = new \ReflectionFunction($callable);
57 30
        return $reflection->invokeArgs($this->resolveDependencies($reflection, $arguments));
58
    }
59
60
    /**
61
     * Creates an object of a given class with resolving constructor dependencies based on parameter types.
62
     *
63
     * This methods allows invoking a constructor and let type hinted parameter names to be
64
     * resolved as objects of the Container. It additionally allow calling constructor passing named arguments.
65
     *
66
     * For example, the following constructor may be invoked using the Container to resolve the formatter dependency:
67
     *
68
     * ```php
69
     * class StringFormatter
70
     * {
71
     *     public function __construct($string, \Yiisoft\I18n\MessageFormatterInterface $formatter)
72
     *     {
73
     *         // ...
74
     *     }
75
     * }
76
     *
77
     * $injector = new Yiisoft\Injector\Injector($container);
78
     * $stringFormatter = $injector->make(StringFormatter::class, ['string' => 'Hello World!']);
79
     * ```
80
     *
81
     * This will pass the string `'Hello World!'` as the first argument, and a formatter instance created
82
     * by the DI container as the second argument.
83
     *
84
     * @param string $class name of the class to be created.
85
     * @param array $arguments The array of the function arguments.
86
     * This can be either a list of arguments, or an associative array where keys are argument names.
87
     * @return mixed object of the given class.
88
     * @throws ContainerExceptionInterface
89
     * @throws MissingRequiredArgumentException|InvalidArgumentException
90
     * @throws ReflectionException
91
     */
92 11
    public function make(string $class, array $arguments = [])
93
    {
94 11
        $classReflection = new \ReflectionClass($class);
95 10
        if (!$classReflection->isInstantiable()) {
96 3
            throw new \InvalidArgumentException("Class $class is not instantiable.");
97
        }
98 7
        $reflection = $classReflection->getConstructor();
99 7
        if ($reflection === null) {
100
            // Method __construct() does not exist
101 1
            return new $class();
102
        }
103
104 6
        return new $class(...$this->resolveDependencies($reflection, $arguments));
105
    }
106
107
    /**
108
     * Resolve dependencies for the given function reflection object and a list of concrete arguments
109
     * and return array of arguments to call the function with.
110
     *
111
     * @param \ReflectionFunctionAbstract $reflection function reflection.
112
     * @param array $arguments concrete arguments.
113
     * @return array resolved arguments.
114
     * @throws ContainerExceptionInterface
115
     * @throws MissingRequiredArgumentException|InvalidArgumentException
116
     * @throws ReflectionException
117
     */
118 36
    private function resolveDependencies(\ReflectionFunctionAbstract $reflection, array $arguments = []): array
119
    {
120 36
        $resolvedArguments = [];
121
122 36
        $pushUnusedArguments = true;
123 36
        foreach ($reflection->getParameters() as $parameter) {
124 32
            $name = $parameter->getName();
125 32
            $class = $parameter->getClass();
126 32
            $hasType = $parameter->hasType();
127 32
            $isNullable = $parameter->allowsNull() && $hasType;
128 32
            $isVariadic = $parameter->isVariadic();
129 32
            $error = null;
130
131
            // Get argument by name
132 32
            if (array_key_exists($name, $arguments)) {
133 6
                if ($isVariadic && is_array($arguments[$name])) {
134 1
                    $resolvedArguments = array_merge($resolvedArguments, array_values($arguments[$name]));
135
                } else {
136 5
                    $resolvedArguments[] = $arguments[$name];
137
                }
138 6
                unset($arguments[$name]);
139 6
                continue;
140
            }
141
142 29
            $type = $hasType ? $parameter->getType()->getName() : null;
143 29
            if ($class !== null || $type === 'object') {
144
                // Unnamed arguments
145 21
                $className = $class !== null ? $class->getName() : null;
146 21
                $found = false;
147 21
                foreach ($arguments as $key => $item) {
148 9
                    if (!is_int($key)) {
149 1
                        continue;
150
                    }
151 8
                    if (is_object($item) and $className === null || $item instanceof $className) {
152 7
                        $found = true;
153 7
                        $resolvedArguments[] = $item;
154 7
                        unset($arguments[$key]);
155 7
                        if (!$isVariadic) {
156 5
                            break;
157
                        }
158
                    }
159
                }
160 21
                if ($found) {
161 7
                    $pushUnusedArguments = false;
162 7
                    continue;
163
                }
164
165 18
                if ($className !== null) {
166
                    // If the argument is optional we catch not instantiable exceptions
167
                    try {
168 17
                        $resolvedArguments[] = $this->container->get($className);
169 15
                        continue;
170 3
                    } catch (NotFoundExceptionInterface $e) {
171 3
                        $error = $e;
172
                    }
173
                }
174
            }
175
176 15
            if ($parameter->isDefaultValueAvailable()) {
177 2
                $resolvedArguments[] = $parameter->getDefaultValue();
178 13
            } elseif (!$parameter->isOptional()) {
179 9
                if ($isNullable) {
180 2
                    $resolvedArguments[] = null;
181
                } else {
182 9
                    throw $error ?? new MissingRequiredArgumentException($name, $reflection->getName());
183
                }
184 4
            } elseif ($hasType) {
185 2
                $pushUnusedArguments = false;
186
            }
187
        }
188
189 29
        foreach ($arguments as $key => $value) {
190 7
            if (is_int($key)) {
191 7
                if (!is_object($value)) {
192 2
                    throw new InvalidArgumentException((string)$key, $reflection->getName());
193
                }
194 5
                if ($pushUnusedArguments) {
195 2
                    $resolvedArguments[] = $value;
196
                }
197
            }
198
        }
199 27
        return $resolvedArguments;
200
    }
201
}
202