Completed
Push — master ( f19b46...664764 )
by Rasmus
03:08
created

IntField   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 28 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 6
dl 21
loc 75
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 14 14 3
A setValue() 7 10 3
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;
4
5
use InvalidArgumentException;
6
use mindplay\kissform\InputModel;
7
use mindplay\kissform\Validators\CheckInt;
8
use mindplay\kissform\Validators\CheckMaxValue;
9
use mindplay\kissform\Validators\CheckMinValue;
10
use mindplay\kissform\Validators\CheckRange;
11
use UnexpectedValueException;
12
13
/**
14
 * This class provides information about an integer field.
15
 */
16
class IntField extends TextField
17
{
18
    /**
19
     * @var int|null minimum value
20
     */
21
    public $min_value;
22
23
    /**
24
     * var int|null maximum value
25
     */
26
    public $max_value;
27
28
    /**
29
     * @param InputModel $model
30
     *
31
     * @return int|null
32
     *
33
     * @throws UnexpectedValueException if unable to parse the input
34
     */
35 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...
36
    {
37 1
        $input = $model->getInput($this);
38
39 1
        if ($input === null) {
40 1
            return null; // no input available
41
        }
42
43 1
        if (is_numeric($input)) {
44 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...
45
        }
46
47
        throw new UnexpectedValueException("unexpected input: {$input}");
48
    }
49
50
    /**
51
     * @param InputModel $model
52
     * @param int|null   $value
53
     *
54
     * @return void
55
     *
56
     * @throws InvalidArgumentException if the given value is unacceptable.
57
     */
58 1
    public function setValue(InputModel $model, $value)
59
    {
60 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...
61 1
            $model->setInput($this, (string) $value);
62 1
        } elseif ($value === null) {
63 1
            $model->setInput($this, null);
64
        } else {
65 1
            throw new InvalidArgumentException("unexpected value type: " . gettype($value));
66
        }
67 1
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 2
    public function createValidators()
73
    {
74 2
        $validators = parent::createValidators();
75
        
76 2
        $validators[] = new CheckInt();
77
        
78 2
        if ($this->min_value !== null) {
79 1
            if ($this->max_value !== null) {
80 1
                $validators[] = new CheckRange($this->min_value, $this->max_value);
81
            } else {
82 1
                $validators[] = new CheckMinValue($this->min_value);
83
            }
84 2
        } else if ($this->max_value !== null) {
85 1
            $validators[] = new CheckMaxValue($this->max_value);
86
        }
87
88 2
        return $validators;
89
    }
90
}
91