Passed
Push — master ( 8afadc...1cb41c )
by Alexander
03:09 queued 54s
created

DefinitionResolver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 77
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureResolvable() 0 14 4
A resolveArray() 0 20 6
A resolve() 0 12 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Definitions\Helpers;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Definitions\Contract\DefinitionInterface;
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> $definitions Definitions to resolve.
27
     *
28
     * @return array The resolved dependencies.
29
     *
30
     * @psalm-return array<string,mixed>
31
     */
32 22
    public static function resolveArray(ContainerInterface $container, ?ContainerInterface $referenceContainer, array $definitions): array
33
    {
34 22
        $result = [];
35
        /** @var mixed $definition */
36 22
        foreach ($definitions as $key => $definition) {
37
            if (
38 22
                $definition instanceof ParameterDefinition &&
39
                (
40 21
                    $definition->isVariadic() ||
41 22
                    ($definition->isOptional() && !$definition->hasValue())
42
                )
43
            ) {
44 16
                continue;
45
            }
46
47
            /** @var mixed */
48 22
            $result[$key] = self::resolve($container, $referenceContainer, $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 Definition to resolve.
58
     *
59
     * @return mixed
60
     */
61 22
    public static function resolve(ContainerInterface $container, ?ContainerInterface $referenceContainer, $definition)
62
    {
63 22
        if ($definition instanceof DefinitionInterface) {
64 22
            $container = $referenceContainer !== null && $definition instanceof ReferenceInterface ? $referenceContainer : $container;
65
            /** @var mixed $definition */
66 22
            $definition = $definition->resolve($container);
67 8
        } elseif (is_array($definition)) {
68
            /** @psalm-var array<string,mixed> $definition */
69 6
            return self::resolveArray($container, $referenceContainer, $definition);
70
        }
71
72 22
        return $definition;
73
    }
74
75
    /**
76
     * @param mixed $value
77
     *
78
     * @throws InvalidConfigException
79
     *
80
     * @return array|ReferenceInterface|ValueDefinition
81
     */
82 9
    public static function ensureResolvable($value)
83
    {
84 9
        if ($value instanceof ReferenceInterface || is_array($value)) {
85 2
            return $value;
86
        }
87
88 7
        if ($value instanceof DefinitionInterface) {
89 1
            throw new InvalidConfigException(
90
                'Only references are allowed in constructor arguments, a definition object was provided: ' .
91 1
                var_export($value, true)
92
            );
93
        }
94
95 6
        return new ValueDefinition($value);
96
    }
97
}
98