forked from response-interop/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-docs.php
More file actions
151 lines (125 loc) · 4.57 KB
/
extract-docs.php
File metadata and controls
151 lines (125 loc) · 4.57 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
<?php
// replace some text with [_Text_][]
$docs = [];
$files = [
__DIR__ . "/src/ResponseStruct.php",
__DIR__ . "/src/ResponseHeadersCollection.php",
__DIR__ . "/src/ResponseBodyHandler.php",
__DIR__ . "/src/ResponseCookieHelperService.php",
__DIR__ . "/src/ResponseSenderService.php",
__DIR__ . "/src/ResponseThrowable.php",
__DIR__ . "/src/ResponseTypeAliases.php",
];
foreach ($files as $file) {
require $file;
$interfaces = get_declared_interfaces();
$interface = end($interfaces);
$class = new ReflectionClass($interface);
$namespace = $class->getNamespaceName();
$docs[] = "### _" . stripNamespace($namespace, $class->getName()) . "_";
$docs[] = "";
addNarrative($docs, $class, '', '');
$docs[] = "";
$properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
if ($properties) {
$docs[] = "- Properties:";
$docs[] = "";
foreach ($properties as $property) {
$annotations = getAnnotations($property);
preg_match("/@var (.*)/", $annotations, $matches);
$type = stripNamespace($namespace, $matches[1] ?? $property->getType());
$name = $property->getName();
$docs[] = " - ```php";
$docs[] = " public {$type} \${$name} { get; set; }"; // reflect on get/set
$docs[] = " ```";
addNarrative($docs, $property, " ", "- ");
$docs[] = "";
}
}
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
if ($methods) {
$docs[] = "- Methods:";
$docs[] = "";
// @TODO must show default values for params
foreach ($methods as $method) {
if ($method->getDeclaringClass() != $class) {
continue;
}
$annotations = getAnnotations($method);
$signature = "public function " . $method->getName() . "(";
foreach ($method->getParameters() as $parameter) {
$name = $parameter->getName();
preg_match("/@param (.*) \\\${$name}/m", $annotations, $matches);
$type = stripNamespace($namespace, $matches[1] ?? $parameter->getType());
$signature .= "{$type} \${$name}, ";
}
$signature = rtrim($signature, ", ");
preg_match("/@return (.*)/", $annotations, $matches);
$type = stripNamespace($namespace, $matches[1] ?? $method->getReturnType()); // if null, blow up, need a return
$signature .= ") : {$type};";
if (strlen($signature) > 80) {
$signature = wrapSignature($signature);
}
$docs[] = " - ```php";
$docs[] = " {$signature}";
$docs[] = " ```";
addNarrative($docs, $method, " ", "- ");
$docs[] = "";
}
}
}
$docs = implode(PHP_EOL, $docs) . PHP_EOL;
$readme = file_get_contents(__DIR__ . '/README.tpl.md');
$readme = str_replace('{{= docs }}', $docs, $readme);
$readme = preg_replace('/^\s+$/m', '', $readme);
$readme = preg_replace('/^- Methods:\n\n###/m', "###", $readme);
file_put_contents(__DIR__ . '/README.md', $readme);
function cleanComment($r)
{
$comment = $r->getDocComment();
$comment = preg_replace("/^[ ]{0,}\/\*\*/m", "", $comment);
$comment = preg_replace("/^[ ]{0,}\*\//m", "", $comment);
$comment = preg_replace("/^[ ]{0,}\*[ ]{0,1}/m", "", $comment);
return trim($comment);
}
function getAnnotations($r)
{
$comment = cleanComment($r);
$pos = strpos($comment, PHP_EOL . "@");
if ($pos === false) {
return "";
}
return substr($comment, $pos);
}
function addNarrative(&$docs, $r, $indent, $prefix)
{
$comment = cleanComment($r);
$pos = strpos($comment, PHP_EOL . "@");
if ($pos !== false) {
$comment = substr($comment, 0, $pos);
}
$comment = $prefix . $comment;
$lines = explode(PHP_EOL, $comment);
foreach ($lines as &$line) {
$docs[] = $indent . $line;
}
}
function stripNamespace(string $namespace, string $type) : string
{
return str_replace(
$namespace . "\\",
"",
$type
);
}
function wrapSignature(string $signature) : string
{
[$params, $return] = explode(' : ', $signature);
if (strpos($signature, '()') === false) {
$params = str_replace("(", "(\n ", $params);
$params = str_replace(",", ",\n ", $params);
$params = str_replace(")", ",\n )", $params);
}
// $return = str_replace("|", "\n |", $return);
return "{$params} : {$return}";
}