Failed Conditions
Push — master ( 1ba572...22cc0d )
by Alexander
02:39 queued 11s
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 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
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
	 * Collapses ranges.
61
	 *
62
	 * @param array  $revisions       Revisions.
63
	 * @param string $range_separator Range separator.
64
	 *
65
	 * @return array
66
	 */
67 1
	public function collapseRanges(array $revisions, $range_separator = '-')
68
	{
69 1
		sort($revisions, SORT_NUMERIC);
70 1
		$revisions = array_map('intval', $revisions);
71
72 1
		$ret = array();
73 1
		$range_start = $range_end = null;
74
75 1
		foreach ( $revisions as $revision ) {
76
			// New range detected.
77 1
			if ( $range_start === null ) {
78 1
				$range_start = $range_end = $revision;
79 1
				continue;
80
			}
81
82
			// Expanding existing range.
83 1
			if ( $range_end + 1 === $revision ) {
84 1
				$range_end = $revision;
85 1
				continue;
86
			}
87
88 1
			$ret[] = $range_start === $range_end ? $range_start : $range_start . $range_separator . $range_end;
0 ignored issues
show
Bug introduced by
Are you sure $range_end of type void can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
			$ret[] = $range_start === $range_end ? $range_start : $range_start . $range_separator . /** @scrutinizer ignore-type */ $range_end;
Loading history...
89 1
			$range_start = $range_end = $revision;
90
		}
91
92 1
		if ( $range_start !== null && $range_end !== null ) {
93 1
			$ret[] = $range_start === $range_end ? $range_start : $range_start . $range_separator . $range_end;
94
		}
95
96 1
		return $ret;
97
	}
98
99
}
100