Action   F
last analyzed

Complexity

Total Complexity 63

Size/Duplication

Total Lines 384
Duplicated Lines 38.8 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 63
lcom 1
cbo 0
dl 149
loc 384
rs 3.36
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditArea() 0 5 2
A editIBlockElement() 20 20 5
B deleteIBlockElement() 23 23 6
A editAndDeleteIBlockElement() 0 6 1
A areaForIBlockElement() 0 4 1
A editIBlockSection() 21 21 5
B deleteIBlockSection() 23 23 6
A editAndDeleteIBlockSection() 0 6 1
A areaForIBlockSection() 0 4 1
B editHLBlockElement() 3 23 7
B deleteHLBlockElement() 3 24 8
A editAndDeleteHLBlockElement() 0 7 1
A areaForHLBlockElement() 0 4 1
A addForIBlock() 0 15 3
A getIBlockElementPanelButtons() 13 13 2
A getIBlockSectionPanelButtons() 13 13 2
A prepareIBlockElementArrayById() 15 15 4
A prepareIBlockSectionArrayById() 15 15 4
A prepareHLBlockIdByTableName() 0 15 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Action often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Action, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Arrilot\BitrixHermitage;
4
5
use Bitrix\Highloadblock\HighloadBlockTable;
6
use Bitrix\Main\Application;
7
use Bitrix\Main\Localization\Loc;
8
use CBitrixComponent;
9
use CBitrixComponentTemplate;
10
use CIBlock;
11
use InvalidArgumentException;
12
13
Loc::loadMessages(__FILE__);
14
15
class Action
16
{
17
    protected static $panelButtons = [];
18
19
    protected static $iblockElementArray = [];
20
21
    protected static $iblockSectionArray = [];
22
23
    protected static $hlblockIdsByTableName= [];
24
25
    /**
26
     * Get edit area id for specific type
27
     *
28
     * @param CBitrixComponentTemplate $template
29
     * @param $type
30
     * @param $element
31
     * @return string
32
     */
33
    public static function getEditArea($template, $type, $element)
34
    {
35
        $id = is_numeric($element) ? $element : $element['ID'];
36
        return $template->GetEditAreaId("{$type}_{$id}");
37
    }
38
39
    /**
40
     * @param CBitrixComponentTemplate $template
41
     * @param $element
42
     * @return string
43
     */
44 View Code Duplication
    public static function editIBlockElement($template, $element)
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...
45
    {
46
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
47
            return '';
48
        }
49
50
        if (is_numeric($element)) {
51
            $element = static::prepareIBlockElementArrayById($element);
52
        }
53
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
54
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
55
        }
56
57
        $buttons = static::getIBlockElementPanelButtons($element);
58
        $link = $buttons["edit"]["edit_element"]["ACTION_URL"];
59
60
        $template->AddEditAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_EDIT"));
61
62
        return static::areaForIBlockElement($template, $element);
63
    }
64
65
    /**
66
     * @param CBitrixComponentTemplate $template
67
     * @param $element
68
     * @param string $confirm
69
     * @return string
70
     */
71 View Code Duplication
    public static function deleteIBlockElement($template, $element, $confirm = null)
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...
72
    {
73
        $confirm = $confirm ?: Loc::getMessage('ARRILOT_BITRIX_HERMITAGE_DELETE_IBLOCK_ELEMENT_CONFIRM');
74
75
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
76
            return '';
77
        }
78
79
        if (is_numeric($element)) {
80
            $element = static::prepareIBlockElementArrayById($element);
81
        }
82
83
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
84
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
85
        }
86
    
87
        $buttons = static::getIBlockElementPanelButtons($element);
88
        $link = $buttons["edit"]["delete_element"]["ACTION_URL"];
89
90
        $template->AddDeleteAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_DELETE"), array("CONFIRM" => $confirm));
91
92
        return static::areaForIBlockElement($template, $element);
93
    }
94
95
    /**
96
     * @param CBitrixComponentTemplate $template
97
     * @param $element
98
     * @return string
99
     */
100
    public static function editAndDeleteIBlockElement($template, $element)
101
    {
102
        static::editIBlockElement($template, $element);
103
104
        return static::deleteIBlockElement($template, $element);
105
    }
106
107
    /**
108
     * @param CBitrixComponentTemplate $template
109
     * @param $element
110
     * @return string
111
     */
112
    public static function areaForIBlockElement($template, $element)
113
    {
114
        return static::getEditArea($template, 'iblock_element', $element);
115
    }
116
117
    /**
118
     * @param CBitrixComponentTemplate $template
119
     * @param $section
120
     * @return string
121
     */
122 View Code Duplication
    public static function editIBlockSection($template, $section)
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...
123
    {
124
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
125
            return '';
126
        }
127
128
        if (is_numeric($section)) {
129
            $section = static::prepareIBlockSectionArrayById($section);
130
        }
131
132
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
133
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
134
        }
135
    
136
        $buttons = static::getIBlockSectionPanelButtons($section);
137
        $link = $buttons["edit"]["edit_section"]["ACTION_URL"];
138
139
        $template->AddEditAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_EDIT"));
140
141
        return static::areaForIBlockSection($template, $section);
142
    }
143
144
    /**
145
     * @param CBitrixComponentTemplate $template
146
     * @param $section
147
     * @param string $confirm
148
     * @return string
149
     */
150 View Code Duplication
    public static function deleteIBlockSection($template, $section, $confirm = null)
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...
151
    {
152
        $confirm = $confirm ?: Loc::getMessage("ARRILOT_BITRIX_HERMITAGE_DELETE_IBLOCK_SECTION_CONFIRM");
153
154
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
155
            return '';
156
        }
157
158
        if (is_numeric($section)) {
159
            $section = static::prepareIBlockSectionArrayById($section);
160
        }
161
162
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
163
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
164
        }
165
    
166
        $buttons = static::getIBlockSectionPanelButtons($section);
167
        $link = $buttons["edit"]["delete_section"]["ACTION_URL"];
168
169
        $template->AddDeleteAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_DELETE"), array("CONFIRM" => $confirm));
170
171
        return static::areaForIBlockSection($template, $section);
172
    }
173
174
    /**
175
     * @param CBitrixComponentTemplate $template
176
     * @param $section
177
     * @return string
178
     */
179
    public static function editAndDeleteIBlockSection($template, $section)
180
    {
181
        static::editIBlockSection($template, $section);
182
    
183
        return static::deleteIBlockSection($template, $section);
184
    }
185
186
    /**
187
     * @param CBitrixComponentTemplate $template
188
     * @param $section
189
     * @return string
190
     */
191
    public static function areaForIBlockSection($template, $section)
192
    {
193
        return static::getEditArea($template, 'iblock_section', $section);
194
    }
195
    
196
    /**
197
     * @param CBitrixComponentTemplate $template
198
     * @param $element
199
     * @param string $label
200
     * @return string
201
     */
202
    public static function editHLBlockElement($template, $element, $label = null)
203
    {
204
        $label = $label ?: Loc::getMessage("ARRILOT_BITRIX_HERMITAGE_EDIT_HLBLOCK_ELEMENT_LABEL");
205
206
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
207
            return '';
208
        }
209
210 View Code Duplication
        if (!$element["HLBLOCK_ID"] && $element["HLBLOCK_TABLE_NAME"]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
211
            $element["HLBLOCK_ID"] = static::prepareHLBlockIdByTableName($element["HLBLOCK_TABLE_NAME"]);
212
        }
213
214
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
215
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID/HLBLOCK_TABLE_NAME');
216
        }
217
218
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?ENTITY_ID=%s&ID=%s&lang=ru&bxpublic=Y';
219
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
220
221
        $template->AddEditAction('hlblock_element_' . $element['ID'], $link, $label);
222
223
        return static::areaForHLBlockElement($template, $element);
224
    }
225
    
226
    /**
227
     * @param CBitrixComponentTemplate $template
228
     * @param $element
229
     * @param string $label
230
     * @param string $confirm
231
     * @return string
232
     */
233
    public static function deleteHLBlockElement($template, $element, $label = null, $confirm = null)
234
    {
235
        $label = $label ?: Loc::getMessage('ARRILOT_BITRIX_HERMITAGE_DELETE_HLBLOCK_ELEMENT_LABEL');
236
        $confirm = $confirm ?: Loc::getMessage('ARRILOT_BITRIX_HERMITAGE_DELETE_HLBLOCK_ELEMENT_CONFIRM');
237
238
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
239
            return '';
240
        }
241
242 View Code Duplication
        if (!$element["HLBLOCK_ID"] && $element["HLBLOCK_TABLE_NAME"]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
243
            $element["HLBLOCK_ID"] = static::prepareHLBlockIdByTableName($element["HLBLOCK_TABLE_NAME"]);
244
        }
245
246
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
247
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID/HLBLOCK_TABLE_NAME');
248
        }
249
250
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?action=delete&ENTITY_ID=%s&ID=%s&lang=ru';
251
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
252
253
        $template->AddDeleteAction('hlblock_element_' . $element['ID'], $link, $label, array("CONFIRM" => $confirm));
254
255
        return static::areaForHLBlockElement($template, $element);
256
    }
257
258
    /**
259
     * @param CBitrixComponentTemplate $template
260
     * @param $element
261
     *
262
     * @return string
263
     */
264
    public static function editAndDeleteHLBlockElement($template, $element)
265
    {
266
        static::editHLBlockElement($template, $element);
267
        static::deleteHLBlockElement($template, $element);
268
269
        return static::deleteHLBlockElement($template, $element);
270
    }
271
272
    /**
273
     * @param CBitrixComponentTemplate $template
274
     * @param $element
275
     * @return string
276
     */
277
    public static function areaForHLBlockElement($template, $element)
278
    {
279
        return static::getEditArea($template, 'hlblock_element', $element);
280
    }
281
282
    /**
283
     * @param CBitrixComponent|CBitrixComponentTemplate $componentOrTemplate
284
     * @param $iblockId
285
     * @param array $options
286
     */
287
    public static function addForIBlock($componentOrTemplate, $iblockId, $options = [])
288
    {
289
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
290
            return;
291
        }
292
293
        if ($componentOrTemplate instanceof CBitrixComponentTemplate) {
0 ignored issues
show
Bug introduced by
The class CBitrixComponentTemplate does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
294
            $componentOrTemplate = $componentOrTemplate->__component;
295
        }
296
297
        $buttons = CIBlock::GetPanelButtons($iblockId, 0, 0, $options);
298
        $menu = CIBlock::GetComponentMenu($GLOBALS['APPLICATION']->GetPublicShowMode(), $buttons);
299
300
        $componentOrTemplate->addIncludeAreaIcons($menu);
301
    }
302
303
    /**
304
     * @param $element
305
     * @return array
306
     */
307 View Code Duplication
    protected static function getIBlockElementPanelButtons($element)
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...
308
    {
309
        if (!isset(static::$panelButtons['iblock_element'][$element['ID']])) {
310
            static::$panelButtons['iblock_element'][$element['ID']] = CIBlock::GetPanelButtons(
311
                $element["IBLOCK_ID"],
312
                $element['ID'],
313
                0,
314
                ['SECTION_BUTTONS' => false, 'SESSID' => false]
315
            );
316
        }
317
318
        return static::$panelButtons['iblock_element'][$element['ID']];
319
    }
320
321
    /**
322
     * @param $section
323
     * @return array
324
     */
325 View Code Duplication
    protected static function getIBlockSectionPanelButtons($section)
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...
326
    {
327
        if (!isset(static::$panelButtons['iblock_section'][$section['ID']])) {
328
            static::$panelButtons['iblock_section'][$section['ID']] = CIBlock::GetPanelButtons(
329
                $section["IBLOCK_ID"],
330
                0,
331
                $section['ID'],
332
                ['SESSID' => false]
333
            );
334
        }
335
336
        return static::$panelButtons['iblock_section'][$section['ID']];
337
    }
338
    
339
    /**
340
     * @param int $id
341
     * @return array
342
     */
343 View Code Duplication
    protected static function prepareIBlockElementArrayById($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...
344
    {
345
        $id = (int) $id;
346
        if (!$id) {
347
            return [];
348
        }
349
350
        if (empty(static::$iblockElementArray[$id])) {
351
            $connection = Application::getConnection();
352
            $el = $connection->query("SELECT ID, IBLOCK_ID FROM b_iblock_element WHERE ID = {$id}")->fetch();
353
            static::$iblockElementArray[$id] = $el ? $el : [];
354
        }
355
356
        return static::$iblockElementArray[$id];
357
    }
358
359
    /**
360
     * @param int $id
361
     * @return array
362
     */
363 View Code Duplication
    protected static function prepareIBlockSectionArrayById($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...
364
    {
365
        $id = (int) $id;
366
        if (!$id) {
367
            return [];
368
        }
369
370
        if (empty(static::$iblockSectionArray[$id])) {
371
            $connection = Application::getConnection();
372
            $el = $connection->query("SELECT ID, IBLOCK_ID FROM b_iblock_section WHERE ID = {$id}")->fetch();
373
            static::$iblockSectionArray[$id] = $el ? $el : [];
374
        }
375
376
        return static::$iblockSectionArray[$id];
377
    }
378
379
    /**
380
     * @param string $tableName
381
     * @return int
382
     */
383
    protected static function prepareHLBlockIdByTableName($tableName)
384
    {
385
        if (empty(static::$hlblockIdsByTableName[$tableName])) {
386
            $row = HighloadBlockTable::getList([
387
                'select' => ['ID'],
388
                'filter' => ['=TABLE_NAME' => $tableName]
389
            ])->fetch();
390
391
            if (!empty($row['ID'])) {
392
                static::$hlblockIdsByTableName[$tableName] = (int) $row['ID'];
393
            }
394
        }
395
396
        return static::$hlblockIdsByTableName[$tableName];
397
    }
398
}
399