1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Annotation; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AnnotationParser |
9
|
|
|
* @package Dgame\Annotation |
10
|
|
|
*/ |
11
|
|
|
final class AnnotationParser |
12
|
|
|
{ |
13
|
|
|
private const REGEX = '\s+(.*?)\s*(\$.+?)?\s*\z'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $comment |
17
|
|
|
* |
18
|
|
|
* @return VariableAnnotation[] |
19
|
|
|
* @throws Exception |
20
|
|
|
*/ |
21
|
|
|
public function parseParameterAnnotations(string $comment): array |
22
|
|
|
{ |
23
|
|
|
return $this->parseAnnotations('param', $comment); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $comment |
28
|
|
|
* |
29
|
|
|
* @return VariableAnnotation[] |
30
|
|
|
* @throws Exception |
31
|
|
|
*/ |
32
|
|
|
public function parseVariableAnnotations(string $comment): array |
33
|
|
|
{ |
34
|
|
|
return $this->parseAnnotations('var', $comment); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $comment |
39
|
|
|
* |
40
|
|
|
* @return VariableAnnotation[] |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
|
|
public function parsePropertyAnnotations(string $comment): array |
44
|
|
|
{ |
45
|
|
|
return $this->parseAnnotations('property', $comment); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $annotation |
50
|
|
|
* @param string $comment |
51
|
|
|
* |
52
|
|
|
* @return VariableAnnotation[] |
53
|
|
|
* @throws Exception |
54
|
|
|
*/ |
55
|
|
|
private function parseAnnotations(string $annotation, string $comment): array |
56
|
|
|
{ |
57
|
|
|
$output = []; |
58
|
|
|
$lines = preg_split('/\R/m', $comment); |
59
|
|
|
$lines = $lines === false ? [] : $lines; |
60
|
|
|
foreach ($lines as $line) { |
61
|
|
|
$result = $this->parseAnnotation($annotation, $line); |
62
|
|
|
if ($result !== null) { |
63
|
|
|
$output[] = $result; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $output; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $annotation |
72
|
|
|
* @param string $line |
73
|
|
|
* |
74
|
|
|
* @return VariableAnnotation|null |
75
|
|
|
* @throws Exception |
76
|
|
|
*/ |
77
|
|
|
private function parseAnnotation(string $annotation, string $line): ?VariableAnnotation |
78
|
|
|
{ |
79
|
|
|
$regex = sprintf('/@%s%s/i', $annotation, self::REGEX); |
80
|
|
|
if (preg_match($regex, $line, $matches)) { |
81
|
|
|
[$type, $var] = $this->parseAnnotationMatch($matches); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return new VariableAnnotation($type, $var); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $matches |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
private function parseAnnotationMatch(array $matches): array |
95
|
|
|
{ |
96
|
|
|
array_shift($matches); |
97
|
|
|
if (!array_key_exists(0, $matches)) { |
98
|
|
|
$matches[0] = 'mixed'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!array_key_exists(1, $matches)) { |
102
|
|
|
$matches[1] = ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $matches; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.