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

GroupByBugMergeTemplate::groupRevisionsByBugs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
cc 4
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 string  $path            Path.
31
	 * @param array   $revisions       Revisions.
32
	 * @param string  $repository_url  Repository URL.
33
	 * @param string  $relative_path   Relative path.
34
	 * @param boolean $merge_direction Merge direction.
35
	 *
36
	 * @return string
37
	 */
38 1
	protected function generateGroupBody($path, array $revisions, $repository_url, $relative_path, $merge_direction)
39
	{
40 1
		$merged_messages = array();
41 1
		$revision_log = $this->revisionLogFactory->getRevisionLog($repository_url . $path);
42 1
		$revisions_data = $revision_log->getRevisionsData('summary', $revisions);
43 1
		$revisions_grouped = $this->groupRevisionsByBugs($revision_log->getRevisionsData('bugs', $revisions));
44
45 1
		$unprocessed_revisions = $revisions;
46
47
		// Group revisions without bugs.
48 1
		foreach ( $revisions_grouped as $bug_revisions ) {
49 1
			$bug_title_added = false;
50
51 1
			foreach ( $bug_revisions as $revision ) {
52 1
				$commit_message_parts = explode(PHP_EOL, $revisions_data[$revision]['msg'], 2);
53 1
				$bug_title = array_shift($commit_message_parts);
54 1
				$commit_message = $commit_message_parts ? implode(PHP_EOL, $commit_message_parts) : '(no details)';
55
56 1
				if ( !$bug_title_added ) {
57 1
					$merged_messages[] = ' * ' . $bug_title;
58 1
					$bug_title_added = true;
59
				}
60
61 1
				$merged_messages[] = 'r' . $revision . ': ' . $commit_message;
62
			}
63
64 1
			$unprocessed_revisions = array_diff($unprocessed_revisions, $bug_revisions);
65
		}
66
67
		// Group revisions without bugs.
68 1
		if ( $unprocessed_revisions ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $unprocessed_revisions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69 1
			$merged_messages[] = 'Revisions without Bug IDs:';
70
71 1
			foreach ( $unprocessed_revisions as $revision ) {
72 1
				$merged_messages[] = ' * r' . $revision . ': ' . $revisions_data[$revision]['msg'];
73
			}
74
		}
75
76 1
		$merged_messages = array_unique(array_map('trim', $merged_messages));
77
78 1
		if ( ($revisions_grouped && $unprocessed_revisions)
0 ignored issues
show
Bug Best Practice introduced by
The expression $unprocessed_revisions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79 1
			|| count($revisions_grouped) > 1
80 1
			|| count($unprocessed_revisions) > 1
81
		) {
82 1
			$ret = '';
83 1
			$ret .= $this->generateGroupHeading($path, $relative_path, $merge_direction) . PHP_EOL;
84 1
			$ret .= implode(PHP_EOL, $merged_messages);
85
86 1
			return $ret;
87
		}
88
89
90 1
		$ret = '';
91 1
		$ret .= $this->generateGroupHeading($path, $relative_path, $merge_direction, false) . ': ';
92 1
		$ret .= implode(PHP_EOL, $merged_messages);
93
94 1
		return $ret;
95
	}
96
97
	/**
98
	 * Groups revisions by bugs.
99
	 *
100
	 * @param array $revisions_bugs Revisions bugs.
101
	 *
102
	 * @return array
103
	 */
104 1
	protected function groupRevisionsByBugs(array $revisions_bugs)
105
	{
106 1
		$ret = array();
107
108 1
		foreach ( $revisions_bugs as $revision => $revision_bugs ) {
109 1
			foreach ( $revision_bugs as $bug ) {
110 1
				if ( !isset($ret[$bug]) ) {
111 1
					$ret[$bug] = array();
112
				}
113
114 1
				$ret[$bug][] = $revision;
115
			}
116
		}
117
118 1
		return $ret;
119
	}
120
121
}
122