GroupByBugMergeTemplate   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
eloc 42
dl 0
loc 107
ccs 44
cts 44
cp 1
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A groupRevisionsByBugs() 0 15 4
B generateGroupBody() 0 59 11
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 2
	protected function generateGroupBody($path, array $revisions, $repository_url, $relative_path, $merge_direction)
39
	{
40 2
		$merged_messages = array();
41 2
		$revision_log = $this->revisionLogFactory->getRevisionLog($repository_url . $path);
42 2
		$revisions_data = $revision_log->getRevisionsData('summary', $revisions);
43 2
		$revisions_grouped = $this->groupRevisionsByBugs($revision_log->getRevisionsData('bugs', $revisions));
44
45 2
		$unprocessed_revisions = $revisions;
46
47
		// Group revisions without bugs.
48 2
		foreach ( $revisions_grouped as $bug_revisions ) {
49 2
			$bug_title_added = false;
50
51 2
			foreach ( $bug_revisions as $revision ) {
52 2
				$commit_message_parts = explode(PHP_EOL, $revisions_data[$revision]['msg']);
53 2
				$commit_message_parts = array_filter($commit_message_parts); // Removes empty lines.
54
55 2
				$bug_title = array_shift($commit_message_parts);
56 2
				$commit_message = $commit_message_parts ? implode(PHP_EOL, $commit_message_parts) : '(no details)';
57
58 2
				if ( !$bug_title_added ) {
59 2
					$merged_messages[] = ' * ' . $bug_title;
60 2
					$bug_title_added = true;
61
				}
62
63 2
				$merged_messages[] = 'r' . $revision . ': ' . $commit_message;
64
			}
65
66 2
			$unprocessed_revisions = array_diff($unprocessed_revisions, $bug_revisions);
67
		}
68
69
		// Group revisions without bugs.
70 2
		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...
71 2
			$merged_messages[] = 'Revisions without Bug IDs:';
72
73 2
			foreach ( $unprocessed_revisions as $revision ) {
74 2
				$merged_messages[] = ' * r' . $revision . ': ' . $revisions_data[$revision]['msg'];
75
			}
76
		}
77
78 2
		$merged_messages = array_unique(array_map('trim', $merged_messages));
79
80 2
		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...
81 2
			|| count($revisions_grouped) > 1
82 2
			|| count($unprocessed_revisions) > 1
83
		) {
84 1
			$ret = '';
85 1
			$ret .= $this->generateGroupHeading($path, $relative_path, $merge_direction) . PHP_EOL;
86 1
			$ret .= implode(PHP_EOL, $merged_messages);
87
88 1
			return $ret;
89
		}
90
91
92 2
		$ret = '';
93 2
		$ret .= $this->generateGroupHeading($path, $relative_path, $merge_direction, false) . ': ';
94 2
		$ret .= implode(PHP_EOL, $merged_messages);
95
96 2
		return $ret;
97
	}
98
99
	/**
100
	 * Groups revisions by bugs.
101
	 *
102
	 * @param array $revisions_bugs Revisions bugs.
103
	 *
104
	 * @return array
105
	 */
106 2
	protected function groupRevisionsByBugs(array $revisions_bugs)
107
	{
108 2
		$ret = array();
109
110 2
		foreach ( $revisions_bugs as $revision => $revision_bugs ) {
111 2
			foreach ( $revision_bugs as $bug ) {
112 2
				if ( !isset($ret[$bug]) ) {
113 2
					$ret[$bug] = array();
114
				}
115
116 2
				$ret[$bug][] = $revision;
117
			}
118
		}
119
120 2
		return $ret;
121
	}
122
123
}
124