GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d3cdc5...5d7d21 )
by Ransford
01:39
created

RangeHelper::validate()   C

Complexity

Conditions 12
Paths 23

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 24
nc 23
nop 1
dl 0
loc 40
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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