Passed
Push — master ( 8afadc...1cb41c )
by Alexander
03:09 queued 54s
created

Normalizer::normalize()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 45
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 20
nc 9
nop 2
dl 0
loc 45
ccs 20
cts 20
cp 1
crap 12
rs 6.9666
c 0
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\Definitions\Helpers;
6
7
use Yiisoft\Definitions\ArrayDefinition;
8
use Yiisoft\Definitions\CallableDefinition;
9
use Yiisoft\Definitions\Contract\DefinitionInterface;
10
use Yiisoft\Definitions\Contract\ReferenceInterface;
11
use Yiisoft\Definitions\Exception\InvalidConfigException;
12
13
use Yiisoft\Definitions\Reference;
14
15
use Yiisoft\Definitions\ValueDefinition;
16
17
use function array_key_exists;
18
use function is_array;
19
use function is_callable;
20
use function is_object;
21
use function is_string;
22
23
/**
24
 * Normalizer definition from configuration to an instance of {@see DefinitionInterface}.
25
 *
26
 * @psalm-import-type ArrayDefinitionConfig from ArrayDefinition
27
 */
28
final class Normalizer
29
{
30
    /**
31
     * Normalize definition to an instance of {@see DefinitionInterface}.
32
     * Definition may be defined multiple ways:
33
     *  - class name,
34
     *  - string as reference to another class or alias,
35
     *  - instance of {@see ReferenceInterface},
36
     *  - callable,
37
     *  - array,
38
     *  - ready object.
39
     *
40
     * @param mixed $definition The definition for normalization.
41
     * @param string $class The class name of the object to be defined (optional). It is used in two cases.
42
     *  - The definition is a string, and class name equals to definition. Returned `ArrayDefinition` with defined
43
     *    class.
44
     *  - The definition is an array without class name. Class name will be added to array and `ArrayDefinition`
45
     *    will be returned.
46
     *
47
     * @throws InvalidConfigException If configuration is not valid.
48
     *
49
     * @return DefinitionInterface Normalized definition as an object.
50
     */
51 13
    public static function normalize($definition, string $class = null): DefinitionInterface
52
    {
53
        // Reference
54 13
        if ($definition instanceof ReferenceInterface) {
55 1
            return $definition;
56
        }
57
58 12
        if (is_string($definition)) {
59
            // Current class
60
            if (
61 4
                $class === $definition ||
62 4
                ($class === null && class_exists($definition))
63
            ) {
64
                /** @psalm-var class-string $definition */
65 1
                return ArrayDefinition::fromPreparedData($definition);
66
            }
67
68
            // Reference to another class or alias
69 3
            return Reference::to($definition);
70
        }
71
72
        // Callable definition
73 8
        if (is_callable($definition, true)) {
74 3
            return new CallableDefinition($definition);
75
        }
76
77
        // Array definition
78 5
        if (is_array($definition)) {
79 3
            $config = $definition;
80 3
            if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) {
81 2
                if ($class === null) {
82 1
                    throw new InvalidConfigException('Array definition should contain the key "class": ' . var_export($definition, true));
83
                }
84 1
                $config[ArrayDefinition::CLASS_NAME] = $class;
85
            }
86
            /** @psalm-var ArrayDefinitionConfig $config */
87 2
            return ArrayDefinition::fromConfig($config);
88
        }
89
90
        // Ready object
91 2
        if (is_object($definition) && !($definition instanceof DefinitionInterface)) {
92 1
            return new ValueDefinition($definition);
93
        }
94
95 1
        throw new InvalidConfigException('Invalid definition: ' . var_export($definition, true));
96
    }
97
}
98