Completed
Push — master ( 8c59ef...24dea2 )
by Alexander
03:02
created

GroupByBugMergeTemplate::groupRevisionsByBugs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4
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\CommitMessage;
12
13
14
class GroupByBugMergeTemplate extends AbstractGroupByMergeTemplate
15
{
16
17
	/**
18
	 * Returns merge template name.
19
	 *
20
	 * @return string
21
	 */
22 2
	public function getName()
23
	{
24 2
		return 'group_by_bug';
25
	}
26
27
	/**
28
	 * Builds group body.
29
	 *
30
	 * @param array  $revisions  Revisions.
31
	 * @param string $source_url Source URL.
32
	 *
33
	 * @return string
34
	 */
35 1
	protected function generateGroupBody(array $revisions, $source_url)
36
	{
37 1
		$merged_messages = array();
38 1
		$revision_log = $this->revisionLogFactory->getRevisionLog($source_url);
39 1
		$revisions_data = $revision_log->getRevisionsData('summary', $revisions);
40 1
		$revisions_grouped = $this->groupRevisionsByBugs($revision_log->getRevisionsData('bugs', $revisions));
41
42 1
		$unprocessed_revisions = $revisions;
43
44
		// Group revisions without bugs.
45 1
		foreach ( $revisions_grouped as $bug_revisions ) {
46 1
			$bug_title_added = false;
47
48 1
			foreach ( $bug_revisions as $revision ) {
49 1
				$commit_message_parts = explode(PHP_EOL, $revisions_data[$revision]['msg'], 2);
50 1
				$bug_title = array_shift($commit_message_parts);
51 1
				$commit_message = $commit_message_parts ? implode(PHP_EOL, $commit_message_parts) : '(no details)';
52
53 1
				if ( !$bug_title_added ) {
54 1
					$merged_messages[] = ' * ' . $bug_title;
55 1
					$bug_title_added = true;
56
				}
57
58 1
				$merged_messages[] = 'r' . $revision . ': ' . $commit_message;
59
			}
60
61 1
			$unprocessed_revisions = array_diff($unprocessed_revisions, $bug_revisions);
62
		}
63
64
		// Group revisions without bugs.
65 1
		if ( $unprocessed_revisions ) {
66 1
			$merged_messages[] = 'Revisions without Bug IDs:';
67
68 1
			foreach ( $unprocessed_revisions as $revision ) {
69 1
				$merged_messages[] = ' * r' . $revision . ': ' . $revisions_data[$revision]['msg'];
70
			}
71
		}
72
73 1
		$merged_messages = array_unique(array_map('trim', $merged_messages));
74
75 1
		return implode(PHP_EOL, $merged_messages);
76
	}
77
78
	/**
79
	 * Groups revisions by bugs.
80
	 *
81
	 * @param array $revisions_bugs Revisions bugs.
82
	 *
83
	 * @return array
84
	 */
85 1
	protected function groupRevisionsByBugs(array $revisions_bugs)
86
	{
87 1
		$ret = array();
88
89 1
		foreach ( $revisions_bugs as $revision => $revision_bugs ) {
90 1
			foreach ( $revision_bugs as $bug ) {
91 1
				if ( !isset($ret[$bug]) ) {
92 1
					$ret[$bug] = array();
93
				}
94
95 1
				$ret[$bug][] = $revision;
96
			}
97
		}
98
99 1
		return $ret;
100
	}
101
102
}
103