Completed
Push — master ( 85b626...d4133d )
by Alexey
04:53
created

Options::parse()   C

Complexity

Conditions 14
Paths 150

Size

Total Lines 60
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
dl 0
loc 60
rs 5.7378
c 1
b 0
f 0
eloc 45
nc 150
nop 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
  static $options;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $options.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
  public function parse() {
19
    if (!Options::$options) {
20
      Options::$options = \Ecommerce\Item\Option::getList();
21
    }
22
    $options = [];
23
    $modelName = 'Ecommerce\Item\Option';
0 ignored issues
show
Unused Code introduced by
$modelName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
24
    foreach ($this->data['ЗначенияСвойства'] as $opt) {
25
      $optionId = \App::$cur->migrations->ids['parseIds']['Ecommerce\Item\Option'][$opt['Ид']]->object_id;
26
      if (Options::$options[$optionId]->type == 'select') {
27
        if (empty($options[$optionId])) {
28
          $options[$optionId] = [];
29
        } else {
30
          if (!Options::$options[$optionId]->advance) {
31
            Options::$options[$optionId]->advance = ['multi' => true];
32
            Options::$options[$optionId]->save();
33
          }
34
        }
35
        $options[$optionId][] = \App::$cur->migrations->ids['parseIds']['Ecommerce\Item\Option\Item'][$opt['Значение']]->object_id;
36
      } else {
37
        $options[$optionId] = $opt['Значение'];
38
      }
39
    }
40
    $itemParams = \Ecommerce\Item\Param::getList(['where' => ['item_id', $this->model->id]]);
41
    foreach ($itemParams as $itemParam) {
42
      if (Options::$options[$itemParam->item_option_id]->type == 'select') {
43
        if (empty($options[$itemParam->item_option_id]) || !in_array($itemParam->value, $options[$itemParam->item_option_id])) {
44
          $itemParam->delete();
45
        } else {
46
          unset($options[$itemParam->item_option_id][array_search($itemParam->value, $options[$itemParam->item_option_id])]);
47
        }
48
      } else {
49
        if (empty($options[$itemParam->item_option_id])) {
50
          $itemParam->delete();
51
        } else {
52
          $itemParam->value = $options[$itemParam->item_option_id];
53
          $itemParam->save();
54
          unset($options[$itemParam->item_option_id]);
55
        }
56
      }
57
    }
58
    foreach ($options as $optionId => $values) {
59
      if (is_array($values)) {
60
        foreach ($values as $value) {
61
          $itemParam = new \Ecommerce\Item\Param([
62
              'item_option_id' => $optionId,
63
              'item_id' => $this->model->id,
64
              'value' => $value
65
          ]);
66
          $itemParam->save();
67
        }
68
      } else {
69
        $itemParam = new \Ecommerce\Item\Param([
70
            'item_option_id' => $optionId,
71
            'item_id' => $this->model->id,
72
            'value' => $values
73
        ]);
74
        $itemParam->save();
75
      }
76
    }
77
  }
78
79
}
80