Completed
Push — master ( 7d4d47...4dc2a1 )
by Peter
08:01
created

AnnotationsMatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 97.06%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 58
ccs 33
cts 34
cp 0.9706
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 4 1
B matches() 0 46 6
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 http://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\ParseException;
18
use Maslosoft\Addendum\Interfaces\Matcher\MatcherInterface;
19
use Maslosoft\Addendum\Matcher\Helpers\Processor;
20
21
class AnnotationsMatcher implements MatcherInterface
22
{
23
24
	use Traits\PluginsTrait;
25
26 53
	protected function process($string)
27
	{
28 53
		return Processor::process($this, $string);
29 1
	}
30
31 53
	public function matches($string, &$annotations)
32
	{
33 53
		$string = $this->process($string);
34 53
		$annotations = [];
35 53
		$annotationMatcher = new AnnotationMatcher;
36 53
		$annotationMatcher->setPlugins($this->getPlugins());
37 53
		while (true)
38
		{
39 53
			if (preg_match('~\s(?=@)~', $string, $matches, PREG_OFFSET_CAPTURE))
40 53
			{
41 53
				$offset = $matches[0][1] + 1;
42 53
				$string = substr($string, $offset);
43 53
			}
44
			else
45
			{
46 53
				return; // no more annotations
47
			}
48
49 53
			if (($length = $annotationMatcher->matches($string, $data)) !== false)
50 53
			{
51 51
				$srcString = $string;
52 51
				$string = substr($string, $length);
53 51
				list($name, $params) = $data;
54 51
				$annotations[$name][] = $params;
55
56
				// If have some params, match should be fine
57 51
				if (!empty($params))
58 51
				{
59 41
					continue;
60
				}
61
62
				// Check if we should have some match
63 20
				$stringParams = trim(preg_split("~[\r\n]~", $srcString)[0], '() ');
64
65 20
				if (strlen($stringParams) > $length)
66 20
				{
67
					$msgParams = [
68 1
						$name,
69
						$stringParams
70 1
					];
71 1
					$msg = vsprintf('Could not parse params `%s` annotation near `%s`', $msgParams);
72 1
					throw new ParseException($msg);
73
				}
74 19
			}
75 50
		}
76
	}
77
78
}
79