Passed
Push — master ( 059934...f33627 )
by Divine Niiquaye
02:08
created

Resolver::autowireArguments()   B

Complexity

Conditions 10
Paths 76

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 22
c 1
b 0
f 0
nc 76
nop 3
dl 0
loc 38
rs 7.6666

How to fix   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
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\DependencyInjection;
19
20
use Nette\DI\Resolver as NetteResolver;
21
use Nette\Utils\Reflection;
22
use ReflectionFunctionAbstract;
23
24
/**
25
 * Services resolver
26
 *
27
 * @internal
28
 */
29
class Resolver extends NetteResolver
30
{
31
    /**
32
     * Add missing arguments using autowiring.
33
     *
34
     * @param ReflectionFunctionAbstract $method
35
     * @param mixed[] $arguments
36
     * @param  (callable(string $type, bool $single): object|object[]|null) $getter
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
37
     *
38
     * @return mixed[]
39
     */
40
    public static function autowireArguments(
41
        ReflectionFunctionAbstract $method,
42
        array $arguments,
43
        callable $getter
44
    ): array {
45
        $optCount = 0;
46
        $num = -1;
47
        $res = [];
48
49
        foreach ($method->getParameters() as $num => $param) {
50
            $paramName = $param->name;
51
            if (!$param->isVariadic() && array_key_exists($paramName, $arguments)) {
52
                $res[$num] = $arguments[$paramName];
53
                unset($arguments[$paramName], $arguments[$num]);
54
            } elseif (array_key_exists($num, $arguments)) {
55
                $res[$num] = $arguments[$num];
56
                unset($arguments[$num]);
57
            } else {
58
                $res[$num] = self::autowireArgument($param, $getter);
59
            }
60
61
            $optCount = $param->isOptional() && $res[$num] === ($param->isDefaultValueAvailable() ? Reflection::getParameterDefaultValue($param) : null)
62
                ? $optCount + 1
63
                : 0;
64
        }
65
66
        // extra parameters
67
        while (array_key_exists(++$num, $arguments)) {
68
            $res[$num] = $arguments[$num];
69
            unset($arguments[$num]);
70
            $optCount = 0;
71
        }
72
73
        if ($optCount) {
74
            $res = array_slice($res, 0, -$optCount);
75
        }
76
77
        return $res;
78
    }
79
}
80