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.
Passed
Push — develop ( 23e2c5...5223b2 )
by Sébastien
50s
created

Range::getRanges()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 1
nop 1
dl 0
loc 26
ccs 15
cts 15
cp 1
crap 5
rs 8.439
c 1
b 0
f 0
1
<?php
2
/**
3
 * Novactive Collection.
4
 *
5
 * @author    Luke Visinoni <[email protected], [email protected]>
6
 * @author    Sébastien Morel <[email protected], [email protected]>
7
 * @copyright 2017 Novactive
8
 * @license   MIT
9
 */
10
11
namespace Novactive\Collection\Selector;
12
13
use Novactive\Collection\Collection;
14
use Novactive\Collection\Factory;
15
16
/**
17
 * Class Range.
18
 */
19
class Range
20
{
21
    /**
22
     * Explode the selector to get ranges.
23
     *
24
     * @param $selector
25
     *
26
     * @return Collection
27
     */
28 23
    protected function getRanges($selector)
29
    {
30 23
        return Factory::create(explode(';', $selector))->map(
31
            function ($range) {
32 23
                if ($separator = $this->dotDotSeparator($range) != false) {
33 5
                    return Factory::create([substr($range, 0, $separator), substr($range, $separator + 2)]);
34
                }
35 20
                if (($separator = $this->columnSeparator($range) != false) ||
36 14
                    ($separator = $this->dashSeparator($range) != false)
37 20
                ) {
38 13
                    return Factory::create([substr($range, 0, $separator), substr($range, $separator + 1)]);
39
                }
40 11
                if ($separator = $this->commaSeparator($range) != false) {
41 3
                    return Factory::create(
42
                        [
43 3
                            substr($range, 0, $separator),
44 3
                            intval(substr($range, 0, $separator)) + intval(substr($range, $separator + 1) - 1),
45
                        ]
46 3
                    );
47
                }
48
49
                // just a number here
50 10
                return Factory::create([$range, $range]);
51
            }
52 23
        );
53
    }
54
55
    /**
56
     * @param Collection $parameters
57
     *
58
     * @return bool
59
     */
60 25
    public function supports(Collection $parameters)
61
    {
62 25
        return $parameters->assert(
63
            function ($param) {
64 25
                if (is_array($param) && count($param) == 2) {
65 5
                    return true;
66
                }
67
68 22
                return preg_match('/^([0-9])*([,-:;\\.]*)([0-9])*$/uis', $param) == 1;
69 25
            },
70
            true
71 25
        );
72
    }
73
74
    /**
75
     * @param Collection $parameters
76
     * @param Collection $collection
77
     */
78 23
    public function convert(Collection $parameters, Collection $collection)
79
    {
80 23
        $newCollection = Factory::create();
81 23
        $selector      = $parameters->map(
82
            function ($param) {
83 23
                if ((is_array($param) && count($param) == 2)) {
84 5
                    return implode(':', $param);
85
                }
86
87 20
                return $param;
88
            }
89 23
        )->implode(';');
90
91 23
        return $this->getRanges($selector)->reduce(
92 23
            function (Collection $accumulator, Collection $range) use ($collection) {
93 23
                $from = $range->first();
94 23
                $to   = $range->last();
95 23
                if ($to >= $from) {
96 19
                    return $accumulator->append($collection->slice($from, ($to - $from) + 1));
97
                }
98
99 7
                return $accumulator->append($collection->slice($to, ($from - $to) + 1)->inverse());
100 23
            },
101
            $newCollection
102 23
        );
103
    }
104
105
    /**
106
     * @param $string
107
     *
108
     * @return bool|int
109
     */
110 23
    protected function dotDotSeparator($string)
111
    {
112 23
        return strpos($string, '..');
113
    }
114
115
    /**
116
     * @param $string
117
     *
118
     * @return bool|int
119
     */
120 20
    protected function columnSeparator($string)
121
    {
122 20
        return strpos($string, ':');
123
    }
124
125
    /**
126
     * @param $string
127
     *
128
     * @return bool|int
129
     */
130 14
    protected function dashSeparator($string)
131
    {
132 14
        return strpos($string, '-');
133
    }
134
135
    /**
136
     * @param $string
137
     *
138
     * @return bool|int
139
     */
140 11
    protected function commaSeparator($string)
141
    {
142 11
        return strpos($string, ',');
143
    }
144
}
145