Passed
Push — master ( 42da22...4e8764 )
by Alexander
02:00
created

SummaryMergeTemplate::apply()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 39
ccs 22
cts 22
cp 1
rs 8.6506
cc 7
nc 10
nop 1
crap 7
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 SummaryMergeTemplate extends AbstractMergeTemplate
15
{
16
17
	/**
18
	 * Returns merge template name.
19
	 *
20
	 * @return string
21
	 */
22 2
	public function getName()
23
	{
24 2
		return 'summary';
25
	}
26
27
	/**
28
	 * Applies merge template to a working copy.
29
	 *
30
	 * @param string $wc_path Working copy path.
31
	 *
32
	 * @return string
33
	 */
34 4
	public function apply($wc_path)
35
	{
36 4
		$merged_revisions = $this->repositoryConnector->getMergedRevisionChanges($wc_path, true);
37 4
		$unmerged_revisions = $this->repositoryConnector->getMergedRevisionChanges($wc_path, false);
38 4
		$has_merged_revisions = $this->flattenMergedRevisions($merged_revisions);
39 4
		$has_unmerged_revisions = $this->flattenMergedRevisions($unmerged_revisions);
40
41 4
		if ( !$has_merged_revisions && !$has_unmerged_revisions ) {
2 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...
42 2
			return '';
43
		}
44
45 2
		$target = $this->getMomentInTime(
46 2
			$this->repositoryConnector->getRelativePath($wc_path),
47 2
			$this->repositoryConnector->getLastRevision($wc_path)
48
		);
49
50 2
		$ret = '';
51
52 2
		foreach ( $merged_revisions as $path => $revisions ) {
53
			// No changes on particular path.
54 2
			if ( !$revisions ) {
55 1
				continue;
56
			}
57
58 1
			$source = $this->getMomentInTime($path, max($revisions));
59 1
			$ret .= PHP_EOL . 'Merge of "' . $source . '" to "' . $target . '".' . PHP_EOL;
60
		}
61
62 2
		foreach ( $unmerged_revisions as $path => $revisions ) {
63
			// No changes on particular path.
64 2
			if ( !$revisions ) {
65 1
				continue;
66
			}
67
68 1
			$source = $this->getMomentInTime($path, max($revisions));
69 1
			$ret .= PHP_EOL . 'Reverse-merge of "' . $source . '" to "' . $target . '".' . PHP_EOL;
70
		}
71
72 2
		return trim($ret);
73
	}
74
75
	/**
76
	 * Returns moment in time.
77
	 *
78
	 * @param string  $path     Path.
79
	 * @param integer $revision Revision.
80
	 *
81
	 * @return string
82
	 */
83 2
	protected function getMomentInTime($path, $revision)
84
	{
85 2
		return ltrim($path, '/') . '@' . $revision;
86
	}
87
88
}
89