Completed
Branch master (ebb499)
by Alexey
04:15
created

Param   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 111
loc 111
rs 10
c 1
b 1
f 0
wmc 9
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A indexes() 23 23 1
A realType() 15 15 3
A name() 3 3 1
A value() 8 8 3
A relations() 20 20 1

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
 * Item param
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\Item;
13
14 View Code Duplication
class Param extends \Model {
15
16
    public static $objectName = 'Параметр товара';
17
    public static $labels = [
18
        'item_option_id' => 'Параметр',
19
        'item_id' => 'Товар',
20
        'value' => 'Значение',
21
    ];
22
    public static $cols = [
23
        //Основные параметры
24
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
25
        'item_option_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'option', 'onChange' => 'reloadForm'],
26
        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'],
27
        //Системные
28
        'date_create' => ['type' => 'dateTime']
29
    ];
30
31
    public static function indexes() {
32
        return [
33
            'ecommerce_itemOptionRelation' => [
34
                'type' => 'INDEX',
35
                'cols' => [
36
                    'item_param_item_id',
37
                    'item_param_item_option_id'
38
                ]
39
            ],
40
            'ecommerce_paramItemIndex' => [
41
                'type' => 'INDEX',
42
                'cols' => [
43
                    'item_param_item_id',
44
                ]
45
            ],
46
            'ecommerce_paramOptionIndex' => [
47
                'type' => 'INDEX',
48
                'cols' => [
49
                    'item_param_item_option_id'
50
                ]
51
            ],
52
        ];
53
    }
54
55
    public function realType() {
56
        if ($this->option) {
57
            $type = $this->option->type;
58
59
            if ($type == 'select') {
60
                return [
61
                    'type' => 'select',
62
                    'source' => 'relation',
63
                    'relation' => 'option:items',
64
                ];
65
            }
66
            return $type;
67
        }
68
        return 'text';
69
    }
70
71
    public static $dataManagers = [
72
73
        'manager' => [
74
            'name' => 'Параметры товара',
75
            'cols' => [
76
                'item_option_id',
77
                'item_id',
78
                'value',
79
            ],
80
        ],
81
    ];
82
    public static $forms = [
83
        'manager' => [
84
            'map' => [
85
                ['item_id', 'item_option_id'],
86
                ['value']
87
            ]
88
    ]];
89
90
    function name() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
        return $this->value;
92
    }
93
94
    public function value($default = '') {
95
        if ($this->option->type != 'select') {
96
            return $this->value;
97
        } elseif ($this->optionItem) {
98
            return $this->optionItem->value;
99
        }
100
        return $default;
101
    }
102
103
    public static function relations() {
104
        return [
105
            'file' => [
106
                'model' => 'Files\File',
107
                'col' => 'value'
108
            ],
109
            'option' => [
110
                'model' => 'Ecommerce\Item\Option',
111
                'col' => 'item_option_id'
112
            ],
113
            'item' => [
114
                'model' => 'Ecommerce\Item',
115
                'col' => 'item_id'
116
            ],
117
            'optionItem' => [
118
                'model' => 'Ecommerce\Item\Option\Item',
119
                'col' => 'value'
120
            ]
121
        ];
122
    }
123
124
}
125