Completed
Push — master ( 46ca35...9cf9e5 )
by Maxim
03:19
created

MinMaxLengthConstraint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
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 69
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A check() 9 9 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's length is in the given range.
10
 */
11 View Code Duplication
class MinMaxLengthConstraint 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
     * MinMaxLengthConstraint constructor.
29
     *
30
     * @param $min
31
     * @param $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_string($value)) {
48
            $length = strlen($value);
49
50
            return $length >= $this->min && $length <= $this->max;
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getMessage() {
60
        if ($this->message !== null) {
61
            return $this->message;
62
        }
63
64
        return s(
65
            'Must be between "%s" and "%s" characters long.',
66
            $this->min, $this->max
67
        );
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getOptions() {
74
        return [
75
            'min' => $this->min,
76
            'max' => $this->max,
77
        ];
78
    }
79
}
80