Passed
Push — master ( 5d4c6c...ea9d31 )
by Alexander
07:11
created

Normalizer::normalize()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 34
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
nc 8
nop 3
dl 0
loc 34
ccs 14
cts 16
cp 0.875
crap 10.1953
rs 7.6666
c 2
b 0
f 0

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