AbstractGroupByMergeTemplate   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 45
c 1
b 0
f 0
dl 0
loc 118
ccs 44
cts 44
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A doGenerate() 0 11 2
A apply() 0 25 5
A generateGroupHeading() 0 27 5
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
abstract class AbstractGroupByMergeTemplate extends AbstractMergeTemplate
15
{
16
17
	/**
18
	 * Applies merge template to a working copy.
19
	 *
20
	 * @param string $wc_path Working copy path.
21
	 *
22
	 * @return string
23
	 */
24 8
	public function apply($wc_path)
25
	{
26 8
		$ret = array();
27 8
		$merged_revisions = $this->repositoryConnector->getMergedRevisionChanges($wc_path, true);
28 8
		$unmerged_revisions = $this->repositoryConnector->getMergedRevisionChanges($wc_path, false);
29 8
		$has_merged_revisions = $this->flattenMergedRevisions($merged_revisions);
30 8
		$has_unmerged_revisions = $this->flattenMergedRevisions($unmerged_revisions);
31
32 8
		if ( !$has_merged_revisions && !$has_unmerged_revisions ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $has_merged_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...
Bug Best Practice introduced by
The expression $has_unmerged_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...
33 4
			return '';
34
		}
35
36 4
		$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
37 4
		$relative_path = $this->repositoryConnector->getRelativePath($wc_url);
38 4
		$repository_url = $this->repositoryConnector->getRootUrl($wc_url);
39
40 4
		if ( $has_merged_revisions ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $has_merged_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...
41 2
			$ret[] = $this->doGenerate($merged_revisions, $relative_path, $repository_url, true);
42
		}
43
44 4
		if ( $has_unmerged_revisions ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $has_unmerged_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...
45 2
			$ret[] = $this->doGenerate($unmerged_revisions, $relative_path, $repository_url, false);
46
		}
47
48 4
		return \implode(\PHP_EOL . \PHP_EOL, $ret);
49
	}
50
51
	/**
52
	 * Builds commit message.
53
	 *
54
	 * @param array   $merged_revisions Merged revisions.
55
	 * @param string  $relative_path    Relative path.
56
	 * @param string  $repository_url   Repository url.
57
	 * @param boolean $merge_direction  Merge direction.
58
	 *
59
	 * @return string
60
	 */
61 4
	protected function doGenerate(array $merged_revisions, $relative_path, $repository_url, $merge_direction)
62
	{
63 4
		$ret = '';
64
65 4
		foreach ( $merged_revisions as $path => $revisions ) {
66 4
			$ret .= PHP_EOL;
67 4
			$ret .= $this->generateGroupBody($path, $revisions, $repository_url, $relative_path, $merge_direction);
68 4
			$ret .= PHP_EOL;
69
		}
70
71 4
		return trim($ret);
72
	}
73
74
	/**
75
	 * Builds group heading.
76
	 *
77
	 * @param string  $source_path     Source path for merge operation.
78
	 * @param string  $target_path     Target path for merge operation.
79
	 * @param boolean $merge_direction Merge direction.
80
	 * @param boolean $long            Generate long heading.
81
	 *
82
	 * @return string
83
	 */
84 4
	protected function generateGroupHeading($source_path, $target_path, $merge_direction, $long = true)
85
	{
86 4
		$from_path = basename($source_path);
87 4
		$source_project = $this->repositoryConnector->getProjectUrl($source_path);
88
89 4
		$to_path = basename($target_path);
90 4
		$target_project = $this->repositoryConnector->getProjectUrl($target_path);
91
92 4
		if ( $source_project !== $target_project ) {
93 4
			$from_project_parts = explode('/', $source_project);
94 4
			$from_path .= ' (' . end($from_project_parts) . ')';
95
		}
96
97 4
		if ( $long ) {
98 3
			return sprintf(
99 3
				'%s from %s to %s',
100 3
				$merge_direction ? 'Merging' : 'Reverse-merging',
101 3
				ucfirst($from_path),
102 3
				ucfirst($to_path)
103 3
			);
104
		}
105
106 4
		return sprintf(
107 4
			'%s (%s > %s)',
108 4
			$merge_direction ? 'Merge' : 'Reverse-merge',
109 4
			$from_path,
110 4
			$to_path
111 4
		);
112
	}
113
114
	/**
115
	 * Builds group body.
116
	 *
117
	 * @param string  $path            Path.
118
	 * @param array   $revisions       Revisions.
119
	 * @param string  $repository_url  Repository URL.
120
	 * @param string  $relative_path   Relative path.
121
	 * @param boolean $merge_direction Merge direction.
122
	 *
123
	 * @return string
124
	 */
125
	abstract protected function generateGroupBody(
126
		$path,
127
		array $revisions,
128
		$repository_url,
129
		$relative_path,
130
		$merge_direction
131
	);
132
133
}
134