Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
193 lines (165 loc) · 5.46 KB

File metadata and controls

193 lines (165 loc) · 5.46 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php declare(strict_types = 1);
/**
* This file is part of ScaleUpStack/Annotations.
*
* For the full copyright and license information, please view the README.md and LICENSE.md files that were distributed
* with this source code.
*
* @copyright 2019 - present ScaleUpVentures GmbH, https://www.scaleupventures.com
* @link https://github.com/scaleupstack/annotations
*/
namespace ScaleUpStack\Annotations;
final class DocBlockParser
{
/**
* @param int $context
* One of Annotations::CONTEXT_*
*/
public function parse(string $docBlock, int $context) : Annotations
{
$collection = new Annotations();
$strippedLines = $this->stripDocBlock($docBlock);
$annotationsData = $this->extractTagsAndArguments($strippedLines);
foreach ($annotationsData as $data) {
$collection->add(
$data['tag'],
$data['arguments'],
$context
);
}
return $collection;
}
/**
* Strippes all the DocBlock formatting and returns an array with the lines.
*/
private function stripDocBlock(string $docBlock) : array
{
if ('' == $docBlock) {
return [];
}
$lines = explode("\n", $docBlock);
// validate first line
$firstLine = array_shift($lines);
Assert::same(
'/**',
$firstLine,
'First line of DocBlock must be "/**" but %2$s given.'
);
// validate last line
$lastLine = array_pop($lines);
Assert::regex(
$lastLine,
'(^[ ]+\*/$)',
'Last line of DocBlock must be " */" but %1$s given.'
);
// validate other lines
Assert::allRegex(
$lines,
'/^[ ]+\*( |$)/',
'Lines in a DocBlock must start with " * " or equal to " *", but %s given.'
);
// remove leading ' * ' or ' *' and return
return preg_replace(
'/^[ ]+\* ?/',
'',
$lines
);
}
/**
* Parses the lines and combines multi-line arguments
*/
private function extractTagsAndArguments(array $lines) : array
{
if ([] === $lines) {
return [];
}
$data = [];
$tag = null;
$stateSearchStartOfTag = 1;
$stateSearchEndOfMultiLineValue = 2;
$currentState = $stateSearchStartOfTag;
foreach ($lines as $line) {
if ($currentState === $stateSearchStartOfTag) {
// pattern: ^@<name-of-tag><optional: space plus rest of line>
$pattern = '(^@([a-z:-]+)( (.*))?$)';
$count = preg_match($pattern, $line, $matches);
if (1 !== $count) {
continue;
}
// line with starting tag
$tag = $matches[1];
$restOfLine = '';
if (array_key_exists(3, $matches)) {
$restOfLine = trim($matches[3], ' ');
}
if ('{' !== $restOfLine) {
// single-line arguments string
$data[] = [
'tag' => $tag,
'arguments' => $restOfLine,
];
} else {
// start of a multi-line arguments string
$currentState = $stateSearchEndOfMultiLineValue;
$arguments = [];
}
} else if ($currentState === $stateSearchEndOfMultiLineValue) {
if ('}' !== $line) {
// additional line in multi-line argument string
$arguments[] = $line;
} else {
// end of a multi-line argument string
$data[] = [
'tag' => $tag,
'arguments' => $this->trim($arguments),
];
$currentState = $stateSearchStartOfTag;
}
}
}
if (
! is_null($tag) &&
$currentState !== $stateSearchStartOfTag
) {
throw new InvalidArgumentException(
sprintf('Closing curly bracket in multi-line annotation is missing for @%s.', $tag)
);
}
return $data;
}
/**
* Alligns the lines on the left so that at least in one line there are no preceding spaces, and removes trailing
* spaces on the right.
*/
private function trim(array $lines) : string
{
// find shortest prefix of spaces
$shortestSpacePrefix = null;
$pattern = '(^([ ]*)[^ ])';
foreach ($lines as $line) {
$count = preg_match($pattern, $line, $matches);
if (1 === $count) {
$currentSpacePrefix = strlen($matches[1]);
if (
is_null($shortestSpacePrefix) ||
$shortestSpacePrefix > $currentSpacePrefix
) {
$shortestSpacePrefix = $currentSpacePrefix;
}
}
}
// remove prefix in all lines
$replacePattern = sprintf('/^([ ]{%d})/', $shortestSpacePrefix);
$lines = preg_replace(
$replacePattern,
'',
$lines
);
$lines = preg_replace(
'/([ ]+)$/',
'',
$lines
);
return implode("\n", $lines);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.