IBlockType   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 143
Duplicated Lines 12.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 18
loc 143
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 9 9 2
A update() 9 9 2
A delete() 0 8 2
A setId() 0 6 1
A setSections() 0 6 2
A setEditFileBefore() 0 6 1
A setEditFileAfter() 0 6 1
A setInRss() 0 6 2
A setSort() 0 6 1
A setLang() 0 15 3

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
namespace Arrilot\BitrixMigrations\Constructors;
5
6
7
use Arrilot\BitrixMigrations\Logger;
8
use Bitrix\Main\Application;
9
10
class IBlockType
11
{
12
    use FieldConstructor;
13
14
    /**
15
     * Добавить тип инфоблока
16
     * @throws \Exception
17
     */
18 View Code Duplication
    public function add()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        $obj = new \CIBlockType();
21
        if (!$obj->Add($this->getFieldsWithDefault())) {
22
            throw new \Exception($obj->LAST_ERROR);
23
        }
24
25
        Logger::log("Добавлен тип инфоблока {$this->fields['ID']}", Logger::COLOR_GREEN);
26
    }
27
28
    /**
29
     * Обновить тип инфоблока
30
     * @param $id
31
     * @throws \Exception
32
     */
33 View Code Duplication
    public function update($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $obj = new \CIBlockType();
36
        if (!$obj->Update($id, $this->fields)) {
37
            throw new \Exception($obj->LAST_ERROR);
38
        }
39
40
        Logger::log("Обновлен тип инфоблока {$id}", Logger::COLOR_GREEN);
41
    }
42
43
    /**
44
     * Удалить тип инфоблока
45
     * @param $id
46
     * @throws \Exception
47
     */
48
    public static function delete($id)
49
    {
50
        if (!\CIBlockType::Delete($id)) {
51
            throw new \Exception('Ошибка при удалении типа инфоблока');
52
        }
53
54
        Logger::log("Удален тип инфоблока {$id}", Logger::COLOR_GREEN);
55
    }
56
57
    /**
58
     * ID типа информационных блоков. Уникален.
59
     * @param string $id
60
     * @return $this
61
     */
62
    public function setId($id)
63
    {
64
        $this->fields['ID'] = $id;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Разделяются ли элементы блока этого типа по разделам.
71
     * @param bool $has
72
     * @return $this
73
     */
74
    public function setSections($has = true)
75
    {
76
        $this->fields['SECTIONS'] = $has ? 'Y' : 'N';
77
78
        return $this;
79
    }
80
81
    /**
82
     * Полный путь к файлу-обработчику массива полей элемента перед сохранением на странице редактирования элемента.
83
     * @param string $editFileBefore
84
     * @return $this
85
     */
86
    public function setEditFileBefore($editFileBefore)
87
    {
88
        $this->fields['EDIT_FILE_BEFORE'] = $editFileBefore;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Полный путь к файлу-обработчику вывода интерфейса редактирования элемента.
95
     * @param string $editFileAfter
96
     * @return $this
97
     */
98
    public function setEditFileAfter($editFileAfter)
99
    {
100
        $this->fields['EDIT_FILE_AFTER'] = $editFileAfter;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Блоки данного типа экспортировать в RSS
107
     * @param bool $inRss
108
     * @return $this
109
     */
110
    public function setInRss($inRss = false)
111
    {
112
        $this->fields['IN_RSS'] = $inRss ? 'Y' : 'N';
113
114
        return $this;
115
    }
116
117
    /**
118
     * Порядок сортировки типа
119
     * @param int $sort
120
     * @return $this
121
     */
122
    public function setSort($sort = 500)
123
    {
124
        $this->fields['SORT'] = $sort;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Указать языковые фразы
131
     * @param string $lang ключ языка (ru)
132
     * @param string $name
133
     * @param string $sectionName
134
     * @param string $elementName
135
     * @return $this
136
     */
137
    public function setLang($lang, $name, $sectionName = null, $elementName = null)
138
    {
139
        $setting = ['NAME' => $name];
140
141
        if ($sectionName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sectionName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
142
            $setting['SECTION_NAME'] = $sectionName;
143
        }
144
        if ($elementName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $elementName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
145
            $setting['ELEMENT_NAME'] = $elementName;
146
        }
147
148
        $this->fields['LANG'][$lang] = $setting;
149
150
        return $this;
151
    }
152
}