LogMessageParser   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 33
c 0
b 0
f 0
dl 0
loc 107
ccs 33
cts 33
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B parse() 0 27 7
A _parseBugTraqLogRegex() 0 15 3
A _replaceMantisToJIRA() 0 10 2
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Repository\Parser;
12
13
14
class LogMessageParser
15
{
16
17
	/**
18
	 * Regular expression for pre-filtering bugs in log message.
19
	 *
20
	 * @var string
21
	 */
22
	private $_preFilterRegExp = '';
23
24
	/**
25
	 * Regular expression for matching bugs in log message.
26
	 *
27
	 * @var string
28
	 */
29
	private $_filterRegExp = '';
30
31
	/**
32
	 * Create instance of log message parser.
33
	 *
34
	 * @param string $bugtraq_logregex Regular expression(-s) for bug id finding in log message.
35
	 */
36 29
	public function __construct($bugtraq_logregex)
37
	{
38 29
		$bugtraq_logregex = $this->_replaceMantisToJIRA($bugtraq_logregex);
39
40 29
		$this->_parseBugTraqLogRegex($bugtraq_logregex);
41
	}
42
43
	/**
44
	 * Replaces Mantis to JIRA.
45
	 *
46
	 * @param string $bugtraq_logregex Regular expression(-s) for bug id finding in log message.
47
	 *
48
	 * @return string
49
	 */
50 29
	private function _replaceMantisToJIRA($bugtraq_logregex)
51
	{
52 29
		$mantis_regexp = '(?:[Bb]ugs?|[Ii]ssues?|[Rr]eports?|[Ff]ixe?s?|[Rr]esolves?)+\s+(?:#?(?:\d+)[,\.\s]*)+';
53 29
		$mantis_regexp .= "\n" . '(\d+)' . "\n";
54
55 29
		if ( $bugtraq_logregex === $mantis_regexp ) {
56 1
			return '([A-Z]+\-\d+)';
57
		}
58
59 28
		return $bugtraq_logregex;
60
	}
61
62
	/**
63
	 * Parses "bugtraq:logregex" property (if any).
64
	 *
65
	 * @param string $bugtraq_logregex Regular expression(-s).
66
	 *
67
	 * @return void
68
	 */
69 29
	private function _parseBugTraqLogRegex($bugtraq_logregex)
70
	{
71 29
		$bugtraq_logregex = array_filter(explode(PHP_EOL, $bugtraq_logregex));
72
73 29
		if ( count($bugtraq_logregex) === 2 ) {
74 18
			$this->_preFilterRegExp = '/' . $bugtraq_logregex[0] . '/s';
75 18
			$this->_filterRegExp = '/' . $bugtraq_logregex[1] . '/s';
76
		}
77 11
		elseif ( count($bugtraq_logregex) === 1 ) {
78 10
			$this->_preFilterRegExp = '';
79 10
			$this->_filterRegExp = '/' . $bugtraq_logregex[0] . '/s';
80
		}
81
		else {
82 1
			$this->_preFilterRegExp = '';
83 1
			$this->_filterRegExp = '';
84
		}
85
	}
86
87
	/**
88
	 * Finds bug IDs in log message.
89
	 *
90
	 * @param string $log_message Log message.
91
	 *
92
	 * @return array
93
	 */
94 26
	public function parse($log_message)
95
	{
96 26
		if ( !$this->_filterRegExp ) {
97 1
			return array();
98
		}
99
100 25
		if ( $this->_preFilterRegExp ) {
101 18
			if ( !preg_match_all($this->_preFilterRegExp, $log_message, $pre_filter_regs) ) {
102 5
				return array();
103
			}
104
105 13
			$ret = array();
106
107 13
			foreach ( $pre_filter_regs[0] as $match ) {
108 13
				if ( preg_match_all($this->_filterRegExp, $match, $filter_regs) ) {
109 13
					$ret = array_merge($ret, $filter_regs[1]);
110
				}
111
			}
112
113 13
			return array_unique($ret);
114
		}
115
116 7
		if ( preg_match_all($this->_filterRegExp, $log_message, $filter_regs) ) {
117 6
			return array_unique($filter_regs[1]);
118
		}
119
120 1
		return array();
121
	}
122
123
}
124