UserWidget   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 31.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 26
loc 83
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditHtml() 0 23 3
A getValueReadonly() 13 21 4
B generateRow() 13 25 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
namespace DigitalWand\AdminHelper\Widget;
4
5
use Bitrix\Main\UserTable;
6
7
/**
8
 * Виджет для вывода пользователя.
9
 *
10
 * Доступные опции:
11
 * <ul>
12
 * <li> STYLE - inline-стили
13
 * <li> SIZE - значение атрибута size для input
14
 * </ul>
15
 *
16
 * @author Nik Samokhvalov <[email protected]>
17
 */
18
class UserWidget extends NumberWidget
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function getEditHtml()
24
    {
25
        $style = $this->getSettings('STYLE');
26
        $size = $this->getSettings('SIZE');
27
28
        $userId = $this->getValue();
29
30
        $htmlUser = '';
31
32
        if (!empty($userId) && $userId != 0) {
33
            $rsUser = UserTable::getById($userId);
34
            $user = $rsUser->fetch();
35
36
            $htmlUser = '[<a href="user_edit.php?lang=ru&ID=' . $user['ID'] . '">' . $user['ID'] . '</a>] ('
37
                . $user['EMAIL'] . ') ' . $user['NAME'] . '&nbsp;' . $user['LAST_NAME'];
38
        }
39
40
        return '<input type="text"
41
                       name="' . $this->getEditInputName() . '"
42
                       value="' . static::prepareToTagAttr($this->getValue()) . '"
43
                       size="' . $size . '"
44
                       style="' . $style . '"/>' . $htmlUser;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function getValueReadonly()
51
    {
52
        $userId = $this->getValue();
53
        $htmlUser = '';
54
55 View Code Duplication
        if (!empty($userId) && $userId != 0) {
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...
56
            $rsUser = UserTable::getById($userId);
57
            $user = $rsUser->fetch();
58
59
            $htmlUser = '[<a href="user_edit.php?lang=ru&ID=' . $user['ID'] . '">' . $user['ID'] . '</a>]';
60
61
            if ($user['EMAIL']) {
62
                $htmlUser .= ' (' . $user['EMAIL'] . ')';
63
            }
64
65
            $htmlUser .= ' ' . static::prepareToOutput($user['NAME'])
66
                . '&nbsp;' . static::prepareToOutput($user['LAST_NAME']);
67
        }
68
69
        return $htmlUser;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function generateRow(&$row, $data)
76
    {
77
        $userId = $this->getValue();
78
        $strUser = '';
79
80 View Code Duplication
        if (!empty($userId) && $userId != 0) {
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...
81
            $rsUser = UserTable::getById($userId);
82
            $user = $rsUser->fetch();
83
84
            $strUser = '[<a href="user_edit.php?lang=ru&ID=' . $user['ID'] . '">' . $user['ID'] . '</a>]';
85
86
            if ($user['EMAIL']) {
87
                $strUser .= ' (' . $user['EMAIL'] . ')';
88
            }
89
90
            $strUser .= ' ' . static::prepareToOutput($user['NAME'])
91
                . '&nbsp;' . static::prepareToOutput($user['LAST_NAME']);
92
        }
93
94
        if ($strUser) {
95
            $row->AddViewField($this->getCode(), $strUser);
96
        } else {
97
            $row->AddViewField($this->getCode(), '');
98
        }
99
    }
100
}