Passed
Pull Request — master (#233)
by Dmitriy
02:42
created

DefinitionNormalizer::normalize()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 5.025
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 75
    public static function normalize($definition, string $id = null): DefinitionInterface
24
    {
25 75
        if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) {
26 30
            [$class, $constructorArguments, $methodsAndProperties] = $definition;
27
28 30
            $class = $class ?? $id;
29 30
            if ($class === null) {
30
                throw new InvalidConfigException('Invalid definition: don\'t set class name.');
31
            }
32
33 30
            return ArrayDefinition::fromPreparedData($class, $constructorArguments, $methodsAndProperties);
34
        }
35
36 75
        if ($definition instanceof ExtensibleService) {
37 2
            return $definition;
38
        }
39
40 75
        return Normalizer::normalize($definition, $id);
41
    }
42
}
43