Passed
Push — master ( ca5e6e...04ea7b )
by Sergei
55:45
created

DefinitionResolver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
eloc 18
c 1
b 1
f 0
dl 0
loc 70
ccs 20
cts 20
cp 1
rs 10

3 Methods

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