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

ClassLiteralMatcher::matches()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.0163

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 24
cts 26
cp 0.9231
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 6
nop 2
crap 6.0163
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\ClassNotFoundException;
18
use Maslosoft\Addendum\Interfaces\Matcher\MatcherInterface;
19
use Maslosoft\Addendum\Interfaces\Plugins\Matcher\MatcherClassNotFoundHandlerInterface;
20
use Maslosoft\Addendum\Matcher\Helpers\Processor;
21
use Maslosoft\Addendum\Matcher\Traits\PluginsTrait;
22
use Maslosoft\Addendum\Utilities\ClassChecker;
23
use Maslosoft\Addendum\Utilities\ReflectionName;
24
use Maslosoft\Gazebo\PluginFactory;
25
26
/**
27
 * ClassLiteralMatcher
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
class ClassLiteralMatcher implements MatcherInterface
32
{
33
34
	use PluginsTrait;
35
36 14
	protected function process($matches)
37
	{
38 14
		return Processor::process($this, $matches[1]);
39
	}
40
41 44
	public function matches($string, &$value)
42
	{
43 44
		$matches = [];
44 44
		$regex = '([A-Z\\\][a-zA-Z0-9_\\\]+)';
45 44
		if (preg_match("/^{$regex}/", $string, $matches))
46 44
		{
47 14
			$value = $this->process($matches);
48 14
			if (!ClassChecker::exists($value))
49 14
			{
50
				// Might be constant...
51 4
				if (!defined($matches[0]))
52 4
				{
53 1
					$name = ReflectionName::createName($this->getPlugins()->reflection);
54
55
					$params = [
56 1
						$matches[0],
57 1
						$name,
58
						$string
59 1
					];
60 1
					$plugins = $this->getPlugins()->addendum->plugins->matcher;
61
62 1
					$instances = PluginFactory::fly()->create($plugins, $this, MatcherClassNotFoundHandlerInterface::class);
63
64
					/* @var $instances MatcherClassNotFoundHandlerInterface[] */
65
66 1
					foreach($instances as $ignorer)
67
					{
68 1
						if($ignorer->shouldSkip($matches[0]))
69 1
						{
70 1
							return false;
71
						}
72
					}
73
					throw new ClassNotFoundException(vsprintf("Could not find class %s, when processing annotations on %s, near %s", $params));
74
				}
75 3
				return false;
76
			}
77 11
			return strlen($matches[0]);
78
		}
79 39
		$value = false;
80 39
		return false;
81
	}
82
83
}
84