Passed
Push — master ( 0b1876...128832 )
by Alexander
02:14
created

DefinitionNormalizer::normalize()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di\Helpers;
6
7
use Yiisoft\Definitions\ArrayDefinition;
8
use Yiisoft\Definitions\Contract\DefinitionInterface;
9
use Yiisoft\Definitions\Exception\InvalidConfigException;
10
use Yiisoft\Definitions\Helpers\Normalizer;
11
use Yiisoft\Di\ExtensibleService;
12
13
use function is_array;
14
15
/**
16
 * @internal Normalizes a definition.
17
 */
18
final class DefinitionNormalizer
19
{
20
    /**
21
     * @param mixed $definition Definition to normalize.
22
     * @param string $id Service ID.
23
     *
24
     * @throws InvalidConfigException If configuration is not valid.
25
     */
26 84
    public static function normalize($definition, string $id): DefinitionInterface
27
    {
28 84
        if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) {
29
            /** @psalm-suppress MixedArgument Definition should be valid {@see Container::$validate} */
30 38
            return ArrayDefinition::fromPreparedData(
31 38
                $definition['class'] ?? $id,
32 38
                $definition['__construct()'],
33 38
                $definition['methodsAndProperties']
34
            );
35
        }
36
37 84
        if ($definition instanceof ExtensibleService) {
38 5
            return $definition;
39
        }
40
41 84
        return Normalizer::normalize($definition, $id);
42
    }
43
}
44