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

DefinitionParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 77
ccs 33
cts 33
cp 1
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 64 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di\Helpers;
6
7
use Yiisoft\Definitions\ArrayDefinition;
8
9
use function count;
10
use function is_array;
11
use function is_callable;
12
use function is_string;
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 Definition to parse.
55
     *
56
     * @return array Definition parsed into array of a special structure.
57
     *
58
     * @psalm-return array{mixed,array}
59
     */
60 106
    public static function parse($definition): array
61
    {
62 106
        if (!is_array($definition)) {
63 77
            return [$definition, []];
64
        }
65
66
        // Dedicated definition
67 52
        if (isset($definition[self::DEFINITION_META])) {
68
            /** @var mixed $newDefinition */
69 3
            $newDefinition = $definition[self::DEFINITION_META];
70 3
            unset($definition[self::DEFINITION_META]);
71
72 3
            return [$newDefinition, $definition];
73
        }
74
75
        // Callable definition
76 49
        if (is_callable($definition, true)) {
77 3
            return [$definition, []];
78
        }
79
80
        // Array definition
81 46
        $meta = [];
82 46
        $class = null;
83 46
        $constructorArguments = [];
84 46
        $methodsAndProperties = [];
85
        /** @var mixed $value */
86 46
        foreach ($definition as $key => $value) {
87 46
            if (is_string($key)) {
88
                // Class
89 46
                if ($key === ArrayDefinition::CLASS_NAME) {
90
                    /** @var mixed $class */
91 43
                    $class = $value;
92 43
                    continue;
93
                }
94
95
                // Constructor arguments
96 45
                if ($key === ArrayDefinition::CONSTRUCTOR) {
97
                    /** @var mixed $constructorArguments */
98 13
                    $constructorArguments = $value;
99 13
                    continue;
100
                }
101
102
                // Methods and properties
103 35
                if (count($methodArray = explode('()', $key, 2)) === 2) {
104 17
                    $methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $methodArray[0], $value];
105 17
                    continue;
106
                }
107 27
                if (count($propertyArray = explode('$', $key, 2)) === 2) {
108 5
                    $methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $propertyArray[1], $value];
109 5
                    continue;
110
                }
111
            }
112
113
            /** @var mixed */
114 22
            $meta[$key] = $value;
115
        }
116
        return [
117
            [
118 46
                'class' => $class,
119 46
                '__construct()' => $constructorArguments,
120 46
                'methodsAndProperties' => $methodsAndProperties,
121 46
                self::IS_PREPARED_ARRAY_DEFINITION_DATA => true,
122
            ],
123 46
            $meta,
124
        ];
125
    }
126
}
127