Options::parse()   F
last analyzed

Complexity

Conditions 29
Paths 14094

Size

Total Lines 90
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 29
eloc 63
nc 14094
nop 0
dl 0
loc 90
rs 2.0585
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
/**
4
 * Item images parser
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2016 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Exchange1c\Parser\Item;
13
14
class Options extends \Migrations\Parser {
15
16
    public static $options;
17
18
    public function parse() {
19
        if (!Options::$options) {
20
            Options::$options = \Ecommerce\Item\Option::getList();
0 ignored issues
show
Bug introduced by
The type Ecommerce\Item\Option was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
        }
22
23
        if (!empty($this->data['ЗначенияСвойства'])) {
24
            if (\Tools::isAssoc($this->data['ЗначенияСвойства'])) {
0 ignored issues
show
Bug introduced by
The type Tools was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
                $data = [$this->data['ЗначенияСвойства']];
26
            } else {
27
                $data = &$this->data['ЗначенияСвойства'];
28
            }
29
        } else {
30
            $data = [];
31
        }
32
33
34
        $options = [];
35
        $itemParams = \Ecommerce\Item\Param::getList(['where' => ['item_id', $this->model->id],'key'=>'item_option_id']);
0 ignored issues
show
Bug introduced by
The type Ecommerce\Item\Param was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
        foreach ($this->object->object->params as $param) {
37
            if ($param->type == 'paramValue' && isset($itemParams[$param->value])) {
38
                $options[$param->value] = $itemParams[$param->value]->value;
39
            }
40
        }
41
        foreach ($data as $opt) {
42
            if (empty($opt['Ид'])) {
43
                var_dump($data, $opt);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($data, $opt) looks like debug code. Are you sure you do not want to remove it?
Loading history...
44
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
45
            }
46
            $optionId = \App::$cur->migrations->findObject($opt['Ид'], 'Ecommerce\Item\Option');
47
            if ($optionId) {
48
                $optionId = $optionId->object_id;
49
            }
50
            if ($optionId && !isset(Options::$options[$optionId])) {
51
                Options::$options = \Ecommerce\Item\Option::getList();
52
            }
53
            if (isset(Options::$options[$optionId]) && Options::$options[$optionId]->type == 'select') {
54
                if (empty($options[$optionId])) {
55
                    $options[$optionId] = [];
56
                } else {
57
                    if (!Options::$options[$optionId]->advance) {
58
                        Options::$options[$optionId]->advance = ['multi' => true];
59
                        Options::$options[$optionId]->save();
60
                    }
61
                }
62
                if ($opt['Значение'] && $value = \App::$cur->migrations->findObject($opt['Значение'], 'Ecommerce\Item\Option\Item')) {
63
                    $options[$optionId][] = $value->object_id;
64
                }
65
            } else {
66
                $options[$optionId] = $opt['Значение'];
67
            }
68
        }
69
70
71
        foreach ($itemParams as $itemParam) {
72
            if ($itemParam->item_option_id && !isset(Options::$options[$itemParam->item_option_id])) {
73
                Options::$options = \Ecommerce\Item\Option::getList();
74
            }
75
            if (isset(Options::$options[$itemParam->item_option_id]) && Options::$options[$itemParam->item_option_id]->type == 'select') {
76
                if (empty($options[$itemParam->item_option_id]) || !in_array($itemParam->value, $options[$itemParam->item_option_id])) {
77
                    $itemParam->delete();
78
                } else {
79
                    unset($options[$itemParam->item_option_id][array_search($itemParam->value, $options[$itemParam->item_option_id])]);
80
                }
81
            } else {
82
                if (empty($options[$itemParam->item_option_id])) {
83
                    $itemParam->delete();
84
                } else {
85
                    $itemParam->value = $options[$itemParam->item_option_id];
86
                    $itemParam->save();
87
                    unset($options[$itemParam->item_option_id]);
88
                }
89
            }
90
        }
91
        foreach ($options as $optionId => $values) {
92
            if (is_array($values)) {
93
                foreach ($values as $value) {
94
                    $itemParam = new \Ecommerce\Item\Param([
95
                        'item_option_id' => $optionId,
96
                        'item_id' => $this->model->id,
97
                        'value' => $value
98
                    ]);
99
                    $itemParam->save();
100
                }
101
            } else {
102
                $itemParam = new \Ecommerce\Item\Param([
103
                    'item_option_id' => $optionId,
104
                    'item_id' => $this->model->id,
105
                    'value' => $values
106
                ]);
107
                $itemParam->save();
108
            }
109
        }
110
    }
111
}