Test Failed
Push — refactoring ( c1e666...b19607 )
by Sergei
02:36
created

DefinitionParser::parse()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 32
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 57
ccs 27
cts 27
cp 1
crap 9
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 99
     * @param mixed $definition
54
     *
55 99
     * @throws InvalidConfigException
56 99
     */
57
    public static function parse($definition): array
58
    {
59
        if (!is_array($definition)) {
60
            return [$definition, []];
61
        }
62
63 99
        // Dedicated definition
64
        if (isset($definition[self::DEFINITION_META])) {
65 99
            $newDefinition = $definition[self::DEFINITION_META];
66 93
            unset($definition[self::DEFINITION_META]);
67
68
            return [$newDefinition, $definition];
69
        }
70 41
71 3
        // Callable definition
72 3
        if (is_callable($definition, true)) {
73 3
            return [$definition, []];
74 3
        }
75
76 3
        // Array definition
77
        $meta = [];
78
        $class = null;
79 38
        $constructorArguments = [];
80 38
        $methodsAndProperties = [];
81
        foreach ($definition as $key => $value) {
82 38
            // Class
83 3
            if ($key === ArrayDefinition::CLASS_NAME) {
84
                $class = $value;
85
                continue;
86
            }
87
88 35
            // Constructor arguments
89 32
            if ($key === ArrayDefinition::CONSTRUCTOR) {
90 24
                $constructorArguments = $value;
91 35
                continue;
92
            }
93 35
94
            // Methods and properties
95
            if (substr($key, -2) === '()') {
96 16
                $methodsAndProperties[$key] = [ArrayDefinition::FLAG_METHOD, $key, $value];
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Factory\Definiti...Definition::FLAG_METHOD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
97
                continue;
98 14
            }
99 14
            if (strncmp($key, '$', 1) === 0) {
100
                $methodsAndProperties[$key] = [ArrayDefinition::FLAG_PROPERTY, $key, $value];
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Factory\Definiti...finition::FLAG_PROPERTY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
                continue;
102 35
            }
103
104
            $meta[$key] = $value;
105
        }
106
        return [
107
            [
108 19
                $class,
109
                $constructorArguments,
110 19
                $methodsAndProperties,
111 3
                self::IS_PREPARED_ARRAY_DEFINITION_DATA => true,
112 3
            ],
113 3
            $meta,
114
        ];
115
    }
116
}
117