TCheckRange   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkValue() 0 9 2
A checkRule() 0 9 3
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