Test Failed
Push — master ( afbf72...42b272 )
by Alexey
04:43
created

Info   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 68
loc 68
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A relations() 12 12 1
B realType() 20 20 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Cart info
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce\Cart;
13
14 View Code Duplication
class Info extends \Model {
15
16
    public static $objectName = 'Информация';
17
    public static $labels = [
18
        'name' => 'Название',
19
        'value' => 'Значение',
20
        'useradds_field_id' => 'Поле',
21
        'cart_id' => 'Корзина'
22
    ];
23
    public static $cols = [
24
        'name' => ['type' => 'text'],
25
        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'],
26
        'cart_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'cart'],
27
        'useradds_field_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'field'],
28
        //Системные параметры
29
        'date_create' => ['type' => 'dateTime'],
30
    ];
31
    public static $dataManagers = [
32
        'manager' => [
33
            'cols' => [
34
                'name',
35
                'value',
36
            ],
37
        ],
38
    ];
39
    public static $forms = [
40
        'manager' => [
41
            'map' => [
42
                ['name', 'value'],
43
                ['useradds_field_id', 'cart_id'],
44
            ]
45
        ]
46
    ];
47
48
    public static function relations() {
49
        return [
50
            'cart' => [
51
                'model' => 'Ecommerce\Cart',
52
                'col' => 'cart_id'
53
            ],
54
            'field' => [
55
                'model' => 'Ecommerce\UserAdds\Field',
56
                'col' => 'useradds_field_id'
57
            ]
58
        ];
59
    }
60
61
    public function realType() {
62
        if ($this->field && $this->field->type) {
63
            $type = $this->field->type;
64
65
            if ($type == 'select') {
66
                return [
67
                    'type' => 'select',
68
                    'source' => 'relation',
69
                    'relation' => 'field:fieldItems',
70
                ];
71
            } elseif ($type === 'autocomplete') {
72
                return [
73
                    'type' => 'autocomplete',
74
                    'options' => json_decode($this->field->options, true)
75
                ];
76
            }
77
            return $type;
78
        }
79
        return 'text';
80
    }
81
}
82