Range   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A expand() 0 18 4
A format() 0 8 3
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Host;
9
10
class Range
11
{
12
    const PATTERN = '/\[(.+?)\]/';
13
14 23
    public static function expand(array $hostnames)
15
    {
16 23
        $expanded = [];
17 23
        foreach ($hostnames as $hostname) {
18 13
            if (preg_match(self::PATTERN, $hostname, $matches)) {
19 11
                list($start, $end) = explode(':', $matches[1]);
20 11
                $zeroBased = preg_match('/^0[1-9]/', $start);
21
22 11
                foreach (range($start, $end) as $i) {
23 11
                    $expanded[] = preg_replace(self::PATTERN, self::format($i, $zeroBased), $hostname);
24
                }
25
            } else {
26 13
                $expanded[] = $hostname;
27
            }
28
        }
29
30 23
        return $expanded;
31
    }
32
33 11
    private static function format($i, $zeroBased)
34
    {
35 11
        if ($zeroBased) {
36 5
            return strlen($i) === 1 ? "0$i" : $i;
37
        } else {
38 7
            return $i;
39
        }
40
    }
41
}
42