Passed
Pull Request — master (#8)
by Sergei
02:40
created

DefinitionResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 76
ccs 22
cts 22
cp 1
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 11 3
A resolveArray() 0 20 6
A ensureResolvable() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Definitions\Infrastructure;
6
7
use Yiisoft\Definitions\Contract\DefinitionInterface;
8
use Yiisoft\Definitions\Contract\DependencyResolverInterface;
9
use Yiisoft\Definitions\Contract\ReferenceInterface;
10
use Yiisoft\Definitions\Exception\InvalidConfigException;
11
use Yiisoft\Definitions\ParameterDefinition;
12
use Yiisoft\Definitions\ValueDefinition;
13
14
use function is_array;
15
16
/**
17
 * @internal
18
 */
19
final class DefinitionResolver
20
{
21
    /**
22
     * Resolves dependencies by replacing them with the actual object instances.
23
     *
24
     * @param array $dependencies The dependencies.
25
     *
26
     * @psalm-param array<string,mixed> $dependencies
27
     *
28
     * @return array The resolved dependencies.
29
     *
30
     * @psalm-return array<string,mixed>
31
     */
32 22
    public static function resolveArray(DependencyResolverInterface $dependencyResolver, array $dependencies): array
33
    {
34 22
        $result = [];
35
        /** @var mixed $definition */
36 22
        foreach ($dependencies as $key => $definition) {
37
            if (
38 22
                $definition instanceof ParameterDefinition &&
39
                (
40 17
                    $definition->isVariadic() ||
41 22
                    ($definition->isOptional() && !$definition->hasValue())
42
                )
43
            ) {
44 16
                continue;
45
            }
46
47
            /** @var mixed */
48 22
            $result[$key] = self::resolve($dependencyResolver, $definition);
49
        }
50
51 22
        return $result;
52
    }
53
54
    /**
55
     * This function resolves a definition recursively, checking for loops.
56
     *
57
     * @param mixed $definition
58
     *
59
     * @return mixed
60
     */
61 22
    public static function resolve(DependencyResolverInterface $dependencyResolver, $definition)
62
    {
63 22
        if ($definition instanceof DefinitionInterface) {
64
            /** @var mixed $definition */
65 22
            $definition = $definition->resolve($dependencyResolver);
66 8
        } elseif (is_array($definition)) {
67
            /** @psalm-var array<string,mixed> $definition */
68 6
            return self::resolveArray($dependencyResolver, $definition);
69
        }
70
71 22
        return $definition;
72
    }
73
74
    /**
75
     * @param mixed $value
76
     *
77
     * @throws InvalidConfigException
78
     *
79
     * @return array|ReferenceInterface|ValueDefinition
80
     */
81 9
    public static function ensureResolvable($value)
82
    {
83 9
        if ($value instanceof ReferenceInterface || is_array($value)) {
84 2
            return $value;
85
        }
86
87 7
        if ($value instanceof DefinitionInterface) {
88 1
            throw new InvalidConfigException(
89
                'Only references are allowed in constructor arguments, a definition object was provided: ' .
90 1
                var_export($value, true)
91
            );
92
        }
93
94 6
        return new ValueDefinition($value);
95
    }
96
}
97