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 — master ( d1c3b2...1bd811 )
by Henrique
06:49 queued 02:12
created

Length::extractLength()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

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