Completed
Push — master ( 28256c...92a7d3 )
by Peter
20:38
created

AnnotationUtility::rawAnnotate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0327

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 36
ccs 22
cts 26
cp 0.8462
rs 8.8571
cc 3
eloc 24
nc 4
nop 2
crap 3.0327
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\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
	 * @param string[] $annotations
44
	 * @param callback $callback param is file path
45
	 */
46 1
	public static function fileWalker($annotations, $callback, $searchPaths = [])
47
	{
48 1
		$patterns = [];
49
50 1
		foreach ($annotations as $annotation)
51
		{
52 1
			$annotation = preg_replace('~^@~', '', $annotation);
53 1
			$patterns[] = sprintf('~@%s~', $annotation);
54 1
		}
55
56 1
		foreach ($searchPaths as $path)
57
		{
58 1
			$directoryIterator = new RecursiveDirectoryIterator($path);
59 1
			$iterator = new RecursiveIteratorIterator($directoryIterator);
60 1
			$regexIterator = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
61 1
			foreach ($regexIterator as $matches)
62
			{
63 1
				$file = $matches[0];
64 1
				$parse = false;
65
66 1
				if (is_readable($file))
67 1
				{
68 1
					$contents = file_get_contents($file);
69 1
				}
70
				else
71
				{
72
					// TODO Log this
73
					continue;
74
				}
75 1
				foreach ($patterns as $pattern)
76
				{
77
					if ($parse)
78 1
					{
79
						continue;
80
					}
81 1
					if (preg_match($pattern, $contents))
82 1
					{
83 1
						$parse = true;
84 1
					}
85 1
				}
86 1
				if (!$parse)
87 1
				{
88 1
					continue;
89
				}
90 1
				call_user_func($callback, $file, $contents);
91 1
			}
92 1
		}
93 1
	}
94
95
	/**
96
	 * Annotate file without including php file and without using reflection.
97
	 * This method returns raw annotation values.
98
	 * <i>This is intented for various builders, which should not include files.</i>
99
	 * This <b>ALWAYS</b> parses file.
100
	 * @param string $file
101
	 * @param string $className <b>NOT RECOMMENDED!</b> Optional class name if multiple classes are declared in one file
102
	 * @return mixed[][]
103
	 */
104 1
	public static function rawAnnotate($file, $className = null)
105
	{
106
107 1
		$docExtractor = new DocComment();
108 1
		$docs = $docExtractor->forFile($file, $className);
109
110 1
		$matcher = new AnnotationsMatcher();
111 1
		$class = [];
112 1
		$matcher->setPlugins(new MatcherConfig([
113 1
			'addendum' => new Addendum(),
114 1
			'reflection' => new ReflectionFile($file)
115 1
		]));
116 1
		$matcher->matches($docs['class'], $class);
117
118 1
		$methods = [];
119 1
		foreach ((array) $docs['methods'] as $name => $doc)
120
		{
121
			$methods[$name] = [];
122
			$matcher->matches($doc, $methods[$name]);
123 1
		}
124
125 1
		$fields = [];
126 1
		foreach ((array) $docs['fields'] as $name => $doc)
127
		{
128
			$fields[$name] = [];
129
			$matcher->matches($doc, $fields[$name]);
130 1
		}
131
		$result = [
132 1
			'namespace' => $docs['namespace'],
133 1
			'className' => $docs['className'],
134 1
			'class' => $class,
135 1
			'methods' => $methods,
136
			'fields' => $fields
137 1
		];
138 1
		return $result;
139
	}
140
141
}
142