GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#161)
by
unknown
03:30
created

LengthBetween::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 5
Ratio 50 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 5
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4.074
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
13
/**
14
 * This rule is for validating that the length of the value is within predefined boundaries.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class LengthBetween extends Between
19
{
20
    /**
21
     * A constant that is used when the value is too long.
22
     */
23
    const TOO_LONG = 'LengthBetween::TOO_LONG';
24
25
    /**
26
     * A constant that is used when the value is too short.
27
     */
28
    const TOO_SHORT = 'LengthBetween::TOO_SHORT';
29
30
    /**
31
     * The message templates which can be returned by this validator.
32
     *
33
     * @var array
34
     */
35
    protected $messageTemplates = [
36
        self::TOO_LONG => '{{ name }} must be {{ max }} characters or shorter',
37
        self::TOO_SHORT => '{{ name }} must be {{ min }} characters or longer'
38
    ];
39
40
    /**
41
     * The upper boundary for the length of the value.
42
     *
43
     * @var int
44
     */
45
    protected $max;
46
47
    /**
48
     * The lower boundary for the length of the value.
49
     *
50
     * @var int
51
     */
52
    protected $min;
53
54
    /**
55
     * The encoding to be used for multibyte string functions.
56
     *
57
     * @var string|null
58
     */
59
    protected $encoding;
60
61
    /**
62
     * @param int $min
63
     * @param int|null $max
64
     * @param string|null $encoding
65
     */
66 7
    public function __construct($min, $max, $encoding = null)
67
    {
68 7
        $this->min = $min;
69 7
        $this->max = $max;
70 7
        $this->encoding = $encoding;
71 7
    }
72
73
    /**
74
     * Validates that the length of the value is between min and max.
75
     *
76
     * @param mixed $value
77
     * @return bool
78
     */
79 6
    public function validate($value)
80
    {
81 6 View Code Duplication
        if (is_null($this->encoding) || !function_exists('mb_strlen')) {
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...
82 6
            $length = strlen($value);
83 6
        } else {
84
            $length = mb_strlen($value, $this->encoding);
85
        }
86
87 6
        return !$this->tooSmall($length, self::TOO_SHORT) && !$this->tooLarge($length, self::TOO_LONG);
88
    }
89
90
    /**
91
     * Returns whether or not the value is too long, and logs an error if it is.
92
     *
93
     * @param mixed $value
94
     * @param string $error
95
     * @return bool
96
     */
97 6
    protected function tooLarge($value, $error)
98
    {
99 6
        if ($this->max !== null) {
100 5
            return parent::tooLarge($value, $error);
101
        }
102 1
        return false;
103
    }
104
105
    /**
106
     * Returns the parameters that may be used in a validation message.
107
     *
108
     * @return array
109
     */
110 2
    protected function getMessageParameters()
111
    {
112 2
        return array_merge(parent::getMessageParameters(), [
113 2
            'min' => $this->min,
114 2
            'max' => $this->max
115 2
        ]);
116
    }
117
}
118