Test Failed
Pull Request — master (#212)
by Sergei
10:45 queued 07:10
created

DefinitionParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 34
c 5
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 count;
11
use function is_array;
12
use function is_callable;
13
14
/**
15
 * @internal Splits metadata and definition.
16
 *
17
 * Supports the following configuration:
18
 *
19
 * 1) With a dedicated definition:
20
 *
21
 * ```php
22
 * Engine::class => [
23
 *     'definition' => [
24
 *         '__class' => BigEngine::class,
25
 *         'setNumber()' => [42],
26
 *     ],
27
 *     'tags' => ['a', 'b'],
28
 *     'reset' => function () {
29
 *         $this->number = 42;
30
 *      },
31
 * ]
32
 * ```
33
 *
34
 * 2) Mixed in array definition:
35
 *
36
 * ```php
37
 * Engine::class => [
38
 *     '__class' => BigEngine::class,
39
 *     'setNumber()' => [42],
40
 *     'tags' => ['a', 'b'],
41
 *     'reset' => function () {
42
 *         $this->number = 42;
43
 *      },
44
 * ]
45
 * ```
46
 */
47
final class DefinitionParser
48
{
49
    private const DEFINITION_META = 'definition';
50
51
    public const IS_PREPARED_ARRAY_DEFINITION_DATA = 'isPreparedArrayDefinitionData';
52
53
    /**
54
     * @param mixed $definition
55
     *
56
     * @throws InvalidConfigException
57
     */
58
    public static function parse($definition): array
59
    {
60
        if (!is_array($definition)) {
61
            return [$definition, []];
62
        }
63
64
        // Dedicated definition
65
        if (isset($definition[self::DEFINITION_META])) {
66
            $newDefinition = $definition[self::DEFINITION_META];
67
            unset($definition[self::DEFINITION_META]);
68
69
            return [$newDefinition, $definition];
70
        }
71
72
        // Callable definition
73
        if (is_callable($definition, true)) {
74
            return [$definition, []];
75
        }
76
77
        // Array definition
78
        $meta = [];
79
        $class = null;
80
        $constructorArguments = [];
81
        $methodsAndProperties = [];
82
        foreach ($definition as $key => $value) {
83
            // Class
84
            if ($key === ArrayDefinition::CLASS_NAME) {
85
                $class = $value;
86
                continue;
87
            }
88
89
            // Constructor arguments
90
            if ($key === ArrayDefinition::CONSTRUCTOR) {
91
                $constructorArguments = $value;
92
                continue;
93
            }
94
95
            // Methods and properties
96
            if (count($methodArray = explode('()', $key)) === 2) {
97
                $methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $methodArray[0], $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...
98
                continue;
99
            }
100
            if (count($propertyArray = explode('$', $key)) === 2) {
101
                $methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $propertyArray[1], $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...
102
                continue;
103
            }
104
105
            $meta[$key] = $value;
106
        }
107
        return [
108
            [
109
                $class,
110
                $constructorArguments,
111
                $methodsAndProperties,
112
                self::IS_PREPARED_ARRAY_DEFINITION_DATA => true,
113
            ],
114
            $meta,
115
        ];
116
    }
117
}
118