Passed
Pull Request — master (#85)
by Sergei
02:15
created

DefinitionResolver   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 14
eloc 20
dl 0
loc 69
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

3 Methods

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