RegexMatcher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 38
ccs 14
cts 17
cp 0.8235
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matches() 0 20 3
A process() 0 4 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link https://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Matcher;
16
17
use Maslosoft\Addendum\Exceptions\MatcherException;
18
use Maslosoft\Addendum\Interfaces\Matcher\MatcherInterface;
19
use Maslosoft\Addendum\Matcher\Traits\PluginsTrait;
20
21
class RegexMatcher implements MatcherInterface
22
{
23
	use PluginsTrait;
24
25
	protected $regex;
26
27 61
	public function __construct($regex)
28
	{
29 61
		$this->regex = $regex;
30 61
	}
31
32 61
	public function matches($string, &$value)
33
	{
34 61
		$matches = [];
35 61
		$matched = preg_match("/^{$this->regex}/", $string, $matches);
36 61
		if ($matched)
37
		{
38 61
			$value = $this->process($matches);
39 61
			return strlen($matches[0]);
40
		}
41 61
		if (false === $matched)
42
		{
43
			$params = [
44
				$this->regex,
45
				$string
46
			];
47
			throw new MatcherException(vsprintf('Could not interpret matcher regex: `%s`, When processing "%s". ', $params), preg_last_error());
48
		}
49 61
		$value = false;
50 61
		return false;
51
	}
52
53 61
	protected function process($matches)
54
	{
55 61
		return $matches[0];
56
	}
57
58
}
59