Passed
Push — master ( cfe928...9076e1 )
by Alexander
10:06 queued 08:36
created

Normalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 66
ccs 13
cts 14
cp 0.9286
rs 10
wmc 9

1 Method

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