Elgg  Version 3.0
CommitMessage.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
20  private $validMsgParts = [
21  1 => 'type',
22  2 => 'component',
23  3 => 'summary',
24  5 => 'body'
25  ];
26 
32  private $type;
33 
39  private $component;
40 
46  private $summary;
47 
53  private $body;
54 
60  private $originalMsg = '';
61 
67  private $msg = '';
68 
74  private $lengthyLines;
75 
81  private static $validTypes = [
82  'feature',
83  'feat',
84  'fix',
85  'fixes',
86  'fixed',
87  'doc',
88  'docs',
89  'chore',
90  'perf',
91  'performance',
92  'security',
93  'deprecate',
94  'deprecates'
95  ];
96 
102  private $ignoreRegex = '/^Merge |^Revert /i';
103 
121  private $formatRegex = "/^(\w*)\(([\w]+)\)\: ([^\n]*)(\n\n?(.*))?$/is";
122 
127  private $maxLineLength = 160;
128 
134  public function __construct($msg = null) {
135  if ($msg) {
136  $this->setMsg($msg);
137  }
138  }
139 
147  public function setMsg($msg) {
148  $this->originalMsg = $msg;
149 
150  $msg = str_replace(["\r\n", "\r"], "\n", $msg);
151  $this->msg = $this->removeComments($msg);
152  $this->processMsg();
153  }
154 
160  public function getMsg() {
161  return $this->msg;
162  }
163 
169  public function getOriginalMsg() {
170  return $this->originalMsg;
171  }
172 
178  public function shouldIgnore() {
179  return preg_match($this->ignoreRegex, $this->msg) === 1;
180  }
181 
187  private function processMsg() {
188  $matches = [];
189 
190  preg_match($this->formatRegex, $this->msg, $matches);
191  foreach ($this->validMsgParts as $i => $part) {
192  $this->$part = isset($matches[$i]) ? $matches[$i] : '';
193  }
194 
195  $this->lengthyLines = $this->findLengthyLines($this->msg, $this->maxLineLength);
196  }
197 
203  public function isValid() {
204  return $this->isValidFormat() &&
205  $this->isValidLineLength() &&
206  $this->isValidType();
207  }
208 
214  public function isValidFormat() {
215  return preg_match($this->formatRegex, $this->msg) === 1;
216  }
217 
225  public function isValidLineLength() {
226  return count($this->lengthyLines) === 0;
227  }
228 
234  public function getLengthyLines() {
235  return $this->lengthyLines;
236  }
237 
243  public function isValidType() {
244  return in_array($this->type, self::$validTypes);
245  }
246 
252  public static function getValidTypes() {
253  return self::$validTypes;
254  }
255 
261  public function getMaxLineLength() {
262  return $this->maxLineLength;
263  }
264 
273  public function setMaxLineLength($len) {
274  $this->maxLineLength = (int) $len;
275  }
276 
285  public function getPart($part) {
286  if ($part && in_array($part, $this->validMsgParts)) {
287  return $this->$part;
288  }
289 
290  throw new UnexpectedValueException("`$part` not a valid message part.");
291  }
292 
300  public static function removeComments($msg) {
301  $msg_arr = [];
302  foreach (explode("\n", rtrim($msg)) as $line) {
303  if (substr($line, 0, 1) !== '#') {
304  $msg_arr[] = $line;
305  }
306  }
307 
308  return implode("\n", $msg_arr);
309  }
310 
319  public static function findLengthyLines($msg, $max_len) {
320  $lines = explode("\n", $msg);
321  $lengthy_lines = [];
322 
323  foreach ($lines as $i => $line) {
324  if (strlen($line) > $max_len) {
325  $lengthy_lines[] = ++$i;
326  }
327  }
328 
329  return $lengthy_lines;
330  }
331 
335  public function __toString() {
336  return $this->getMsg();
337  }
338 }
static removeComments($msg)
Removes all lines that start with #.
getLengthyLines()
Get the line number of lines that are too long.
static getValidTypes()
Return all valid types.
isValid()
Are all parts of the message valid.
$type
Definition: delete.php:21
isValidType()
Is the type valid.
getOriginalMsg()
Return the original message.
getMsg()
Return the processed message.
isValidFormat()
Whether the message format conforms to our standards.
static findLengthyLines($msg, $max_len)
Returns an array of line numbers > $max_len.
isValidLineLength()
Are any of the lines too long?
Configuration exception.
setMsg($msg)
Sets the active message.
getPart($part)
Get part of the message.
$body
Definition: useradd.php:60
shouldIgnore()
Should this msg be ignored for formatting?
$summary
Definition: header.php:24
__construct($msg=null)
Checks if a commit message is in the correct format.
setMaxLineLength($len)
Sets the max line length allowed.
getMaxLineLength()
Return the max line length.