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

FloatField   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 3
dl 63
loc 63
ccs 7
cts 7
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderInput() 8 8 3
A getValue() 14 14 3
A setValue() 10 10 3
A createTypeValidator() 4 4 1

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;
4
5
use InvalidArgumentException;
6
use mindplay\kissform\Fields\Base\NumericField;
7
use mindplay\kissform\InputModel;
8
use mindplay\kissform\InputRenderer;
9
use mindplay\kissform\Validators\CheckFloat;
10
use mindplay\kissform\Validators\CheckInt;
11
use UnexpectedValueException;
12
13
/**
14
 * This class provides information about a floating point field.
15
 */
16 View Code Duplication
class FloatField extends NumericField
0 ignored issues
show
Duplication introduced by
This class 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...
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 1
    public function renderInput(InputRenderer $renderer, InputModel $model, array $attr)
22
    {
23 1
        $pattern = $this->min_value === null || $this->min_value < 0
24 1
            ? '-?\d*(\.(?=\d))?\d*' // accept negative
25 1
            : '\d*(\.(?=\d))?\d*';
26
        
27 1
        return parent::renderInput($renderer, $model, $attr + ['pattern' => $pattern]);
28
    }
29
30
    /**
31
     * @param InputModel $model
32
     *
33
     * @return float|null
34
     *
35
     * @throws UnexpectedValueException if unable to parse the input
36
     */
37
    public function getValue(InputModel $model)
38
    {
39
        $input = $model->getInput($this);
40
41
        if ($input === null) {
42
            return null; // no input available
43
        }
44
45
        if (is_numeric($input)) {
46
            return (float) $input;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (double) $input; (double) 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...
47
        }
48
49
        throw new UnexpectedValueException("unexpected input: {$input}");
50
    }
51
52
    /**
53
     * @param InputModel $model
54
     * @param float|null $value
55
     *
56
     * @return void
57
     *
58
     * @throws InvalidArgumentException if the given value is unacceptable.
59
     */
60
    public function setValue(InputModel $model, $value)
61
    {
62
        if (is_numeric($value)) {
63
            $model->setInput($this, (string) $value);
64
        } elseif ($value === null) {
65
            $model->setInput($this, null);
66
        } else {
67
            throw new InvalidArgumentException("unexpected value type: " . gettype($value));
68
        }
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 1
    protected function createTypeValidator()
75
    {
76 1
        return new CheckFloat();
77
    }
78
}
79