Passed
Pull Request — master (#241)
by Dmitriy
02:20
created

DefinitionNormalizer::normalize()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 4
nop 2
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Yiisoft\Factory\Definition\ArrayDefinition;
8
use Yiisoft\Factory\Definition\DefinitionInterface;
9
use Yiisoft\Factory\Definition\Normalizer;
10
use Yiisoft\Factory\Exception\InvalidConfigException;
11
use function is_array;
12
13
/**
14
 * @internal
15
 */
16
final class DefinitionNormalizer
17
{
18
    /**
19
     * @param mixed $definition
20
     *
21
     * @throws InvalidConfigException
22
     */
23 81
    public static function normalize($definition, string $id = null): DefinitionInterface
24
    {
25 81
        if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) {
26 34
            $class = $definition['class'];
27 34
            $constructorArguments = $definition['__construct()'];
28 34
            $methodsAndProperties = $definition['methodsAndProperties'];
29
30 34
            $class = $class ?? $id;
31 34
            if ($class === null) {
32
                throw new InvalidConfigException('Invalid definition: don\'t set class name.');
33
            }
34
35 34
            return ArrayDefinition::fromPreparedData($class, $constructorArguments, $methodsAndProperties);
36
        }
37
38 81
        if ($definition instanceof ExtensibleService) {
39 3
            return $definition;
40
        }
41
42 81
        return Normalizer::normalize($definition, $id);
43
    }
44
}
45