UrlWidget::getValue()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 10
nop 0
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 7.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace DigitalWand\AdminHelper\Widget;
4
5
use Bitrix\Main\Localization\Loc;
6
7
Loc::loadMessages(__FILE__);
8
9
/**
10
 * Виджет текстового поля для ввода гиперссылки.
11
 *
12
 * Доступные опции:
13
 * <ul>
14
 * <li> PROTOCOL_REQUIRED - ссылка должна иметь протокол</li>
15
 * <li> STYLE - inline-стили </li>
16
 * <li> SIZE - значение атрибута size для input </li>
17
 * <li> MAX_URL_LEN - длина отображаемого URL</li>
18
 * </ul>
19
 *
20
 * @author Nik Samokhvalov <[email protected]>
21
 */
22
class UrlWidget extends StringWidget
23
{
24
    static protected $defaults = array(
25
        'MAX_URL_LEN' => 256,
26
        'PROTOCOL_REQUIRED' => false,
27
    );
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function generateRow(&$row, $data)
33
    {
34
        $value = $this->getValue();
35
36 View Code Duplication
        if ($this->getSettings('EDIT_IN_LIST') AND !$this->getSettings('READONLY')) {
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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
37
            $row->AddInputField($this->getCode(), array('style' => 'width:90%'));
38
        }
39
40
        $row->AddViewField($this->getCode(), $value);
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getValue()
47
    {
48
        $code = $this->getCode();
49
        $value = isset($this->data[$code]) ? $this->data[$code] : null;
50
51
        if ($value !== null) {
52
            $urlText = static::prepareToOutput($value);
53
            $urlText = preg_replace('/^javascript:/i', '', $urlText);
54
55
            if (strlen($urlText) > $this->getSettings('MAX_URL_LEN')) {
56
                $urlText = substr($urlText, 0, $this->getSettings('MAX_URL_LEN'));
57
            }
58
59
            if (($this->getSettings('READONLY') && $this->getCurrentViewType() == static::EDIT_HELPER) || $this->getCurrentViewType() == static::LIST_HELPER) {
60
                $value = '<a href="' . $value . '" target="_blank">' . $urlText . '</a>';
61
            } else {
62
                $value = $urlText;
63
            }
64
        }
65
66
        return $value;
67
    }
68
    
69
    /**
70
     * @inheritdoc
71
     */
72
    protected function getValueReadonly()
73
    {
74
        return $this->getValue();
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function processEditAction()
81
    {
82
        $value = $this->getValue();
83
84
        if (
85
            $this->getSettings('PROTOCOL_REQUIRED')
86
            && !empty($value)
87
            && preg_match('/^https?:\/\//', $value) == 0
88
        ) {
89
90
            $this->addError('PROTOCOL_REQUIRED');
91
        }
92
    }
93
}
94