Completed
Push — master ( f7f501...458ded )
by Rasmus
05:42
created

NumericField::renderInput()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 9
nc 8
nop 3
crap 4
1
<?php
2
3
namespace mindplay\kissform\Fields\Base;
4
5
use mindplay\kissform\Facets\ValidatorInterface;
6
use mindplay\kissform\Fields\TextField;
7
use mindplay\kissform\InputModel;
8
use mindplay\kissform\InputRenderer;
9
use mindplay\kissform\Validators\CheckInt;
10
use mindplay\kissform\Validators\CheckMaxValue;
11
use mindplay\kissform\Validators\CheckMinValue;
12
use mindplay\kissform\Validators\CheckRange;
13
14
/**
15
 * Abstract base class for integer, floating-point and fixed-precision numeric Field types.
16
 */
17
abstract class NumericField extends TextField
18
{
19
    /**
20
     * @var int|float|null minimum value
21
     */
22
    public $min_value;
23
24
    /**
25
     * var int|float|null maximum value
26
     */
27
    public $max_value;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2 View Code Duplication
    public function renderInput(InputRenderer $renderer, InputModel $model, array $attr)
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...
33
    {
34 2
        $defaults = [];
35
36 2
        if ($this->max_length) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->max_length of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
37
            $defaults['maxlength'] = $this->max_length;
38
        }
39
40 2
        if ($this->min_value) {
41 2
            $defaults['min'] = $this->min_value;
42
        }
43
44 2
        if ($this->max_value) {
45 2
            $defaults['max'] = $this->max_value;
46
        }
47
48 2
        return $renderer->inputFor($this, 'number', $attr + $defaults);
49
    }
50
51
    /**
52
     * @return ValidatorInterface
53
     */
54
    abstract protected function createTypeValidator();
55
    
56
    /**
57
     * @inheritdoc
58
     */
59 3
    public function createValidators()
60
    {
61 3
        $validators = parent::createValidators();
62
        
63 3
        $validators[] = $this->createTypeValidator();
64
65 3
        if ($this->min_value !== null) {
66 2
            if ($this->max_value !== null) {
67 2
                $validators[] = new CheckRange($this->min_value, $this->max_value);
68
            } else {
69 2
                $validators[] = new CheckMinValue($this->min_value);
70
            }
71 3
        } else if ($this->max_value !== null) {
72 2
            $validators[] = new CheckMaxValue($this->max_value);
73
        }
74
75 3
        return $validators;
76
    }
77
}
78