Passed
Push — master ( 3633ed...8de50e )
by Alexander
21:09 queued 19:43
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
    /**
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 $definition
48
     * @param string $id
49
     * @param array $params
50
     * @throws InvalidConfigException
51
     */
52 41
    public static function normalize($definition, string $id = null, array $params = []): DefinitionInterface
53
    {
54 41
        if ($definition instanceof DefinitionInterface) {
55 2
            return $definition;
56
        }
57
58 41
        if (\is_string($definition)) {
59 36
            if (empty($definition)) {
60
                throw new InvalidConfigException('Invalid definition: empty string.');
61
            }
62 36
            if ($id === $definition || (!empty($params) && class_exists($definition))) {
63 12
                return ArrayDefinition::fromArray($definition, $params);
64
            }
65 24
            return Reference::to($definition);
66
        }
67
68 28
        if (\is_callable($definition, true)) {
69 2
            return new CallableDefinition($definition);
70
        }
71
72 28
        if (\is_array($definition)) {
73 10
            return ArrayDefinition::fromArray($id, [], $definition);
74
        }
75
76 24
        if (\is_object($definition)) {
77 24
            return new ValueDefinition($definition);
78
        }
79
80
        throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
81
    }
82
83
    /**
84
     * Validates defintion for corectness.
85
     * @param mixed $definition @see normalize()
86
     * @param bool $id
87
     * @return bool
88
     * @throws InvalidConfigException
89
     */
90 7
    public static function validate($definition, bool $throw = true): bool
91
    {
92 7
        if ($definition instanceof DefinitionInterface) {
93
            return true;
94
        }
95
96 7
        if (\is_string($definition) && !empty($definition)) {
97 4
            return true;
98
        }
99
100 3
        if (\is_callable($definition)) {
101
            return true;
102
        }
103
104 3
        if (\is_array($definition)) {
105 1
            return true;
106
        }
107
108 2
        if (\is_object($definition)) {
109 2
            return true;
110
        }
111
112
        if ($throw) {
113
            throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
114
        }
115
116
        return false;
117
    }
118
}
119