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

NumericField   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 29.51 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 5
dl 18
loc 61
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renderInput() 18 18 4
createTypeValidator() 0 1 ?
A createValidators() 0 18 4

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 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