Completed
Push — master ( d3cdc5...5d7d21 )
by Ransford
01:39
created

RangeHelper::isInRange()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Billing Boss
4
 *
5
 * @link      https://github.com/ranskills/billing-boss-php
6
 * @copyright Copyright (c) 2018 Ransford Ako Okpoti
7
 * @license   Refer to the LICENSE distributed with this library
8
 * @since     1.0
9
 */
10
namespace BillingBoss;
11
12
final class RangeHelper
13
{
14
    const VALIDATION_NO_RANGE_FOUND = 'NO RANGE FOUND';
15
    const VALIDATION_OK = 'OK';
16
    //todo change to overlapping range
17
    const VALIDATION_OVERLAPPING_VALUES = 'OVERLAPPING VALUES';
18
    const VALIDATION_CONFLICT = 'CONFLICT';
19
20
    public static function validate($str)
21
    {
22
        $ranges = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ranges is dead and can be removed.
Loading history...
23
        $matches = [];
24
25
        $numMatches = preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches);
26
        if ($numMatches === 0) {
27
            return [self::VALIDATION_NO_RANGE_FOUND, []];
28
        }
29
30
        $numAstericks = substr_count($str, '*');
31
        if ($numAstericks > 1) {
32
            return [self::VALIDATION_CONFLICT, []];
33
        }
34
35
        $lowerLimits = $matches[1];
36
        $upperLimits = $matches[2];
37
        $ranges = [];
38
        for ($i = 0; $i < count($lowerLimits); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
39
            if (is_numeric($upperLimits[$i]) && floatval($lowerLimits[$i]) > floatval($upperLimits[$i])) {
40
                return [self::VALIDATION_CONFLICT, []];
41
            }
42
            $ranges[] = [$lowerLimits[$i], $upperLimits[$i]];
43
        }
44
45
        for ($i = 0; $i < count($ranges); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
46
            for ($j = $i + 1; $j < count($ranges); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
47
                $inRange = self::isInRange($ranges[$i], $ranges[$j][0]) ||
48
                self::isInRange($ranges[$i], $ranges[$j][1]) ||
49
50
                self::isInRange($ranges[$j], $ranges[$i][0]) ||
51
                self::isInRange($ranges[$j], $ranges[$i][1]);
52
53
                if ($inRange) {
54
                    return [self::VALIDATION_OVERLAPPING_VALUES, []];
55
                }
56
            }
57
        }
58
59
        return [self::VALIDATION_OK, $ranges];
60
    }
61
62
    public static function getRangeLimits($str)
63
    {
64
        $ranges = [];
65
66
        $numMatches = preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches);
67
        if ($numMatches === 0) {
68
            return $ranges;
69
        }
70
71
        $lowerLimits = $matches[1];
72
        $upperLimits = $matches[2];
73
        for ($i = 0; $i < count($lowerLimits); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
74
            if (is_numeric($upperLimits[$i]) && floatval($lowerLimits[$i]) > floatval($upperLimits[$i])) {
75
                return self::VALIDATION_CONFLICT;
76
            }
77
            $ranges[] = [$lowerLimits[$i], $upperLimits[$i]];
78
        }
79
80
        return $ranges;
81
    }
82
83
    public static function isInRange(array $range, $value)
84
    {
85
        if ($value === '*') {
86
            return false;
87
        }
88
 
89
        if (is_numeric($range[1])) {
90
            return $value >= $range[0] && $value <= $range[1];
91
        }
92
93
        return $value >= $range[0];
94
    }
95
}
96