ValidatorTrait::validateInt()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 5
rs 9.6111
1
<?php
2
3
/**
4
 * Copyright (c) 2020 UMI
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
declare(strict_types=1);
26
27
namespace UmiTop\UmiCore\Util;
28
29
use Exception;
30
31
/**
32
 * Trait ValidatorTrait
33
 * @package UmiTop\UmiCore\Util
34
 */
35
trait ValidatorTrait
36
{
37
    /**
38
     * @param int $val
39
     * @param int|null $min
40
     * @param int|null $max
41
     * @return void
42
     * @throws Exception
43
     */
44 19
    private function validateInt(int $val, int $min = null, int $max = null): void
45
    {
46 19
        if ($min !== null && $val < $min) {
47 1
            throw new Exception('invalid value');
48
        }
49
50 19
        if ($max !== null && $val > $max) {
51 3
            throw new Exception('invalid value');
52
        }
53 19
    }
54
55
    /**
56
     * @param string $val
57
     * @param int $length
58
     * @return void
59
     * @throws Exception
60
     */
61 12
    private function validateStr(string $val, int $length): void
62
    {
63 12
        if (strlen($val) !== $length) {
64 1
            throw new Exception('invalid length');
65
        }
66 11
    }
67
}
68