Failed Conditions
Push — master ( 3d1f7c...ce06ec )
by Alexander
02:49
created

CommitMessageBuilder::getFreshMergedRevisions()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 25
cts 25
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 5
nop 1
crap 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A CommitMessageBuilder::getFragmentForRecentConflicts() 0 17 3
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;
12
13
14
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
15
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLogFactory;
16
17
class CommitMessageBuilder
18
{
19
20
	/**
21
	 * Repository connector.
22
	 *
23
	 * @var Connector
24
	 */
25
	protected $repositoryConnector;
26
27
	/**
28
	 * Revision log factory.
29
	 *
30
	 * @var RevisionLogFactory
31
	 */
32
	protected $revisionLogFactory;
33
34
	/**
35
	 * Working copy conflict tracker.
36
	 *
37
	 * @var WorkingCopyConflictTracker
38
	 */
39
	protected $workingCopyConflictTracker;
40
41
	/**
42
	 * Creates commit message builder instance.
43
	 *
44
	 * @param Connector                  $repository_connector          Repository connector.
45
	 * @param RevisionLogFactory         $revision_log_factory          Revision log factory.
46
	 * @param WorkingCopyConflictTracker $working_copy_conflict_tracker Working copy conflict tracker.
47
	 */
48 6
	public function __construct(
49
		Connector $repository_connector,
50
		RevisionLogFactory $revision_log_factory,
51
		WorkingCopyConflictTracker $working_copy_conflict_tracker
52
	) {
53 6
		$this->repositoryConnector = $repository_connector;
54 6
		$this->revisionLogFactory = $revision_log_factory;
55 6
		$this->workingCopyConflictTracker = $working_copy_conflict_tracker;
56 6
	}
57
58
	/**
59
	 * Builds a commit message.
60
	 *
61
	 * @param string      $wc_path    Working copy path.
62
	 * @param string|null $changelist Changelist.
63
	 *
64
	 * @return string
65
	 */
66 5
	public function build($wc_path, $changelist = null)
67
	{
68
		/*
69
		 * 3. if it's In-Portal project, then:
70
		 * - create commit message that:
71
		 * -- Merge of "{from_path}@{from_rev}" to "{to_path}@{to_rev}".
72
		 * -- Merge of "in-portal/branches/5.2.x@16189" to "in-portal/branches/5.3.x@16188".
73
		 * - {from_path} to be determined from list of merged revisions
74
		 * - {from_rev} - last changed of {from_path} by looking in repo
75
		 * - {to_path} to be determined from working copy
76
		 * - {to_rev} - last changed of {to_path} by looking in repo
77
		 * 4. open interactive editor with auto-generated message
78
		 */
79
80 5
		$commit_message_parts = array();
81
82 5
		if ( strlen($changelist) ) {
83 2
			$commit_message_parts[] = trim($changelist);
84 2
		}
85
86 5
		$commit_message_parts[] = $this->getFragmentForMergedRevisions($wc_path);
87 5
		$commit_message_parts[] = $this->getFragmentForRecentConflicts($wc_path);
88
89 5
		return implode(PHP_EOL, array_filter($commit_message_parts));
90
	}
91
92
	/**
93
	 * Returns commit message fragment for merged revisions.
94
	 *
95
	 * @param string $wc_path Working copy path.
96
	 *
97
	 * @return string
98
	 */
99 5
	protected function getFragmentForMergedRevisions($wc_path)
100
	{
101 5
		$merged_revisions = $this->repositoryConnector->getFreshMergedRevisions($wc_path);
102
103 5
		if ( !$merged_revisions ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $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...
104 3
			return '';
105
		}
106
107 2
		$ret = '';
108 2
		$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
109 2
		$repository_url = $this->repositoryConnector->getRootUrl($wc_url);
110
111 2
		foreach ( $merged_revisions as $path => $revisions ) {
112 2
			$merged_messages = array();
113 2
			$revision_log = $this->revisionLogFactory->getRevisionLog($repository_url . $path);
114 2
			$ret .= PHP_EOL . $this->getCommitMessageHeading($wc_url, $path) . PHP_EOL;
115
116 2
			$revisions_data = $revision_log->getRevisionsData('summary', $revisions);
117
118 2
			foreach ( $revisions as $revision ) {
119 2
				$merged_messages[] = ' * r' . $revision . ': ' . $revisions_data[$revision]['msg'];
120 2
			}
121
122 2
			$merged_messages = array_unique(array_map('trim', $merged_messages));
123 2
			$ret .= implode(PHP_EOL, $merged_messages) . PHP_EOL;
124 2
		}
125
126 2
		return trim($ret);
127
	}
128
129
	/**
130
	 * Builds commit message heading.
131
	 *
132
	 * @param string $wc_url Working copy url.
133
	 * @param string $path   Source path for merge operation.
134
	 *
135
	 * @return string
136
	 */
137 2
	protected function getCommitMessageHeading($wc_url, $path)
138
	{
139 2
		return 'Merging from ' . ucfirst(basename($path)) . ' to ' . ucfirst(basename($wc_url));
140
	}
141
142
	/**
143
	 * Returns commit message fragment for recent conflicts.
144
	 *
145
	 * @param string $wc_path Working copy path.
146
	 *
147
	 * @return string
148
	 */
149 5
	protected function getFragmentForRecentConflicts($wc_path)
150
	{
151 5
		$recorded_conflicts = $this->workingCopyConflictTracker->getRecordedConflicts($wc_path);
152
153 5
		if ( !$recorded_conflicts ) {
154 3
			return '';
155
		}
156
157
		// Ensure empty line before.
158 2
		$ret = PHP_EOL . 'Conflicts:';
159
160 2
		foreach ( $recorded_conflicts as $conflict_path ) {
161 2
			$ret .= PHP_EOL . ' * ' . $conflict_path;
162 2
		}
163
164 2
		return $ret;
165
	}
166
167
}
168