-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
generate-class-reference.php
173 lines (148 loc) · 5.25 KB
/
generate-class-reference.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
<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use GraphQL\Utils\PhpDoc;
use Symfony\Component\VarExporter\Exception\ExceptionInterface;
use Symfony\Component\VarExporter\VarExporter;
const OUTPUT_FILE = __DIR__ . '/docs/class-reference.md';
const ENTRIES = [
GraphQL\GraphQL::class => [],
GraphQL\Type\Definition\Type::class => [],
GraphQL\Type\Definition\ResolveInfo::class => [],
GraphQL\Language\DirectiveLocation::class => ['constants' => true],
GraphQL\Type\SchemaConfig::class => [],
GraphQL\Type\Schema::class => [],
GraphQL\Language\Parser::class => [],
GraphQL\Language\Printer::class => [],
GraphQL\Language\Visitor::class => [],
GraphQL\Language\AST\NodeKind::class => ['constants' => true],
GraphQL\Executor\Executor::class => [],
GraphQL\Executor\ScopedContext::class => [],
GraphQL\Executor\ExecutionResult::class => [],
GraphQL\Executor\Promise\PromiseAdapter::class => [],
GraphQL\Validator\DocumentValidator::class => [],
GraphQL\Error\Error::class => ['constants' => true],
GraphQL\Error\Warning::class => ['constants' => true],
GraphQL\Error\ClientAware::class => [],
GraphQL\Error\DebugFlag::class => ['constants' => true],
GraphQL\Error\FormattedError::class => [],
GraphQL\Server\StandardServer::class => [],
GraphQL\Server\ServerConfig::class => [],
GraphQL\Server\Helper::class => [],
GraphQL\Server\OperationParams::class => [],
GraphQL\Utils\BuildSchema::class => [],
GraphQL\Utils\AST::class => [],
GraphQL\Utils\SchemaPrinter::class => [],
];
/**
* @param ReflectionClass<object> $class
* @param array{constants?: bool, props?: bool, methods?: bool} $options
*
* @throws ExceptionInterface
* @throws ReflectionException
*/
function renderClass(ReflectionClass $class, array $options): string
{
$classDocs = PhpDoc::unwrap(PhpDoc::unpad($class->getDocComment()));
$content = '';
$className = $class->getName();
if ($options['constants'] ?? false) {
$constants = [];
foreach ($class->getConstants(/* TODO enable with PHP 8: ReflectionClassConstant::IS_PUBLIC */) as $name => $value) {
$constants[] = "const {$name} = " . VarExporter::export($value) . ';';
}
if ($constants !== []) {
$constants = "```php\n" . implode("\n", $constants) . "\n```";
$content .= "### {$className} Constants\n\n{$constants}\n\n";
}
}
if ($options['props'] ?? true) {
$props = [];
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if (isApi($property)) {
$props[] = renderProp($property);
}
}
if ($props !== []) {
$props = "```php\n" . implode("\n\n", $props) . "\n```";
$content .= "### {$className} Props\n\n{$props}\n\n";
}
}
if ($options['methods'] ?? true) {
$methods = [];
foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (isApi($method)) {
$methods[] = renderMethod($method);
}
}
if ($methods !== []) {
$methods = implode("\n\n", $methods);
$content .= "### {$className} Methods\n\n{$methods}\n\n";
}
}
return <<<TEMPLATE
## {$className}
{$classDocs}
{$content}
TEMPLATE;
}
/**
* @throws ExceptionInterface
* @throws ReflectionException
*/
function renderMethod(ReflectionMethod $method): string
{
$args = array_map(
static function (ReflectionParameter $p): string {
$type = ltrim($p->getType() . ' ');
$def = $type . '$' . $p->getName();
if ($p->isDefaultValueAvailable()) {
$val = $p->isDefaultValueConstant()
? $p->getDefaultValueConstantName()
: $p->getDefaultValue();
$def .= ' = ' . VarExporter::export($val);
}
return $def;
},
$method->getParameters()
);
$argsStr = implode(', ', $args);
if (strlen($argsStr) >= 80) {
$argsStr = "\n " . implode(",\n ", $args) . "\n";
}
$returnType = $method->getReturnType();
$def = "function {$method->getName()}({$argsStr})";
$def = $method->isStatic()
? "static {$def}"
: $def;
$def = $returnType instanceof ReflectionType
? "{$def}: {$returnType}"
: $def;
$docBlock = PhpDoc::unpad($method->getDocComment());
return <<<TEMPLATE
```php
{$docBlock}
{$def}
```
TEMPLATE;
}
function renderProp(ReflectionProperty $prop): string
{
$signature = implode(' ', Reflection::getModifierNames($prop->getModifiers())) . ' $' . $prop->getName() . ';';
return PhpDoc::unpad($prop->getDocComment()) . "\n" . $signature;
}
/**
* @param ReflectionProperty|ReflectionMethod $reflector
*/
function isApi(Reflector $reflector): bool
{
$comment = $reflector->getDocComment();
if ($comment === false) {
return false;
}
return preg_match('~[\r\n ]+\* @api~', $comment) === 1;
}
file_put_contents(OUTPUT_FILE, '');
foreach (ENTRIES as $className => $options) {
$rendered = renderClass(new ReflectionClass($className), $options);
file_put_contents(OUTPUT_FILE, $rendered, FILE_APPEND);
}