Completed
Push — master ( 7d8097...8a93d3 )
by Peter
06:00 queued 01:01
created

AnnotationsMatcher::matches()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 6.0012

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 46
ccs 30
cts 31
cp 0.9677
rs 8.4751
c 1
b 0
f 0
cc 6
eloc 25
nc 6
nop 2
crap 6.0012
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\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 54
	protected function process($string)
27
	{
28 54
		return Processor::process($this, $string);
29 1
	}
30
31 54
	public function matches($string, &$annotations)
32
	{
33 54
		$string = $this->process($string);
34 54
		$annotations = [];
35 54
		$annotationMatcher = new AnnotationMatcher;
36 54
		$annotationMatcher->setPlugins($this->getPlugins());
37 54
		while (true)
38
		{
39 54
			if (preg_match('~\s(?=@)~', $string, $matches, PREG_OFFSET_CAPTURE))
40 54
			{
41 54
				$offset = $matches[0][1] + 1;
42 54
				$string = substr($string, $offset);
43 54
			}
44
			else
45
			{
46 54
				return; // no more annotations
47
			}
48
49 54
			if (($length = $annotationMatcher->matches($string, $data)) !== false)
50 54
			{
51 52
				$srcString = $string;
52 52
				$string = substr($string, $length);
53 52
				list($name, $params) = $data;
54 52
				$annotations[$name][] = $params;
55
56
				// If have some params, match should be fine
57 52
				if (!empty($params))
58 52
				{
59 42
					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 51
		}
76
	}
77
78
}
79