TypeHintValueResolver::resolve()   F
last analyzed

Complexity

Conditions 15
Paths 344

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 15

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 28
c 2
b 0
f 0
nc 344
nop 2
dl 0
loc 55
ccs 27
cts 27
cp 1
crap 15
rs 3.2833

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
/*
6
 * This file is part of PHP Invoker.
7
 *
8
 * PHP version 7.1 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 DivineNii\Invoker\ArgumentResolver;
19
20
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface;
21
use Psr\Container\ContainerInterface;
22
use Psr\Container\NotFoundExceptionInterface;
23
24
/**
25
 * Inject entries using type-hints or create a class instance.
26
 * Tries to match type-hints with the parameters provided.
27
 *
28
 * @author Divine Niiquaye Ibok <[email protected]>
29
 */
30
class TypeHintValueResolver implements ArgumentValueResolverInterface
31
{
32
    /** @var null|ContainerInterface */
33
    private $container;
34
35 56
    public function __construct(?ContainerInterface $container = null)
36
    {
37 56
        $this->container = $container;
38 56
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 25
    public function resolve(\ReflectionParameter $parameter, array $providedParameters)
44
    {
45 25
        $paramType = $parameter->getType();
46 25
        $types  = $paramType instanceof \ReflectionUnionType ? $paramType->getTypes() : [$paramType];
47 25
        $result = [];
48
49 25
        foreach ($types as $parameterType) {
50 25
            if (!$parameterType instanceof \ReflectionNamedType || $parameterType->isBuiltin()) {
51
                // No type, Primitive types are not supported
52 14
                return null;
53
            }
54 12
            $paramName = $parameterType->getName();
55
56 12
            if ($paramName === 'self') {
57 1
                $paramName = $parameter->getDeclaringClass()->getName();
58
            }
59
60 12
            if (\array_key_exists($paramName, $providedParameters)) {
61 1
                $result[] = $providedParameters[$paramName];
62 1
                unset($providedParameters[$paramName]);
63
64 1
                continue;
65
            }
66
67
            // If an instance is detected
68 11
            foreach ($providedParameters as $key => $value) {
69 1
                if (\is_a($value, $paramName, true)) {
70 1
                    $result[] = $providedParameters[$key];
71
72 1
                    continue;
73
                }
74
            }
75
76
            // Inject entries from a DI container using the type-hints.
77 11
            if (null !== $this->container) {
78
                try {
79 6
                    $result[] = $this->container->get($paramName);
80
81 5
                    continue;
82 1
                } catch (NotFoundExceptionInterface $e) {
83
                    // We need no exception thrown here
84
                }
85
            }
86
87
            try {
88 7
                if (\class_exists($paramName)) {
89 7
                    $result[] = new $paramName();
90
                }
91 1
            } catch (\ArgumentCountError $e) {
92
                // Throw no exception ...
93
            }
94
        }
95
96 12
        if ([] !== $result) {
97 10
            return $parameter->isVariadic() ? $result : end($result);
98
        }
99 2
    }
100
}
101