Elgg  Version 1.11
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  'fix',
85  'docs',
86  'chore',
87  'perf',
88  'security',
89  'deprecate',
90  );
91 
99  private $validComponents = array(
100  'i18n',
101  'seo',
102  'a11y',
103  'cache',
104  'db',
105  'views',
106  'session',
107  'router'
108  );
109 
115  private $ignoreRegex = '/^Merge |^Revert /i';
116 
134  private $formatRegex = "/^(\w*)\(([\w]+)\)\: ([^\n]*)(\n\n?(.*))?$/is";
135 
140  private $maxLineLength = 160;
141 
147  public function __construct($msg = null) {
148  if ($msg) {
149  $this->setMsg($msg);
150  }
151  }
152 
160  public function setMsg($msg) {
161  $this->originalMsg = $msg;
162 
163  $msg = str_replace(array("\r", "\n"), "\n", $msg);
164  $this->msg = $this->removeComments($msg);
165  $this->processMsg();
166  }
167 
173  public function getMsg() {
174  return $this->msg;
175  }
176 
182  public function getOriginalMsg() {
183  return $this->originalMsg;
184  }
185 
191  public function shouldIgnore() {
192  return preg_match($this->ignoreRegex, $this->msg) === 1;
193  }
194 
200  private function processMsg() {
201  $matches = array();
202 
203  preg_match($this->formatRegex, $this->msg, $matches);
204  foreach ($this->validMsgParts as $i => $part) {
205  $this->$part = isset($matches[$i]) ? $matches[$i] : '';
206  }
207 
208  $this->lengthyLines = $this->findLengthyLines($this->msg, $this->maxLineLength);
209  }
210 
216  public function isValid() {
217  return $this->isValidFormat() &&
218  $this->isValidLineLength() &&
219  $this->isValidType();
220  }
221 
227  public function isValidFormat() {
228  return preg_match($this->formatRegex, $this->msg) === 1;
229  }
230 
238  public function isValidLineLength() {
239  return count($this->lengthyLines) === 0;
240  }
241 
247  public function getLengthyLines() {
248  return $this->lengthyLines;
249  }
250 
256  public function isValidType() {
257  return in_array($this->type, self::$validTypes);
258  }
259 
265  public static function getValidTypes() {
266  return self::$validTypes;
267  }
268 
274  public function getMaxLineLength() {
275  return $this->maxLineLength;
276  }
277 
286  public function setMaxLineLength($len) {
287  $this->maxLineLength = (int)$len;
288  }
289 
298  public function getPart($part) {
299  if ($part && in_array($part, $this->validMsgParts)) {
300  return $this->$part;
301  }
302 
303  throw new UnexpectedValueException("`$part` not a valid message part.");
304  }
305 
313  public static function removeComments($msg) {
314  $msg_arr = array();
315  foreach (explode("\n", rtrim($msg)) as $line) {
316  if (substr($line, 0, 1) !== '#') {
317  $msg_arr[] = $line;
318  }
319  }
320 
321  return implode("\n", $msg_arr);
322  }
323 
332  public static function findLengthyLines($msg, $max_len) {
333  $lines = explode("\n", $msg);
334  $lengthy_lines = array();
335 
336  foreach ($lines as $i => $line) {
337  if (strlen($line) > $max_len) {
338  $lengthy_lines[] = ++$i;
339  }
340  }
341 
342  return $lengthy_lines;
343  }
344 
345 
347  public function __toString() {
348  return $this->getMsg();
349  }
350 }
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:748
getMaxLineLength()
Return the max line length.