pchCommitMessageCheck::parseStatements PHP Method

parseStatements() protected method

If a parse error occures an array with meesages will be returned. If the commit message matches the defined grammar an empty array will be returned. A result array with all parsed messages will be stored in the following form and may be requested using the getResult() method: array( array( 'type' => , 'bug' => | null, 'text' => , ), ... )
protected parseStatements ( string $string ) : array
$string string
return array
    protected function parseStatements($string)
    {
        $lines = explode("\n", $string);
        $statements = array();
        $statement = null;
        foreach ($lines as $line) {
            // Skip empty lines
            if (trim($line) === '') {
                continue;
            }
            // Check for line length
            $line = rtrim($line);
            $echodLine = '"' . substr($line, 0, 30) . '..."';
            if (strlen($line) > 79) {
                return array(new pchIssue(E_ERROR, null, null, "Too long line: {$echodLine}"));
            }
            if (preg_match('(^-\\x20
              (?# Type of statement )
                (?P<type>' . implode('|', array_keys($this->rules)) . ')
              (?# Match optional bug number )
                (?:\\x20\\#(?P<bug>[1-9]+[0-9]*))?
              (?# Match required text line )
                (?::\\x20(?P<text>[\\x20-\\x7E]+))?
            $)x', $line, $match)) {
                // Check if required text has been included in message
                if (!isset($match['text']) || empty($match['text'])) {
                    return array(new pchIssue(E_ERROR, null, null, "Textual description missing in line {$echodLine}"));
                }
                // Check if bug number has been set for statements requireing a
                // bug number
                if ($this->rules[$match['type']] === self::REQUIRED && (!isset($match['bug']) || empty($match['bug']))) {
                    return array(new pchIssue(E_ERROR, null, null, "Missing bug number in line: {$echodLine}"));
                }
                // Ensure no bug number has been provided for statements which
                // may not be used together with a bug number
                if ($this->rules[$match['type']] === self::PROHIBITED) {
                    if (isset($match['bug']) && !empty($match['bug'])) {
                        return array(new pchIssue(E_ERROR, null, null, "Superflous bug number in line: {$echodLine}"));
                    }
                    // Force bug to null, so we can use this variable later
                    $match['bug'] = null;
                }
                // Append prior statement to statement array
                if ($statement !== null) {
                    $statements[] = $statement;
                }
                // Create new statement from data
                $statement = array('type' => str_replace('Closed', 'Fixed', $match['type']), 'bug' => $match['bug'], 'text' => $match['text']);
            } elseif (preg_match('(^  (?P<text>[\\x20-\\x7E]+)$)', $line, $match)) {
                if ($statement == null) {
                    // Each additional comment line has to be preceeded by a
                    // statement
                    return array(new pchIssue(E_ERROR, null, null, "No statement precedes text line: {$echodLine}"));
                }
                $statement['text'] .= ' ' . $match['text'];
            } else {
                return array(new pchIssue(E_ERROR, null, null, "Invalid commit message: {$echodLine}\n\nAllowed are messages following this grammar:\n\n" . $this->getEBNF()));
            }
        }
        // Append last statement
        if ($statement !== null) {
            $statements[] = $statement;
        }
        $this->result = $statements;
        return array();
    }