AnnotationUtility   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 87.23%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 5
dl 0
loc 116
ccs 41
cts 47
cp 0.8723
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B fileWalker() 0 48 9
A rawAnnotate() 0 36 3
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\Utilities;
16
17
use Maslosoft\Addendum\Addendum;
18
use Maslosoft\Addendum\Builder\DocComment;
19
use Maslosoft\Addendum\Collections\MatcherConfig;
20
use Maslosoft\Addendum\Matcher\AnnotationsMatcher;
21
use Maslosoft\Addendum\Reflection\ReflectionFile;
22
use RecursiveDirectoryIterator;
23
use RecursiveIteratorIterator;
24
use RecursiveRegexIterator;
25
use RegexIterator;
26
27
/**
28
 * This is utility class, should not be used in production environment
29
 * @author Piotr
30
 */
31
class AnnotationUtility
32
{
33
34
	public $searchPaths = [
35
		'annotations'
36
	];
37
	public $settingsPath = 'config/Preferences/org/netbeans/modules/php/project/';
38
	public $outputPath = null;
39
40
	/**
41
	 * This utility method find files containing $annotations
42
	 * annotates them and performs callback
43
	 *
44
	 * TODO: Use FileWalker after extensive tests
45
	 * NOTE: It is recommended to use FileWalker class
46
	 *
47
	 * @see FileWalker
48
	 * @param string[] $annotations
49
	 * @param callback $callback param is file path
50
	 */
51 1
	public static function fileWalker($annotations, $callback, $searchPaths = [])
52
	{
53 1
		$patterns = [];
54
55 1
		foreach ($annotations as $annotation)
56
		{
57 1
			$annotation = preg_replace('~^@~', '', $annotation);
58 1
			$patterns[] = sprintf('~@%s~', $annotation);
59
		}
60
61 1
		foreach ($searchPaths as $path)
62
		{
63 1
			$directoryIterator = new RecursiveDirectoryIterator($path);
64 1
			$iterator = new RecursiveIteratorIterator($directoryIterator);
65 1
			$regexIterator = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
66 1
			foreach ($regexIterator as $matches)
67
			{
68 1
				$file = $matches[0];
69 1
				$parse = false;
70
71 1
				if (is_readable($file))
72
				{
73 1
					$contents = file_get_contents($file);
74
				}
75
				else
76
				{
77
					// TODO Log this
78
					continue;
79
				}
80 1
				foreach ($patterns as $pattern)
81
				{
82 1
					if ($parse)
83
					{
84
						continue;
85
					}
86 1
					if (preg_match($pattern, $contents))
87
					{
88 1
						$parse = true;
89
					}
90
				}
91 1
				if (!$parse)
92
				{
93 1
					continue;
94
				}
95 1
				call_user_func($callback, $file, $contents);
96
			}
97
		}
98 1
	}
99
100
	/**
101
	 * Annotate file without including php file and without using reflection.
102
	 * This method returns raw annotation values.
103
	 * <i>This is intended for various builders, which should not include files.</i>
104
	 * This <b>ALWAYS</b> parses file.
105
	 * @param string $file
106
	 * @param string $className <b>NOT RECOMMENDED!</b> Optional class name if multiple classes are declared in one file
107
	 * @return mixed[][]
108
	 */
109 1
	public static function rawAnnotate($file, $className = null)
110
	{
111
112 1
		$docExtractor = new DocComment();
113 1
		$docs = $docExtractor->forFile($file, $className);
114
115 1
		$matcher = new AnnotationsMatcher();
116 1
		$class = [];
117 1
		$matcher->setPlugins(new MatcherConfig([
118 1
			'addendum' => new Addendum(),
119 1
			'reflection' => new ReflectionFile($file)
120
		]));
121 1
		$matcher->matches($docs['class'], $class);
122
123 1
		$methods = [];
124 1
		foreach ((array) $docs['methods'] as $name => $doc)
125
		{
126
			$methods[$name] = [];
127
			$matcher->matches($doc, $methods[$name]);
128
		}
129
130 1
		$fields = [];
131 1
		foreach ((array) $docs['fields'] as $name => $doc)
132
		{
133
			$fields[$name] = [];
134
			$matcher->matches($doc, $fields[$name]);
135
		}
136
		$result = [
137 1
			'namespace' => $docs['namespace'],
138 1
			'className' => $docs['className'],
139 1
			'class' => $class,
140 1
			'methods' => $methods,
141 1
			'fields' => $fields
142
		];
143 1
		return $result;
144
	}
145
146
}
147