Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — 2.0 ( e6a123 )
by Henrique
05:00
created

Length::validateMax()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
use Respect\Validation\Exceptions\ComponentException;
15
16
class Length extends AbstractRule
17
{
18
    public $minValue;
19
    public $maxValue;
20
    public $inclusive;
21
22
    public function __construct($min = null, $max = null, $inclusive = true)
23
    {
24
        $this->minValue = $min;
25
        $this->maxValue = $max;
26
        $this->inclusive = $inclusive;
27
        $paramValidator = new OneOf(new NumericVal(), new NullType());
28
        if (!$paramValidator->validate($min)) {
29
            throw new ComponentException(
30
                sprintf('%s is not a valid numeric length', $min)
31
            );
32
        }
33
34
        if (!$paramValidator->validate($max)) {
35
            throw new ComponentException(
36
                sprintf('%s is not a valid numeric length', $max)
37
            );
38
        }
39
40
        if (!is_null($min) && !is_null($max) && $min > $max) {
41
            throw new ComponentException(
42
                sprintf('%s cannot be less than %s for validation', $min, $max)
43
            );
44
        }
45
    }
46
47
    public function validate($input)
48
    {
49
        $length = $this->extractLength($input);
50
51
        return $this->validateMin($length) && $this->validateMax($length);
52
    }
53
54
    protected function extractLength($input)
55
    {
56
        if (is_string($input)) {
57
            return mb_strlen($input, mb_detect_encoding($input));
58
        }
59
60
        if (is_array($input) || $input instanceof \Countable) {
61
            return count($input);
62
        }
63
64
        if (is_object($input)) {
65
            return count(get_object_vars($input));
66
        }
67
68
        if (is_int($input)) {
69
            return mb_strlen((string) $input);
70
        }
71
72
        return false;
73
    }
74
75
    protected function validateMin($length)
76
    {
77
        if (is_null($this->minValue)) {
78
            return true;
79
        }
80
81
        if ($this->inclusive) {
82
            return $length >= $this->minValue;
83
        }
84
85
        return $length > $this->minValue;
86
    }
87
88
    protected function validateMax($length)
89
    {
90
        if (is_null($this->maxValue)) {
91
            return true;
92
        }
93
94
        if ($this->inclusive) {
95
            return $length <= $this->maxValue;
96
        }
97
98
        return $length < $this->maxValue;
99
    }
100
}
101