TCheckRange::checkValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_rules\Rules;
4
5
6
use kalanis\kw_rules\Exceptions\RuleException;
7
8
9
/**
10
 * trait TCheckRange
11
 * @package kalanis\kw_rules\Rules
12
 * Check original values as range
13
 */
14
trait TCheckRange
15
{
16
    use TRule;
17
18
    /**
19
     * @param mixed|null $againstValue
20
     * @throws RuleException
21
     * @return array<int>
22
     */
23 26
    protected function checkValue($againstValue)
24
    {
25 26
        if (!is_array($againstValue)) {
26 2
            throw new RuleException('No array found. Need set both values to compare!');
27
        }
28 24
        $values = array_map([$this, 'checkRule'], $againstValue);
29 22
        $lower = intval(min($values));
30 22
        $higher = intval(max($values));
31 22
        return [$lower, $higher];
32
    }
33
34
    /**
35
     * @param mixed|null $againstValue
36
     * @throws RuleException
37
     * @return int
38
     */
39 42
    protected function checkRule($againstValue): int
40
    {
41 42
        if (is_array($againstValue)) {
42 1
            throw new RuleException('Sub-array found. Need set only values to compare!');
43
        }
44 42
        if (is_object($againstValue)) {
45 1
            throw new RuleException('Object found. Need set only values to compare!');
46
        }
47 41
        return intval(strval($againstValue));
48
    }
49
}
50