1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PurpleBooth\GitGitHubLint; |
6
|
|
|
|
7
|
|
|
use PurpleBooth\GitGitHubLint\Status\Status; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A commit message |
11
|
|
|
* |
12
|
|
|
* @package PurpleBooth\GitGitHubLint |
13
|
|
|
*/ |
14
|
|
|
class MessageImplementation implements Message |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $commitMessage; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $sha; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Status |
28
|
|
|
*/ |
29
|
|
|
private $status; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Message constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $sha |
35
|
|
|
* @param string $commitMessage |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $sha, string $commitMessage) |
38
|
|
|
{ |
39
|
|
|
$this->commitMessage = explode("\n", $commitMessage); |
40
|
|
|
$this->sha = $sha; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Is the title capitalised |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function isTitleCapitalised() : bool |
49
|
|
|
{ |
50
|
|
|
if ($this->getTitleLength() == 0) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return strtoupper($this->commitMessage[0]){0} === $this->commitMessage[0]{0}; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the title length |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
public function getTitleLength() : int |
63
|
|
|
{ |
64
|
|
|
return strlen($this->commitMessage[0]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Title ends with a full stop |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function hasTitleAFullStop() : bool |
73
|
|
|
{ |
74
|
|
|
if ($this->getTitleLength() == 0) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$lastCharacter = trim($this->commitMessage[0]){$this->getTitleLength() - 1}; |
79
|
|
|
|
80
|
|
|
return $lastCharacter == "."; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Has a gap after the title |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function hasBlankLineAfterTitle() : bool |
89
|
|
|
{ |
90
|
|
|
if (count($this->commitMessage) < 2) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->commitMessage[1] == ""; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Has this message got a body |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function hasABody(): bool |
103
|
|
|
{ |
104
|
|
|
// Has a body |
105
|
|
|
if ($this->getBodyWrapLength() > 0) { |
106
|
|
|
// First line isn't long |
107
|
|
|
return strlen($this->commitMessage[2]) > 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The length at which the message wraps |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function getBodyWrapLength() : int |
119
|
|
|
{ |
120
|
|
|
if (count($this->commitMessage) < 3) { |
121
|
|
|
return 0; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$body = array_slice($this->commitMessage, 2); |
125
|
|
|
$longestLineLength = 0; |
126
|
|
|
|
127
|
|
|
foreach ($body as $line) { |
128
|
|
|
if (strlen($line) > $longestLineLength) { |
129
|
|
|
$longestLineLength = strlen($line); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $longestLineLength; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the SHA that identifies this commit |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getSha() : string |
142
|
|
|
{ |
143
|
|
|
return $this->sha; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the status associated with this message |
148
|
|
|
* |
149
|
|
|
* @throws |
150
|
|
|
* |
151
|
|
|
* @return Status |
152
|
|
|
*/ |
153
|
|
|
public function getStatus() |
154
|
|
|
{ |
155
|
|
|
return $this->status; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Associate a status with this message |
160
|
|
|
* |
161
|
|
|
* @param Status $status |
162
|
|
|
*/ |
163
|
|
|
public function setStatus(Status $status) |
164
|
|
|
{ |
165
|
|
|
$this->status = $status; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|