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

Normalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 18
c 2
b 0
f 0
dl 0
loc 81
ccs 16
cts 18
cp 0.8889
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 42 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use Yiisoft\Factory\Exception\InvalidConfigException;
8
9
use function array_key_exists;
10
use function is_array;
11
use function is_callable;
12
use function is_object;
13
use function is_string;
14
15
/**
16
 * Class Definition represents a definition in a container
17
 */
18
class Normalizer
19
{
20
    /**
21
     * Definition may be defined multiple ways.
22
     * Interface name as string:
23
     *
24
     * ```php
25
     * $container->set('interface_name', EngineInterface::class);
26
     * ```
27
     *
28
     * A closure:
29
     *
30
     * ```php
31
     * $container->set('closure', function($container) {
32
     *     return new MyClass($container->get('db'));
33
     * });
34
     * ```
35
     *
36
     * A callable array:
37
     *
38
     * ```php
39
     * $container->set('static_call', [MyClass::class, 'create']);
40
     * ```
41
     *
42
     * A definition array:
43
     *
44
     * ```php
45
     * $container->set('full_definition', [
46
     *     'class' => EngineMarkOne::class,
47
     *     '__construct()' => [42],
48
     *     '$argName' => 'value',
49
     *     'setX()' => [42],
50
     * ]);
51
     * ```
52
     *
53
     * @param mixed $definition
54
     *
55
     * @throws InvalidConfigException
56
     */
57 25
    public static function normalize($definition, string $id = null): DefinitionInterface
58
    {
59
        // Reference
60 25
        if ($definition instanceof ReferenceInterface) {
61 1
            return $definition;
62
        }
63
64 25
        if (is_string($definition)) {
65
            // Current class
66
            if (
67 18
                $id === $definition ||
68 18
                ($id === null && class_exists($definition))
69
            ) {
70
                /** @psalm-var class-string $definition */
71 10
                return ArrayDefinition::fromPreparedData($definition);
72
            }
73
74
            // Reference to another class or alias
75 8
            return Reference::to($definition);
76
        }
77
78
        // Callable definition
79 10
        if (is_callable($definition, true)) {
80 3
            return new CallableDefinition($definition);
81
        }
82
83
        // Array definition
84 7
        if (is_array($definition)) {
85 6
            $config = $definition;
86 6
            if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) {
87
                $config[ArrayDefinition::CLASS_NAME] = $id;
88
            }
89
            /** @psalm-suppress ArgumentTypeCoercion */
90 6
            return ArrayDefinition::fromConfig($config);
91
        }
92
93
        // Ready object
94 1
        if (is_object($definition)) {
95 1
            return new ValueDefinition($definition);
96
        }
97
98
        throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
99
    }
100
}
101