Failed Conditions
Push — master ( dd969d...42da22 )
by Alexander
10:30
created

RevisionListParser::expandRanges()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 9.1111
cc 6
nc 6
nop 2
crap 6
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Repository\Parser;
12
13
14
class RevisionListParser
15
{
16
17
	/**
18
	 * Expands ranges in revision list.
19
	 *
20
	 * @param array  $revisions       Revisions.
21
	 * @param string $range_separator Range separator.
22
	 *
23
	 * @return array
24
	 * @throws \InvalidArgumentException When inverted range is used.
25
	 * @throws \InvalidArgumentException When invalid revision is used.
26
	 */
27 9
	public function expandRanges(array $revisions, $range_separator = '-')
28
	{
29
		// Since SVN 1.7+ there can be "*" at the end of merged revision or revision range.
30 9
		$ret = array();
31 9
		$range_regexp = '/^([\d]+)' . preg_quote($range_separator, '/') . '([\d]+)\*?$/';
32
33 9
		foreach ( $revisions as $raw_revision ) {
34 9
			if ( preg_match($range_regexp, $raw_revision, $regs) ) {
35 5
				$range_start = (int)$regs[1];
36 5
				$range_end = (int)$regs[2];
37
38 5
				if ( $range_start > $range_end ) {
39 1
					throw new \InvalidArgumentException(
40 1
						'Inverted revision range "' . $raw_revision . '" is not implemented.'
41
					);
42
				}
43
44 4
				for ( $i = $range_start; $i <= $range_end; $i++ ) {
45 4
					$ret[$i] = true;
46
				}
47
			}
48 6
			elseif ( preg_match('/^([\d]+)\*?$/', $raw_revision, $regs) ) {
49 6
				$ret[(int)$regs[1]] = true;
50
			}
51
			else {
52 1
				throw new \InvalidArgumentException('The "' . $raw_revision . '" revision is invalid.');
53
			}
54
		}
55
56 7
		return array_keys($ret);
57
	}
58
59
}
60