-
Notifications
You must be signed in to change notification settings - Fork 0
/
Annotations.php
72 lines (56 loc) · 1.44 KB
/
Annotations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Test;
/**
* Description of Annotations
*
* @author peter
*/
class Annotations {
public function getContracts($class) {
$res = array();
$reflection = new \ReflectionClass($class);
$methods = $reflection->getMethods();
foreach ($methods as $method) {
$res[$method->name] = $this->iterateAnnotations($method->getDocComment());
}
return $res;
}
private function iterateAnnotations($docs) {
$res = array();
//Go through each row
$docs = explode("\n", $docs);
$nrDocs = count($docs);
for ($i = 0; $i < $nrDocs; $i++) {
$startPos = strpos($docs[$i], '@');
// Stop if not found
if ($startPos !== false) {
// Next row is new annotation or end
if (
strpos($docs[$i + 1], '@') ||
strpos($docs[$i + 1], '*/')
) {
$anno = $this->extractValueFromAnnotation(substr($docs[$i], $startPos));
foreach($anno as $an) {
$res[$an['annotation']][] = $an['value'];
}
}
}
}
return $res;
}
private function extractValueFromAnnotation($annotation) {
$parts = \explode('&&', $annotation);
$res = array();
$annotation = '';
foreach($parts as $part) {
$data = explode('\\', $part);
// Set annotation, only first part has it specified.
if($annotation == '') {
$annotation = rtrim(ltrim($data[0], '@'));
}
$data = \explode('\\', $part);
$res[] = array('annotation' => $annotation, 'value' => rtrim(ltrim($data[1]), ' ;'));
}
return $res;
}
}