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