Elgg  Version 1.12
CommitMessage.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
6 
21  private $validMsgParts = array(
22  1 => 'type',
23  2 => 'component',
24  3 => 'summary',
25  5 => 'body'
26  );
27 
33  private $type;
34 
40  private $component;
41 
47  private $summary;
48 
54  private $body;
55 
61  private $originalMsg = '';
62 
68  private $msg = '';
69 
75  private $lengthyLines;
76 
82  private static $validTypes = array(
83  'feature',
84  'feat',
85  'fix',
86  'fixes',
87  'fixed',
88  'doc',
89  'docs',
90  'chore',
91  'perf',
92  'performance',
93  'security',
94  'deprecate',
95  'deprecates'
96  );
97 
105  private $validComponents = array(
106  'i18n',
107  'seo',
108  'a11y',
109  'cache',
110  'db',
111  'views',
112  'session',
113  'router'
114  );
115 
121  private $ignoreRegex = '/^Merge |^Revert /i';
122 
140  private $formatRegex = "/^(\w*)\(([\w]+)\)\: ([^\n]*)(\n\n?(.*))?$/is";
141 
146  private $maxLineLength = 160;
147 
153  public function __construct($msg = null) {
154  if ($msg) {
155  $this->setMsg($msg);
156  }
157  }
158 
166  public function setMsg($msg) {
167  $this->originalMsg = $msg;
168 
169  $msg = str_replace(array("\r", "\n"), "\n", $msg);
170  $this->msg = $this->removeComments($msg);
171  $this->processMsg();
172  }
173 
179  public function getMsg() {
180  return $this->msg;
181  }
182 
188  public function getOriginalMsg() {
189  return $this->originalMsg;
190  }
191 
197  public function shouldIgnore() {
198  return preg_match($this->ignoreRegex, $this->msg) === 1;
199  }
200 
206  private function processMsg() {
207  $matches = array();
208 
209  preg_match($this->formatRegex, $this->msg, $matches);
210  foreach ($this->validMsgParts as $i => $part) {
211  $this->$part = isset($matches[$i]) ? $matches[$i] : '';
212  }
213 
214  $this->lengthyLines = $this->findLengthyLines($this->msg, $this->maxLineLength);
215  }
216 
222  public function isValid() {
223  return $this->isValidFormat() &&
224  $this->isValidLineLength() &&
225  $this->isValidType();
226  }
227 
233  public function isValidFormat() {
234  return preg_match($this->formatRegex, $this->msg) === 1;
235  }
236 
244  public function isValidLineLength() {
245  return count($this->lengthyLines) === 0;
246  }
247 
253  public function getLengthyLines() {
254  return $this->lengthyLines;
255  }
256 
262  public function isValidType() {
263  return in_array($this->type, self::$validTypes);
264  }
265 
271  public static function getValidTypes() {
272  return self::$validTypes;
273  }
274 
280  public function getMaxLineLength() {
281  return $this->maxLineLength;
282  }
283 
292  public function setMaxLineLength($len) {
293  $this->maxLineLength = (int)$len;
294  }
295 
304  public function getPart($part) {
305  if ($part && in_array($part, $this->validMsgParts)) {
306  return $this->$part;
307  }
308 
309  throw new UnexpectedValueException("`$part` not a valid message part.");
310  }
311 
319  public static function removeComments($msg) {
320  $msg_arr = array();
321  foreach (explode("\n", rtrim($msg)) as $line) {
322  if (substr($line, 0, 1) !== '#') {
323  $msg_arr[] = $line;
324  }
325  }
326 
327  return implode("\n", $msg_arr);
328  }
329 
338  public static function findLengthyLines($msg, $max_len) {
339  $lines = explode("\n", $msg);
340  $lengthy_lines = array();
341 
342  foreach ($lines as $i => $line) {
343  if (strlen($line) > $max_len) {
344  $lengthy_lines[] = ++$i;
345  }
346  }
347 
348  return $lengthy_lines;
349  }
350 
351 
353  public function __toString() {
354  return $this->getMsg();
355  }
356 }
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.
isValidType()
Is the type valid.
getOriginalMsg()
Return the original message.
getMsg()
Return the processed message.
isValidFormat()
Whether the message format conforms to our standards.
$summary
Definition: full.php:21
static findLengthyLines($msg, $max_len)
Returns an array of line numbers > $max_len.
isValidLineLength()
Are any of the lines too long?
Save menu items.
setMsg($msg)
Sets the active message.
getPart($part)
Get part of the message.
$type
Definition: add.php:8
shouldIgnore()
Should this msg be ignored for formatting?
__construct($msg=null)
Checks if a commit message is in the correct format.
setMaxLineLength($len)
Sets the max line length allowed.
list style type
Definition: admin.php:753
getMaxLineLength()
Return the max line length.