-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocBlockParser.php
More file actions
245 lines (209 loc) · 8.07 KB
/
Copy pathDocBlockParser.php
File metadata and controls
245 lines (209 loc) · 8.07 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
declare(strict_types=1);
namespace TypeLang\PhpDoc;
use JetBrains\PhpStorm\Language;
use TypeLang\PhpDoc\DocBlock\Combinator\DescriptionCombinator;
use TypeLang\PhpDoc\DocBlock\Description\DescriptionInterface;
use TypeLang\PhpDoc\DocBlock\DocBlock;
use TypeLang\PhpDoc\DocBlock\Tag\TagInterface;
use TypeLang\PhpDoc\Exception\ParsingException;
use TypeLang\PhpDoc\Exception\PhpDocExceptionInterface;
use TypeLang\PhpDoc\Exception\TagParsingException;
use TypeLang\PhpDoc\Parser\Description\BalancedBraceAwareParser;
use TypeLang\PhpDoc\Parser\Description\DescriptionParserInterface;
use TypeLang\PhpDoc\Parser\DocBlockAnalyzer;
use TypeLang\PhpDoc\Parser\Grammar\CombinatorInterface;
use TypeLang\PhpDoc\Parser\Splitter\Segment;
use TypeLang\PhpDoc\Parser\Splitter\SplitterInterface;
use TypeLang\PhpDoc\Parser\Splitter\StringSplitter;
use TypeLang\PhpDoc\Parser\Tag\StringTagParser;
use TypeLang\PhpDoc\Parser\Tag\TagParserInterface;
use TypeLang\PhpDoc\Parser\TagFactory;
use TypeLang\PhpDoc\Parser\TagRegistryBuilder;
use TypeLang\PhpDoc\Platform\PhanPlatform;
use TypeLang\PhpDoc\Platform\PhpCodeSnifferPlatform;
use TypeLang\PhpDoc\Platform\PhpDocumentorPlatform;
use TypeLang\PhpDoc\Platform\PhpStanPlatform;
use TypeLang\PhpDoc\Platform\PhpStormPlatform;
use TypeLang\PhpDoc\Platform\PlatformInterface;
use TypeLang\PhpDoc\Platform\PsalmPlatform;
use TypeLang\PhpDoc\Platform\StandardPlatform;
/**
* @phpstan-import-type CombinatorType from CombinatorInterface
*/
final readonly class DocBlockParser implements DocBlockParserInterface
{
public TagFactoryInterface $factory;
public TagRegistryInterface $tags;
private DocBlockAnalyzer $docBlockAnalyzer;
private TagParserInterface $tagParser;
private DescriptionParserInterface $descriptionParser;
/**
* @param iterable<mixed, PlatformInterface> $platforms additional tag platforms
*/
public function __construct(iterable $platforms = [])
{
$platforms = $this->loadPlatforms($platforms);
$this->tags = $this->createTagRegistry($platforms);
$this->factory = $this->createTagFactory($this->tags, $this->createCombinators($platforms));
$this->docBlockAnalyzer = $this->createAnalyzer($this->createDocBlockSplitter());
$this->tagParser = $this->createTagParser($this->factory);
$this->descriptionParser = $this->createDescriptionParser($this->tagParser);
}
/**
* @param iterable<mixed, PlatformInterface> $additionalPlatforms
*/
public static function createDefault(iterable $additionalPlatforms = []): self
{
return new self([
new PhpDocumentorPlatform(),
new PhpStanPlatform(),
new PsalmPlatform(),
new PhanPlatform(),
new PhpStormPlatform(),
new PhpCodeSnifferPlatform(),
...\iterator_to_array($additionalPlatforms, false),
]);
}
/**
* @param iterable<mixed, PlatformInterface> $platforms
* @return non-empty-list<PlatformInterface>
*/
private function loadPlatforms(iterable $platforms): array
{
// The standard platform is always loaded first; the caller's platforms
// extend it, overriding an entry when they reuse its name.
$result = [new StandardPlatform()];
foreach ($platforms as $platform) {
$result[] = $platform;
}
return $result;
}
private function createAnalyzer(SplitterInterface $splitter): DocBlockAnalyzer
{
return new DocBlockAnalyzer($splitter);
}
private function createDocBlockSplitter(): SplitterInterface
{
return new StringSplitter();
}
/**
* @param list<PlatformInterface> $platforms
*/
private function createTagRegistry(array $platforms): TagRegistryInterface
{
$definitions = [];
$aliases = [];
foreach ($platforms as $platform) {
foreach ($platform->tags as $name => $definition) {
$definitions[$name] = $definition;
}
foreach ($platform->aliases as $alias => $canonical) {
$aliases[$alias] = $canonical;
}
}
return new TagRegistryBuilder($definitions, $aliases)
->build();
}
/**
* @param list<PlatformInterface> $platforms
* @return array<non-empty-string, CombinatorType>
*/
private function createCombinators(array $platforms): array
{
$combinators = [];
foreach ($platforms as $platform) {
foreach ($platform->combinators as $name => $combinator) {
$combinators[$name] = $combinator;
}
}
// Description is always present and cannot be redefined
$combinators[DescriptionCombinator::NAME] = new \ReflectionClass(DescriptionCombinator::class)
->newLazyProxy(fn(): DescriptionCombinator => new DescriptionCombinator(
descriptionParser: $this->descriptionParser,
));
return $combinators;
}
/**
* @param array<non-empty-string, CombinatorType> $combinators
*/
private function createTagFactory(TagRegistryInterface $registry, array $combinators): TagFactoryInterface
{
return new TagFactory(
registry: $registry,
combinators: $combinators,
);
}
private function createTagParser(TagFactoryInterface $factory): TagParserInterface
{
return new StringTagParser($factory);
}
private function createDescriptionParser(TagParserInterface $parser): DescriptionParserInterface
{
return new BalancedBraceAwareParser($parser);
}
/**
* @param list<Segment> $segments
* @return list<TagInterface>
* @throws PhpDocExceptionInterface
*/
private function createTags(array $segments, string $docblock): array
{
$result = [];
foreach ($segments as $segment) {
try {
$result[] = $this->tagParser->parse($segment->text);
} catch (\Throwable $e) {
throw $this->failure($e, $docblock, $segment->offset);
}
}
return $result;
}
/**
* @throws PhpDocExceptionInterface
*/
private function tryCreateDescription(?Segment $segment, string $docblock): ?DescriptionInterface
{
if ($segment === null) {
return null;
}
try {
return $this->descriptionParser->tryParse($segment->text);
} catch (\Throwable $e) {
throw $this->failure($e, $docblock, $segment->offset);
}
}
/**
* Guarantees that only a {@see PhpDocExceptionInterface} leaves the parser.
*
* A parsing error is rebased onto the full docblock so that its offset
* becomes absolute (e.g. the position of the tag that failed to parse). Any
* other, internal failure is wrapped as a {@see TagParsingException} at the
* same location.
*
* @param int<0, max> $offset byte offset of the failing section inside $source
*/
private function failure(\Throwable $e, string $source, int $offset): PhpDocExceptionInterface
{
// A parsing error carries an offset relative to the failing section;
// rebasing it onto $source turns that into an absolute location.
if ($e instanceof ParsingException) {
return $e->withSource($source, $e->offset + $offset);
}
// Any other library error is already contractual and passes through.
if ($e instanceof PhpDocExceptionInterface) {
return $e;
}
// An internal (non-library) failure is wrapped so that the whole
// docblock and the failing offset are still reported to the caller.
return TagParsingException::becauseInternalErrorOccurs($e, $source, $offset);
}
public function parse(#[Language('InjectablePHP')] string $docblock): DocBlock
{
$data = $this->docBlockAnalyzer->analyze($docblock);
return new DocBlock(
description: $this->tryCreateDescription($data->description, $docblock),
tags: $this->createTags($data->tags, $docblock),
);
}
}