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
Pull Request — master (#1034)
by Henrique
02:29
created

Length::extractLength()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.0111
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6.0359
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Respect\Validation\Exceptions\ComponentException;
17
18
class Length extends AbstractRule
19
{
20
    public $minValue;
21
    public $maxValue;
22
    public $inclusive;
23
24 41
    public function __construct($min = null, $max = null, $inclusive = true)
25
    {
26 41
        $this->minValue = $min;
27 41
        $this->maxValue = $max;
28 41
        $this->inclusive = $inclusive;
29 41
        $paramValidator = new AnyOf(new NumericVal(), new NullType());
30 41
        if (!$paramValidator->validate($min)) {
31 1
            throw new ComponentException(
32 1
                sprintf('%s is not a valid numeric length', $min)
33
            );
34
        }
35
36 40
        if (!$paramValidator->validate($max)) {
37 1
            throw new ComponentException(
38 1
                sprintf('%s is not a valid numeric length', $max)
39
            );
40
        }
41
42 39
        if (!is_null($min) && !is_null($max) && $min > $max) {
43 1
            throw new ComponentException(
44 1
                sprintf('%s cannot be less than %s for validation', $min, $max)
45
            );
46
        }
47 38
    }
48
49 36
    public function validate($input): bool
50
    {
51 36
        $length = $this->extractLength($input);
52
53 36
        return $this->validateMin($length) && $this->validateMax($length);
54
    }
55
56 36
    protected function extractLength($input)
57
    {
58 36
        if (is_string($input)) {
59 27
            return mb_strlen($input, mb_detect_encoding($input));
60
        }
61
62 9
        if (is_array($input) || $input instanceof \Countable) {
63 4
            return count($input);
64
        }
65
66 5
        if (is_object($input)) {
67 3
            return count(get_object_vars($input));
68
        }
69
70 2
        if (is_int($input)) {
71 2
            return mb_strlen((string) $input);
72
        }
73
74
        return false;
75
    }
76
77 36
    protected function validateMin($length)
78
    {
79 36
        if (is_null($this->minValue)) {
80 3
            return true;
81
        }
82
83 33
        if ($this->inclusive) {
84 25
            return $length >= $this->minValue;
85
        }
86
87 8
        return $length > $this->minValue;
88
    }
89
90 28
    protected function validateMax($length)
91
    {
92 28
        if (is_null($this->maxValue)) {
93 3
            return true;
94
        }
95
96 25
        if ($this->inclusive) {
97 18
            return $length <= $this->maxValue;
98
        }
99
100 7
        return $length < $this->maxValue;
101
    }
102
}
103