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 ( b6a58e...3a3beb )
by Ransford
01:47
created

RangeHelper::findOverlappingRanges()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 10
nop 1
dl 0
loc 21
rs 8.8333
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
11
namespace BillingBoss;
12
13
use BillingBoss\Exception\RangeException;
14
use BillingBoss\Exception\RangeOverlapException;
15
use BillingBoss\Exception\RangeConflictException;
16
17
final class RangeHelper
18
{
19
20
    /**
21
     * @param $str
22
     * @return array
23
     * @throws RangeException
24
     */
25
    public static function validate($str)
26
    {
27
        $matches = [];
28
        $numMatches = preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches);
29
        if ($numMatches === 0) {
30
            throw new RangeConflictException('No range values provided', RangeException::NO_RANGE_FOUND);
31
        }
32
33
        $numAstericks = substr_count($str, '*');
34
        if ($numAstericks > 1) {
35
            throw new RangeConflictException(
36
                'More than one * provided in the ranges. There can be only one within a set of ranges to be examined.',
37
                RangeException::MULTIPLE_OPEN_ENDED_UPPER_LIMIT
38
            );
39
        }
40
41
        $ranges = self::getRangeLimits($str);
42
        $overlappingRanges = self::findOverlappingRanges($ranges);
43
44
        if (count($overlappingRanges) !== 0) {
45
            $message = sprintf(
46
                'The ranges %s - %s and %s - %s are overlapping and will lead to unexpected results.',
47
                $overlappingRanges[0][0],
48
                $overlappingRanges[0][1],
49
                $overlappingRanges[1][0],
50
                $overlappingRanges[1][1]
51
            );
52
53
            throw new RangeOverlapException(
54
                $message,
55
                RangeException::NO_RANGE_FOUND,
56
                $overlappingRanges
57
            );
58
        }
59
60
        return $ranges;
61
    }
62
63
    private static function findOverlappingRanges(array $ranges): array
64
    {
65
        $numRanges = count($ranges);
66
67
        for ($i = 0; $i < $numRanges; $i++) {
68
            for ($j = $i + 1; $j < $numRanges; $j++) {
69
                $inRange = self::isInRange($ranges[$i], $ranges[$j][0]) ||
70
                           self::isInRange($ranges[$i], $ranges[$j][1]) ||
71
                           self::isInRange($ranges[$j], $ranges[$i][0]) ||
72
                           self::isInRange($ranges[$j], $ranges[$i][1]);
73
74
                if ($inRange) {
75
                    return [
76
                        $ranges[$i],
77
                        $ranges[$j]
78
                    ];
79
                }
80
            }
81
        }
82
83
        return [];
84
    }
85
86
    /**
87
     * @param $str
88
     * @return array
89
     * @throws RangeConflictException
90
     */
91
    private static function getRangeLimits($str)
92
    {
93
        $ranges = [];
94
95
        preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches);
96
97
        $lowerLimits = $matches[1];
98
        $upperLimits = $matches[2];
99
        $len = count($lowerLimits);
100
101
        for ($i = 0; $i < $len; $i++) {
102
            if (is_numeric($upperLimits[$i]) && floatval($lowerLimits[$i]) > floatval($upperLimits[$i])) {
103
                $message = sprintf(
104
                    'Invalid limits provided. The lower limit (%s) is greater than the upper limit (%s)',
105
                    $lowerLimits[$i],
106
                    $upperLimits[$i]
107
                );
108
                throw new RangeConflictException(
109
                    $message,
110
                    RangeException::LOWER_LIMIT_GREATER_THAN_UPPER_LIMIT
111
                );
112
            }
113
114
            $ranges[] = [$lowerLimits[$i], $upperLimits[$i]];
115
        }
116
117
        return $ranges;
118
    }
119
120
    public static function isInRange(array $range, $value)
121
    {
122
        if ($value === '*') {
123
            return false;
124
        }
125
126
        if (is_numeric($range[1])) {
127
            return $value >= $range[0] && $value <= $range[1];
128
        }
129
130
        return $value >= $range[0];
131
    }
132
}
133