TextAreaWidget   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 8
loc 50
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditHtml() 0 8 1
B generateRow() 8 21 9

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
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 19 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
 * Выводит textarea для редактирования длинных строк.
11
 * Урезает длинные строки при отображении в списке
12
 *
13
 * Доступные опции:
14
 * <ul>
15
 * <li><b>COLS</b> - ширина</li>
16
 * <li><b>ROWS</b> - высота</li>
17
 * </ul>
18
 */
19
class TextAreaWidget extends StringWidget
20
{
21
    /**
22
     * количество отображаемых символов в режиме списка.
23
     */
24
    const LIST_TEXT_SIZE = 150;
25
26
    static protected $defaults = array(
27
        'COLS' => 65,
28
        'ROWS' => 5,
29
        'EDIT_IN_LIST' => false
30
    );
31
32
    /**
33
     * @inheritdoc
34
     */
35
    protected function getEditHtml()
36
    {
37
        $cols = $this->getSettings('COLS');
38
        $rows = $this->getSettings('ROWS');
39
40
        return '<textarea cols="' . $cols . '" rows="' . $rows . '" name="' . $this->getEditInputName() . '">'
41
        . static::prepareToOutput($this->getValue(), false) . '</textarea>';
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function generateRow(&$row, $data)
48
    {
49
        $text = $this->getValue();
50
51
        if ($this->getSettings('EDIT_IN_LIST') AND !$this->getSettings('READONLY')) {
0 ignored issues
show
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...
52
            $row->AddInputField($this->getCode(), array('style' => 'width:90%'));
53
        } else {
54 View Code Duplication
            if (strlen($text) > self::LIST_TEXT_SIZE && !$this->isExcelView()) {
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...
55
                $pos = false;
56
                $pos = $pos === false ? stripos($text, " ", self::LIST_TEXT_SIZE) : $pos;
57
                $pos = $pos === false ? stripos($text, "\n", self::LIST_TEXT_SIZE) : $pos;
58
                $pos = $pos === false ? stripos($text, "</", self::LIST_TEXT_SIZE) : $pos;
59
                $pos = $pos === false ? 300 : $pos;
60
                $text = substr($text, 0, $pos) . " ...";
61
            }
62
63
            $text = static::prepareToOutput($text);
64
65
            $row->AddViewField($this->code, $text);
66
        }
67
    }
68
}