Passed
Push — master ( a05b28...201c8d )
by Alexander
08:16
created

Normalizer::normalize()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.1953

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 15
c 2
b 0
f 0
nc 8
nop 3
dl 0
loc 29
ccs 14
cts 16
cp 0.875
crap 10.1953
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public const TAGS = '__tags';
15
    public const DEFINITION = '__definition';
16
17
    /**
18
     * Definition may be defined multiple ways.
19
     * Interface name as string:
20
     *
21
     * ```php
22
     * $container->set('interface_name', EngineInterface::class);
23
     * ```
24
     *
25
     * A closure:
26
     *
27
     * ```php
28
     * $container->set('closure', function($container) {
29
     *     return new MyClass($container->get('db'));
30
     * });
31
     * ```
32
     *
33
     * A callable array:
34
     *
35
     * ```php
36
     * $container->set('static_call', [MyClass::class, 'create']);
37
     * ```
38
     *
39
     * A definition array:
40
     *
41
     * ```php
42
     * $container->set('full_definition', [
43
     *     '__class' => EngineMarkOne::class,
44
     *     '__construct()' => [42],
45
     *     'argName' => 'value',
46
     *     'setX()' => [42],
47
     * ]);
48
     * ```
49
     *
50
     * @param mixed $definition
51
     * @param string $id
52
     * @param array $params
53
     *
54
     * @throws InvalidConfigException
55
     */
56 41
    public static function normalize($definition, string $id = null, array $params = []): DefinitionInterface
57
    {
58 41
        if ($definition instanceof DefinitionInterface) {
59 2
            return $definition;
60
        }
61
62 41
        if (\is_string($definition)) {
63 36
            if (empty($definition)) {
64
                throw new InvalidConfigException('Invalid definition: empty string.');
65
            }
66 36
            if ($id === $definition || (!empty($params) && class_exists($definition))) {
67 12
                return ArrayDefinition::fromArray($definition, $params);
68
            }
69 24
            return Reference::to($definition);
70
        }
71
72 28
        if (\is_callable($definition, true)) {
73 2
            return new CallableDefinition($definition);
74
        }
75
76 28
        if (\is_array($definition)) {
77 10
            return ArrayDefinition::fromArray($id, [], $definition);
78
        }
79
80 24
        if (\is_object($definition)) {
81 24
            return new ValueDefinition($definition);
82
        }
83
84
        throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
85
    }
86
87
    public static function parse($definition): array
88
    {
89
        if (!is_array($definition)) {
90
            return [$definition, []];
91
        }
92
        $tags = (array)($definition[self::TAGS] ?? []);
93
        unset($definition[self::TAGS]);
94
95
        return [$definition[self::DEFINITION] ?? $definition, $tags];
96
    }
97
98
    /**
99
     * Validates defintion for corectness.
100
     *
101
     * @param mixed $definition @see normalize()
102
     * @param bool $id
103
     *
104
     * @throws InvalidConfigException
105
     *
106
     * @return bool
107
     */
108 23
    public static function validate($definition, bool $throw = true): bool
109
    {
110 23
        if ($definition instanceof DefinitionInterface) {
111
            return true;
112
        }
113
114 23
        if (\is_string($definition) && !empty($definition)) {
115 4
            return true;
116
        }
117
118 23
        if (\is_callable($definition)) {
119
            return true;
120
        }
121
122 23
        if (\is_array($definition)) {
123 1
            return true;
124
        }
125
126 23
        if (\is_object($definition)) {
127 23
            return true;
128
        }
129
130
        if ($throw) {
131
            throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
132
        }
133
134
        return false;
135
    }
136
}
137