Completed
Push — master ( 0df277...6ea006 )
by Maxim
03:10
created

ValueRangeConstraint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 67
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A check() 7 7 3
A getMessage() 10 10 2
A getOptions() 6 6 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 Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8
/**
9
 * Check if the value is inside the given range.
10
 */
11 View Code Duplication
class ValueRangeConstraint implements IConstraint {
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...
12
    /**
13
     * @var int
14
     */
15
    protected $min;
16
17
    /**
18
     * @var int
19
     */
20
    protected $max;
21
22
    /**
23
     * @var string
24
     */
25
    protected $message;
26
27
    /**
28
     * ValueRangeConstraint constructor.
29
     *
30
     * @param int $min
31
     * @param int $max
32
     * @param string $message
33
     */
34
    public function __construct($min, $max, $message = null) {
35
        $this->min = $min;
36
        $this->max = $max;
37
        $this->message = $message;
38
    }
39
40
    /**
41
     * @param $value
42
     * @param IValidationData $data
43
     *
44
     * @return bool
45
     */
46
    public function check($value, IValidationData $data = null) {
47
        if (is_numeric($value)) {
48
            return $value >= $this->min && $value <= $this->max;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getMessage() {
58
        if ($this->message !== null) {
59
            return $this->message;
60
        }
61
62
        return s(
63
            'Must have a value between "%s" and "%s".',
64
            $this->min, $this->max
65
        );
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getOptions() {
72
        return [
73
            'min' => $this->min,
74
            'max' => $this->max,
75
        ];
76
    }
77
}
78