Test Failed
Push — master ( 45bc78...882099 )
by Alexey
05:03
created

Catalog   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 30.14 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 22
loc 73
rs 10
c 2
b 0
f 2
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A calcItemsCount() 0 13 4
A relations() 22 22 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
 * INJI
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2017 Alexey Krupskiy
8
 * @license https://github.com/injitools/Inji/blob/master/LICENSE
9
 */
10
11
namespace Ecommerce;
12
13
/**
14
 * @property int $id
15
 * @property string $name
16
 * @property int $weight
17
 * @property int $parent_id
18
 * @property int $icon_file_id
19
 * @property int $items_count
20
 * @property \Ecommerce\Catalog $parent
21
 * @property \Ecommerce\Catalog[] $childs
22
 * @property \Ecommerce\Catalog\Category[] $categories
23
 * @property \Files\File $icon
24
 */
25
class Catalog extends \Model {
26
    static $cols = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $cols.

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...
27
        'name' => ['type' => 'text'],
28
        'weight' => ['type' => 'number'],
29
        'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent', 'extraValues' => ['0' => 'Нет родителя']],
30
        'icon_file_id' => ['type' => 'image'],
31
        'items_count' => ['type' => 'number'],
32
        'childsMgr' => ['type' => 'dataManager', 'relation' => 'childs'],
33
        'categoriesMgr' => ['type' => 'dataManager', 'relation' => 'categories'],
34
    ];
35
    static $labels = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $labels.

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...
36
        'name' => 'Название',
37
        'weight' => 'Вес соритровки',
38
        'parent_id' => 'Родитель',
39
        'icon_file_id' => 'Иконка',
40
        'childsMgr' => 'Дочерние каталоги',
41
        'categoriesMgr' => 'Категории товаров',
42
    ];
43
    static $dataManagers = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dataManagers.

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...
44
        'manager' => [
45
            'filters' => ['name', 'parent_id'],
46
            'cols' => ['name', 'icon_file_id', 'childsMgr', 'categoriesMgr'],
47
            'sortMode' => true
48
        ]
49
    ];
50
    static $forms = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $forms.

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...
51
        'manager' => [
52
            'map' => [
53
                ['name', 'icon_file_id'],
54
                ['categoriesMgr'],
55
                ['childsMgr'],
56
57
            ]
58
        ]
59
    ];
60
61
    public function calcItemsCount($save = true) {
62
        $count = 0;
63
        foreach ($this->categories as $category) {
64
            $count += $category->category->items_count;
65
        }
66
        $this->items_count = $count;
67
        if ($save) {
68
            $this->save();
69
            if ($this->parent) {
70
                $this->parent->calcItemsCount();
71
            }
72
        }
73
    }
74
75 View Code Duplication
    static function relations() {
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...
76
        return [
77
            'parent' => [
78
                'model' => 'Ecommerce\Catalog',
79
                'col' => 'parent_id'
80
            ],
81
            'childs' => [
82
                'type' => 'many',
83
                'col' => 'parent_id',
84
                'model' => 'Ecommerce\Catalog'
85
            ],
86
            'categories' => [
87
                'type' => 'many',
88
                'col' => 'catalog_id',
89
                'model' => 'Ecommerce\Catalog\Category'
90
            ],
91
            'icon' => [
92
                'col' => 'icon_file_id',
93
                'model' => 'Files\File'
94
            ]
95
        ];
96
    }
97
}