Completed
Push — master ( a07461...abf834 )
by Rasmus
03:12
created

IntField::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 1
Metric Value
c 1
b 0
f 1
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;
4
5
use InvalidArgumentException;
6
use mindplay\kissform\InputModel;
7
use mindplay\kissform\InputRenderer;
8
use mindplay\kissform\Validators\CheckInt;
9
use mindplay\kissform\Validators\CheckMaxValue;
10
use mindplay\kissform\Validators\CheckMinValue;
11
use mindplay\kissform\Validators\CheckRange;
12
use UnexpectedValueException;
13
14
/**
15
 * This class provides information about an integer field.
16
 */
17
class IntField extends TextField
18
{
19
    /**
20
     * @var int|null minimum value
21
     */
22
    public $min_value;
23
24
    /**
25
     * var int|null maximum value
26
     */
27
    public $max_value;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1 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 1
        $defaults = [];
35
36 1
        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 1
        if ($this->min_value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->min_value 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...
41 1
            $defaults['min'] = $this->min_value;
42
        }
43
44 1
        if ($this->max_value) {
45 1
            $defaults['max'] = $this->max_value;
46
        }
47
48 1
        return $renderer->inputFor($this, 'number', $attr + $defaults);
49
    }
50
    
51
    /**
52
     * @param InputModel $model
53
     *
54
     * @return int|null
55
     *
56
     * @throws UnexpectedValueException if unable to parse the input
57
     */
58 1 View Code Duplication
    public function getValue(InputModel $model)
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...
59
    {
60 1
        $input = $model->getInput($this);
61
62 1
        if ($input === null) {
63 1
            return null; // no input available
64
        }
65
66 1
        if (is_numeric($input)) {
67 1
            return (int) $input;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (int) $input; (integer) is incompatible with the return type of the parent method mindplay\kissform\Field::getValue of type string|array|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
        }
69
70
        throw new UnexpectedValueException("unexpected input: {$input}");
71
    }
72
73
    /**
74
     * @param InputModel $model
75
     * @param int|null   $value
76
     *
77
     * @return void
78
     *
79
     * @throws InvalidArgumentException if the given value is unacceptable.
80
     */
81 1
    public function setValue(InputModel $model, $value)
82
    {
83 1 View Code Duplication
        if (is_int($value)) {
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...
84 1
            $model->setInput($this, (string) $value);
85 1
        } elseif ($value === null) {
86 1
            $model->setInput($this, null);
87
        } else {
88 1
            throw new InvalidArgumentException("unexpected value type: " . gettype($value));
89
        }
90 1
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 2
    public function createValidators()
96
    {
97 2
        $validators = parent::createValidators();
98
99 2
        if ($this->min_value !== null) {
100 1
            if ($this->max_value !== null) {
101 1
                $validators[] = new CheckRange($this->min_value, $this->max_value);
102
            } else {
103 1
                $validators[] = new CheckMinValue($this->min_value);
104
            }
105 2
        } else if ($this->max_value !== null) {
106 1
            $validators[] = new CheckMaxValue($this->max_value);
107
        } else {
108 2
            $validators[] = new CheckInt();
109
        }
110
111 2
        return $validators;
112
    }
113
}
114