Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

drawParam()   F

Complexity

Conditions 13
Paths 528

Size

Total Lines 116
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 87
nc 528
nop 5
dl 0
loc 116
rs 2.8999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
$form = new Ui\Form();
4
$form->begin('Карта миграции');
5
6
function drawPath($path, $form, $models, $objects) {
7
    $form->input('select', 'type[' . $path->id . ']', $path->item, [
8
        'values' => [
9
    '' => 'Выберите',
10
    'continue' => 'Пропустить',
11
    'container' => 'Контейнер',
12
    'object' => [
13
        'text' => 'Объект',
14
        'input' => [
15
            'name' => 'typeOptions[' . $path->id . ']',
16
            'type' => 'select',
17
            'source' => 'array',
18
            'sourceArray' => $models
19
        ]
20
    ]
21
        ] + $objects,
22
        'value' => $path->type != 'object' ? $path->type : $path->object_id,
23
    ]);
24
    foreach ($path->childs as $path) {
25
        echo '<div class="col-xs-offset-1">';
26
        drawPath($path, $form, $models, $objects);
27
        echo '</div>';
28
    }
29
}
30
31
foreach ($map->paths(['where' => ['parent_id', 0]]) as $path) {
32
    drawPath($path, $form, $models, $objects);
33
}
34
35
function drawParam($param, $form, $models, $objects, $parent = 0) {
36
    $selectArrays = [];
37
    $objectsCols = [];
38
39
    if (!isset($selectArrays[$param->object->migration_id])) {
40
        $selectArrays[$param->object->migration_id] = Migrations\Migration\Object::getList(['where' => ['migration_id', $param->object->migration_id], 'forSelect' => true]);
41
42
        $selectArrays[$param->object->migration_id] = [
43
            '' => 'Выберите',
44
            'continue' => 'Пропустить',
45
            'container' => 'Контейнер'
46
                ] + $selectArrays[$param->object->migration_id];
47
    }
48
49
    if (empty($objectsCols[$param->object_id])) {
50
        $modelName = $param->object->model;
51
        foreach (array_keys($modelName::$cols) as $colName) {
52
            $objectsCols[$param->object_id][$colName] = !empty($modelName::$labels[$colName]) ? $modelName::$labels[$colName] : $colName;
53
        }
54
    }
55
    $modelName = $param->object->model;
56
    $relations = [];
57
    foreach ($modelName::relations() as $relName => $relation) {
58
        $relations[$relName] = $relName;
59
    }
60
    if ($parent) {
61
        $parserName = '\Migrations\Parser\Object\\' . ucfirst($parent->type);
62
        $parser = new $parserName;
63
        $parser->param = $parent;
64
        $values = $parser->editor();
65
    } else {
66
        $values = [
67
            '' => 'Выберите',
68
            'continue' => 'Пропустить',
69
            'item_key' => 'Ключ элемента',
70
            'paramsList' => 'Список параметров',
71
            'param' => 'Параметр',
72
            'value' => [
73
                'text' => 'Значение',
74
                'input' => [
75
                    'name' => 'paramOptions[' . $param->id . ']',
76
                    'type' => 'select',
77
                    'source' => 'array',
78
                    'sourceArray' => $objectsCols[$param->object_id],
79
                    'options' => [
80
                        'value' => $param->type == 'value' ? $param->value : ''
81
                    ]
82
                ]
83
            ],
84
            'relation' => [
85
                'text' => 'Зависимость',
86
                'input' => [
87
                    'name' => 'paramOptions[' . $param->id . ']',
88
                    'type' => 'select',
89
                    'source' => 'array',
90
                    'sourceArray' => $relations,
91
                    'options' => [
92
                        'value' => $param->type == 'relation' ? $param->value : ''
93
                    ]
94
                ]
95
            ],
96
            'object' => [
97
                'text' => 'Объект',
98
                'input' => [
99
                    'name' => 'paramOptions[' . $param->id . ']',
100
                    'type' => 'select',
101
                    'source' => 'array',
102
                    'sourceArray' => $selectArrays[$param->object->migration_id],
103
                    'options' => [
104
                        'value' => $param->type == 'object' ? $param->value : ''
105
                    ]
106
                ]
107
            ],
108
            'objectLink' => [
109
                'text' => 'Ссылка на объект',
110
                'input' => [
111
                    'name' => 'paramOptions[' . $param->id . ']',
112
                    'type' => 'select',
113
                    'source' => 'array',
114
                    'sourceArray' => $selectArrays[$param->object->migration_id],
115
                    'options' => [
116
                        'value' => $param->type == 'objectLink' ? $param->value : ''
117
                    ]
118
                ]
119
            ],
120
            'newObject' => [
121
                'text' => 'Новый объект',
122
                'input' => [
123
                    'name' => 'paramOptions[' . $param->id . ']',
124
                    'type' => 'select',
125
                    'source' => 'array',
126
                    'sourceArray' => $models,
127
                    'options' => [
128
                        'value' => $param->type == 'newObject' ? $param->value : ''
129
                    ]
130
                ]
131
            ],
132
            'custom' => [
133
                'text' => 'Свой класс обработки',
134
                'input' => [
135
                    'name' => 'paramOptions[' . $param->id . ']',
136
                    'type' => 'text',
137
                    'options' => [
138
                        'value' => $param->value
139
                    ]
140
                ]
141
            ]
142
        ];
143
    }
144
    $form->input('select', 'param[' . $param->id . ']', $param->code, ['values' => $values,
145
        'value' => $param->type
146
    ]);
147
    foreach ($param->childs as $child) {
148
        echo '<div class="col-xs-offset-1">';
149
        drawParam($child, $form, $models, $objects, $param);
150
        echo '</div>';
151
    }
152
}
153
154
echo "<h2>Объекты</h2>";
155
foreach ($map->migration->objects as $object) {
156
    echo "<h4>{$object->name}</h4>";
157
    foreach ($object->params as $param) {
158
        echo '<div class="col-xs-offset-1">';
159
        drawParam($param, $form, $models, $objects);
160
        echo '</div>';
161
    }
162
}
163
$form->end();
164