Passed
Push — master ( a5444c...bc8062 )
by Alexander
02:19
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 3
Bugs 0 Features 0
Metric Value
eloc 20
c 3
b 0
f 0
dl 0
loc 45
ccs 20
cts 20
cp 1
rs 6.9666
cc 12
nc 9
nop 2
crap 12

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