Elgg  Version 2.3
CommitMessage.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
6 
23  private $validMsgParts = array(
24  1 => 'type',
25  2 => 'component',
26  3 => 'summary',
27  5 => 'body'
28  );
29 
35  private $type;
36 
42  private $component;
43 
49  private $summary;
50 
56  private $body;
57 
63  private $originalMsg = '';
64 
70  private $msg = '';
71 
77  private $lengthyLines;
78 
84  private static $validTypes = array(
85  'feature',
86  'feat',
87  'fix',
88  'fixes',
89  'fixed',
90  'doc',
91  'docs',
92  'chore',
93  'perf',
94  'performance',
95  'security',
96  'deprecate',
97  'deprecates'
98  );
99 
107  private $validComponents = array(
108  'i18n',
109  'seo',
110  'a11y',
111  'cache',
112  'db',
113  'views',
114  'session',
115  'router'
116  );
117 
123  private $ignoreRegex = '/^Merge |^Revert /i';
124 
142  private $formatRegex = "/^(\w*)\(([\w]+)\)\: ([^\n]*)(\n\n?(.*))?$/is";
143 
148  private $maxLineLength = 160;
149 
155  public function __construct($msg = null) {
156  if ($msg) {
157  $this->setMsg($msg);
158  }
159  }
160 
168  public function setMsg($msg) {
169  $this->originalMsg = $msg;
170 
171  $msg = str_replace(array("\r", "\n"), "\n", $msg);
172  $this->msg = $this->removeComments($msg);
173  $this->processMsg();
174  }
175 
181  public function getMsg() {
182  return $this->msg;
183  }
184 
190  public function getOriginalMsg() {
191  return $this->originalMsg;
192  }
193 
199  public function shouldIgnore() {
200  return preg_match($this->ignoreRegex, $this->msg) === 1;
201  }
202 
208  private function processMsg() {
209  $matches = array();
210 
211  preg_match($this->formatRegex, $this->msg, $matches);
212  foreach ($this->validMsgParts as $i => $part) {
213  $this->$part = isset($matches[$i]) ? $matches[$i] : '';
214  }
215 
216  $this->lengthyLines = $this->findLengthyLines($this->msg, $this->maxLineLength);
217  }
218 
224  public function isValid() {
225  return $this->isValidFormat() &&
226  $this->isValidLineLength() &&
227  $this->isValidType();
228  }
229 
235  public function isValidFormat() {
236  return preg_match($this->formatRegex, $this->msg) === 1;
237  }
238 
246  public function isValidLineLength() {
247  return count($this->lengthyLines) === 0;
248  }
249 
255  public function getLengthyLines() {
256  return $this->lengthyLines;
257  }
258 
264  public function isValidType() {
265  return in_array($this->type, self::$validTypes);
266  }
267 
273  public static function getValidTypes() {
274  return self::$validTypes;
275  }
276 
282  public function getMaxLineLength() {
283  return $this->maxLineLength;
284  }
285 
294  public function setMaxLineLength($len) {
295  $this->maxLineLength = (int)$len;
296  }
297 
306  public function getPart($part) {
307  if ($part && in_array($part, $this->validMsgParts)) {
308  return $this->$part;
309  }
310 
311  throw new UnexpectedValueException("`$part` not a valid message part.");
312  }
313 
321  public static function removeComments($msg) {
322  $msg_arr = array();
323  foreach (explode("\n", rtrim($msg)) as $line) {
324  if (substr($line, 0, 1) !== '#') {
325  $msg_arr[] = $line;
326  }
327  }
328 
329  return implode("\n", $msg_arr);
330  }
331 
340  public static function findLengthyLines($msg, $max_len) {
341  $lines = explode("\n", $msg);
342  $lengthy_lines = array();
343 
344  foreach ($lines as $i => $line) {
345  if (strlen($line) > $max_len) {
346  $lengthy_lines[] = ++$i;
347  }
348  }
349 
350  return $lengthy_lines;
351  }
352 
353 
355  public function __toString() {
356  return $this->getMsg();
357  }
358 }
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.
list style type
Definition: admin.css.php:808
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.
shouldIgnore()
Should this msg be ignored for formatting?
$summary
Definition: header.php:12
__construct($msg=null)
Checks if a commit message is in the correct format.
setMaxLineLength($len)
Sets the max line length allowed.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
getMaxLineLength()
Return the max line length.
if(!$display_name) $type
Definition: delete.php:27