Skip to content

Commit 9c09b2a

Browse files
committed
make docgen skip over extraneous code if class is not first line after
1 parent 6e32888 commit 9c09b2a

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

tools/docgen/parse.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ function parse_blocks(array $content): array
2828
{
2929
$blocks = array();
3030

31+
// Find the start of each DocBlock.
3132
$doc_start = array_keys($content, "/**");
3233
foreach ($doc_start as $start) {
34+
// Find the end of the current DocBlock as a key in the content array.
3335
$end = array_values(array_filter(array_keys($content, "*/"), function ($value) use ($start) {
3436
return ($value > $start);
3537
}));
@@ -42,14 +44,41 @@ function parse_blocks(array $content): array
4244
exit("[E] Parsing error: Method or class declaration could not be found under DocBlock.");
4345
}
4446

47+
// Grab the doc between start and end of the block.
4548
$doc = array_slice($content, $start + 1, $end[0] - $start - 1);
4649
foreach ($doc as $i => $line) {
4750
$doc[$i] = ltrim(ltrim($line, '*'));
4851
}
4952

53+
// The declaration of DocBlock type might not be the first line after the end. Filter out lines that
54+
// may contain some extraneous nonsense like `namespace` or `use`. Make sure not to keep going if none
55+
// of the values to skip over are found.
56+
$contentBetween = array_filter($content, fn ($key) => $key > $end[0], ARRAY_FILTER_USE_KEY);
57+
$n = 1;
58+
foreach ($contentBetween as $possibleDecl) {
59+
$skipValues = [
60+
'namespace',
61+
'use',
62+
];
63+
64+
$found = false;
65+
foreach ($skipValues as $skipValue) {
66+
if (str_starts_with($possibleDecl, $skipValue . ' ')) {
67+
$found = true;
68+
$n++;
69+
70+
break;
71+
}
72+
}
73+
74+
if (! $found) {
75+
break;
76+
}
77+
}
78+
5079
$blocks[] = [
5180
'doc' => $doc,
52-
'decl' => $content[$end[0] + 1]
81+
'decl' => $content[$end[0] + $n]
5382
];
5483
}
5584

0 commit comments

Comments
 (0)