-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_generate.php
287 lines (242 loc) · 7.34 KB
/
_generate.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use StageRightLabs\Bloom\ServiceFactory;
use StageRightLabs\Bloom\Utility\Text;
/**
* Generate markdown documentation for service classes using phpdoc-parser.
*/
foreach (ServiceFactory::getRegistry() as $key => $service) {
// Prepare to extract doc block details from the service class via reflection
$class = new \ReflectionClass($service);
// Ensure that this class should have a documentation file
if (classShouldBeExcluded($class)) {
continue;
}
// Prepare a markdown buffer to hold the generated content
$markdown = '';
// Write the page heading to markdown
$h1 = camelCaseToTitleString(maybePluralize($key));
$markdown .= "# {$h1} \n\n";
// Write the page content to markdown
if ($class->getDocComment()) {
// Parse the class documentation string
$documentation = parseDocumentationString($class->getDocComment());
// Write the text content to markdown
foreach (getDocumentationText($documentation) as $text) {
$markdown .= "{$text}\n\n";
}
// Write reference links to markdown
$links = $documentation->getTagsByName('@see');
if (!empty($links)) {
$markdown .= "Further Reading:\n\n";
foreach ($links as $link) {
$markdown .= "- [{$link->value}]({$link->value})\n";
}
}
$markdown .= "\n";
}
// write out the details for each method in the class
foreach ($class->getMethods() as $method) {
// Skip any methods that are flagged for exclusion
if (methodShouldBeExcluded($method)) {
continue;
}
// Parse the method documentation
$documentation = parseDocumentationString($method->getDocComment());
// Write the text content to markdown
$markdown .= "## {$method->getName()}()\n\n";
foreach (getDocumentationText($documentation) as $text) {
$markdown .= "{$text}\n\n";
}
// Write the list of parameters to markdown
$parameters = $documentation->getParamTagValues();
if (!empty($parameters)) {
$markdown .= "### Parameters\n\n";
$markdown .= writeParametersTable($parameters);
$markdown .= "\n";
}
// Write the return value to markdown
$returns = $documentation->getReturnTagValues();
if (!empty($returns)) {
$markdown .= "### Return Type\n\n";
foreach ($returns as $return) {
$markdown .= formatTypeString($return) . "\n";
}
}
// Write reference links to markdown
$links = $documentation->getTagsByName('@see');
if (!empty($links)) {
$markdown .= "\n### Further Reading:\n\n";
foreach ($links as $link) {
$markdown .= "- [{$link->value}]({$link->value})\n";
}
}
$markdown .= "\n";
}
$markdown .= "###### This page was dynamically generated from the {$class->getShortName()} source code.\n\n";
// save the file.
$filename = Text::snakeCase($key) . '.md';
$path = __DIR__ . '/' . $filename;
file_put_contents($path, $markdown);
echo "Wrote {$path}\n";
}
/**
* Parse a documentation string with the PHPStan phpdoc-parser.
*
* @param string $documentation
* @return PhpDocNode
*/
function parseDocumentationString(string $documentation): PhpDocNode
{
$lexer = new Lexer();
$constExprParser = new ConstExprParser();
$typeParser = new TypeParser($constExprParser);
return (new PhpDocParser($typeParser, $constExprParser))
->parse(new TokenIterator($lexer->tokenize($documentation)));
}
/**
* Should this class be excluded from the documentation?
*
* @param ReflectionClass $reflectionClass
* @return bool
*/
function classShouldBeExcluded(ReflectionClass $reflectionClass): bool
{
$ignored = [
'HorizonService',
];
return in_array($reflectionClass->getShortName(), $ignored);
}
/**
* Should this method be excluded from the documentation?
*
* @param ReflectionMethod $method
* @return bool
*/
function methodShouldBeExcluded(ReflectionMethod $method): bool
{
$ignored = [
'__construct'
];
if (in_array($method->getName(), $ignored)) {
return true;
}
return !$method->isPublic();
}
/**
* Convert a camel case string into a human readable title case string.
*
* @param string $str
* @return string
*/
function camelCaseToTitleString(string $str): string
{
return ucwords(preg_replace('/(?<!\ )[A-Z]/', ' $0', $str));
}
/**
* Pluralize a title string, unless it shouldn't be pluralized.
*
* @param string $str
* @return string
*/
function maybePluralize(string $str): string
{
$ignore = [
'friendbot',
'horizon',
];
if (!in_array($str, $ignore)) {
$str .= 's';
}
return $str;
}
/**
* Extract the text nodes from a parsed doc block.
*
* @param PhpDocNode $node
* @return PhpDocTextNode[]
*/
function getDocumentationText(PhpDocNode $node): array
{
return array_filter($node->children, function ($child) {
return $child instanceof PhpDocTextNode
&& !empty($child->text);
});
}
/**
* Convert an array of parameter nodes into a markdown table.
*
* @param array $parameters
* @return string
*/
function writeParametersTable(array $parameters): string
{
$includeNotes = doParametersContainNotes($parameters);
// Header
$table = "| Name | Type ";
if ($includeNotes) {
$table .= '| Notes ';
}
$table .= "|\n";
// Separator
$table .= "| ---- | ---- ";
if ($includeNotes) {
$table .= '| ---- ';
}
$table .= "|\n";
// Rows
foreach ($parameters as $parameterNode) {
// Name
$table .= "| {$parameterNode->parameterName}";
// Type
$table .= "| " . formatTypeString($parameterNode) . " ";
// Notes
if (!empty($parameterNode->description) && $includeNotes) {
$table .= "| {$parameterNode->description} ";
}
$table .= "|\n";
}
return $table;
}
/**
* Does an array of parameters contain any 'note' content?
*
* @param array $parameters
* @return bool
*/
function doParametersContainNotes(array $parameters): bool
{
if (empty($parameters)) {
return false;
}
foreach ($parameters as $parameterNode) {
if (!empty($parameterNode->description)) {
return true;
}
}
return false;
}
/**
* Adjust the formatting of a parameter type string.
*
* @param ParamTagValueNode|ReturnTagValueNode $node
* @return string
*/
function formatTypeString(ParamTagValueNode|ReturnTagValueNode $node): string
{
// Convert separation pipes to commas
$type = str_replace(' | ', ',', $node->type->__toString());
// Remove parenthesis
$type = str_replace(['(', ')'], '', $type);
return "`" . $type . "`";
}