NumberWidget::isNumber()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
nc 3
nop 1
dl 0
loc 4
rs 10
c 1
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 12 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
 * Виджет с числовыми значениями. Точная копия StringWidget, только работает с числами и не ищет по подстроке.
11
 */
12
class NumberWidget extends StringWidget
13
{
14
    static protected $defaults = array(
15
        'FILTER' => '=',
16
        'EDIT_IN_LIST' => true
17
    );
18
19
    public function checkFilter($operationType, $value)
20
    {
21
        return $this->isNumber($value);
22
    }
23
24 View Code Duplication
    public function checkRequired()
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...
25
    {
26
        if ($this->getSettings('REQUIRED') == true) {
27
            $value = $this->getValue();
28
            return !is_null($value) && $value !== '';
29
        } else {
30
            return true;
31
        }
32
    }
33
34
    public function processEditAction()
35
    {
36
        if (!$this->checkRequired()) {
37
            $this->addError('DIGITALWAND_AH_REQUIRED_FIELD_ERROR');
38
39
        } else if (!$this->isNumber($this->getValue())) {
40
            $this->addError('VALUE_IS_NOT_NUMERIC');
41
        }
42
    }
43
44
    protected function isNumber($value)
45
    {
46
        return is_numeric($value) OR is_null($value) OR empty($value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or 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...
47
    }
48
}
49