Test Failed
Pull Request — master (#212)
by Sergei
05:58 queued 03:42
created

DefinitionParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 34
c 4
b 0
f 0
dl 0
loc 68
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 57 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Yiisoft\Factory\Definition\ArrayDefinition;
8
use Yiisoft\Factory\Exception\InvalidConfigException;
9
10
use function is_array;
11
use function is_callable;
12
13
/**
14
 * @internal Splits metadata and definition.
15
 *
16
 * Supports the following configuration:
17
 *
18
 * 1) With a dedicated definition:
19
 *
20
 * ```php
21
 * Engine::class => [
22
 *     'definition' => [
23
 *         '__class' => BigEngine::class,
24
 *         'setNumber()' => [42],
25
 *     ],
26
 *     'tags' => ['a', 'b'],
27
 *     'reset' => function () {
28
 *         $this->number = 42;
29
 *      },
30
 * ]
31
 * ```
32
 *
33
 * 2) Mixed in array definition:
34
 *
35
 * ```php
36
 * Engine::class => [
37
 *     '__class' => BigEngine::class,
38
 *     'setNumber()' => [42],
39
 *     'tags' => ['a', 'b'],
40
 *     'reset' => function () {
41
 *         $this->number = 42;
42
 *      },
43
 * ]
44
 * ```
45
 */
46
final class DefinitionParser
47
{
48
    private const DEFINITION_META = 'definition';
49
50
    public const IS_PREPARED_ARRAY_DEFINITION_DATA = 'isPreparedArrayDefinitionData';
51
52
    /**
53
     * @param mixed $definition
54
     *
55
     * @throws InvalidConfigException
56
     */
57
    public static function parse($definition): array
58
    {
59
        if (!is_array($definition)) {
60
            return [$definition, []];
61
        }
62
63
        // Dedicated definition
64
        if (isset($definition[self::DEFINITION_META])) {
65
            $newDefinition = $definition[self::DEFINITION_META];
66
            unset($definition[self::DEFINITION_META]);
67
68
            return [$newDefinition, $definition];
69
        }
70
71
        // Callable definition
72
        if (is_callable($definition, true)) {
73
            return [$definition, []];
74
        }
75
76
        // Array definition
77
        $meta = [];
78
        $class = null;
79
        $constructorArguments = [];
80
        $methodsAndProperties = [];
81
        foreach ($definition as $key => $value) {
82
            // Class
83
            if ($key === ArrayDefinition::CLASS_NAME) {
84
                $class = $value;
85
                continue;
86
            }
87
88
            // Constructor arguments
89
            if ($key === ArrayDefinition::CONSTRUCTOR) {
90
                $constructorArguments = $value;
91
                continue;
92
            }
93
94
            // Methods and properties
95
            if (substr($key, -2) === '()') {
96
                $methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $key, $value];
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Factory\Definiti...Definition::TYPE_METHOD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
97
                continue;
98
            }
99
            if (strncmp($key, '$', 1) === 0) {
100
                $methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $key, $value];
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Factory\Definiti...finition::TYPE_PROPERTY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
                continue;
102
            }
103
104
            $meta[$key] = $value;
105
        }
106
        return [
107
            [
108
                $class,
109
                $constructorArguments,
110
                $methodsAndProperties,
111
                self::IS_PREPARED_ARRAY_DEFINITION_DATA => true,
112
            ],
113
            $meta,
114
        ];
115
    }
116
}
117