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 (#1094)
by Henrique
04:43
created

Length::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 3
crap 4.3731
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 Countable;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Respect\Validation\Rules\Countable. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Respect\Validation\Exceptions\ComponentException;
18
use function count;
19
use function is_array;
20
use function is_int;
21
use function is_object;
22
use function is_string;
23
use function mb_detect_encoding;
24
use function mb_strlen;
25
26
/**
27
 * Validates the length of the given input.
28
 *
29
 * @author Alexandre Gomes Gaigalas <[email protected]>
30
 * @author Blake Hair <[email protected]>
31
 * @author Danilo Correa <[email protected]>
32
 * @author Henrique Moody <[email protected]>
33
 * @author Hugo Hamon <[email protected]>
34
 * @author João Torquato <[email protected]>
35
 * @author Marcelo Araujo <[email protected]>
36
 */
37
final class Length extends AbstractRule
38
{
39
    /**
40
     * @var int
41
     */
42
    private $minValue;
43
44
    /**
45
     * @var int
46
     */
47
    private $maxValue;
48
49
    /**
50
     * @var bool
51
     */
52
    private $inclusive;
53
54
    /**
55
     * Creates the rule with a minimum and maximum value.
56
     *
57
     * @param int|null $min
58
     * @param int|null $max
59
     * @param bool $inclusive TRUE by default
60
     *
61
     * @throws ComponentException
62
     */
63 15
    public function __construct(int $min = null, int $max = null, $inclusive = true)
64
    {
65 15
        $this->minValue = $min;
66 15
        $this->maxValue = $max;
67 15
        $this->inclusive = $inclusive;
68
69 15
        if (null !== $min && null !== $max && $min > $max) {
70
            throw new ComponentException(
71
                sprintf('%s cannot be less than %s for validation', $min, $max)
72
            );
73
        }
74 15
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 34
    public function validate($input): bool
80
    {
81 34
        $length = $this->extractLength($input);
82 34
        if (null === $length) {
83 1
            return false;
84
        }
85
86 33
        return $this->validateMin($length) && $this->validateMax($length);
87
    }
88
89 34
    private function extractLength($input): ?int
90
    {
91 34
        if (is_string($input)) {
92 26
            return mb_strlen($input, mb_detect_encoding($input));
93
        }
94
95 10
        if (is_array($input) || $input instanceof Countable) {
96 7
            return count($input);
97
        }
98
99 6
        if (is_object($input)) {
100 3
            return $this->extractLength(get_object_vars($input));
101
        }
102
103 3
        if (is_int($input)) {
104 2
            return $this->extractLength((string) $input);
105
        }
106
107 1
        return null;
108
    }
109
110 33
    private function validateMin(int $length): bool
111
    {
112 33
        if (null === $this->minValue) {
113 3
            return true;
114
        }
115
116 30
        if ($this->inclusive) {
117 22
            return $length >= $this->minValue;
118
        }
119
120 8
        return $length > $this->minValue;
121
    }
122
123 25
    private function validateMax(int $length): bool
124
    {
125 25
        if (null === $this->maxValue) {
126 4
            return true;
127
        }
128
129 22
        if ($this->inclusive) {
130 15
            return $length <= $this->maxValue;
131
        }
132
133 7
        return $length < $this->maxValue;
134
    }
135
}
136