Passed
Pull Request — master (#93)
by Sergei
04:47 queued 02:36
created

Normalizer::normalize()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 42
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 42
ccs 18
cts 18
cp 1
rs 7.6666
cc 10
nc 8
nop 2
crap 10

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
 * Class Definition represents a definition in a container
17
 *
18
 * @psalm-import-type ArrayDefinitionConfig from ArrayDefinition
19
 */
20
class Normalizer
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 34
    public static function normalize($definition, string $id = null): DefinitionInterface
60
    {
61
        // Reference
62 34
        if ($definition instanceof ReferenceInterface) {
63 1
            return $definition;
64
        }
65
66 34
        if (is_string($definition)) {
67
            // Current class
68
            if (
69 14
                $id === $definition ||
70 14
                ($id === null && class_exists($definition))
71
            ) {
72
                /** @psalm-var class-string $definition */
73 6
                return ArrayDefinition::fromPreparedData($definition);
74
            }
75
76
            // Reference to another class or alias
77 8
            return Reference::to($definition);
78
        }
79
80
        // Callable definition
81 24
        if (is_callable($definition, true)) {
82 4
            return new CallableDefinition($definition);
83
        }
84
85
        // Array definition
86 20
        if (is_array($definition)) {
87 18
            $config = $definition;
88 18
            if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) {
89 1
                $config[ArrayDefinition::CLASS_NAME] = $id;
90
            }
91
            /** @psalm-var ArrayDefinitionConfig $config */
92 18
            return ArrayDefinition::fromConfig($config);
93
        }
94
95
        // Ready object
96 2
        if (is_object($definition)) {
97 1
            return new ValueDefinition($definition);
98
        }
99
100 1
        throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
101
    }
102
}
103