-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_function_docblocks.php
More file actions
324 lines (287 loc) · 8.32 KB
/
Copy pathapply_function_docblocks.php
File metadata and controls
324 lines (287 loc) · 8.32 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* this File is part of Heimdall - (c) 2026 Heimdall
*
* NOTICE OF LICENSE
*
* OPEN-SOURCE-LIZENZ FÜR DIE EUROPÄISCHE UNION v. 1.2
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://eupl.eu/1.2/de/
*
* @author Wutze
* @copyright 2026 Heimdall
* @link https://github.com/Wutze/heimdall
* @see Internal Documentation ~/doc/
* @package Heimdall
*/
declare(strict_types=1);
if ($argc < 2) {
fwrite(STDERR, "Usage: php doc/apply_function_docblocks.php <file1.php> [file2.php ...]\n");
exit(1);
}
foreach (array_slice($argv, 1) as $file) {
if (!is_file($file)) {
continue;
}
$code = file_get_contents($file);
if ($code === false) {
continue;
}
$lines = preg_split("/\r\n|\n|\r/", $code) ?: [];
$tokens = token_get_all($code);
$out = '';
$count = count($tokens);
for ($i = 0; $i < $count; $i++) {
$tok = $tokens[$i];
if (!is_array($tok) || $tok[0] !== T_FUNCTION) {
$out .= is_array($tok) ? $tok[1] : $tok;
continue;
}
if (hasDocComment($tokens, $i)) {
$out .= $tok[1];
continue;
}
if (isAnonymousFunction($tokens, $i)) {
$out .= $tok[1];
continue;
}
$name = getFunctionName($tokens, $i) ?? 'funktion';
$params = getParameterVariables($tokens, $i);
$returnType = getReturnType($tokens, $i) ?? 'mixed|null';
$lineNo = $tok[2] ?? 1;
$indent = getIndentation($lines, $lineNo);
$doc = buildDocblock($indent, $name, $params, $returnType);
$out .= $doc;
$out .= $tok[1];
}
if ($out !== $code) {
file_put_contents($file, $out);
}
}
/**
* Prüft, ob eine Funktion bereits einen DocBlock hat.
*
* @param array<int, mixed> $tokens Tokenliste des PHP-Codes.
* @param int $index Position des Funktionstokens.
* @return bool True, wenn ein DocComment direkt vor der Funktion gefunden wurde.
*/
function hasDocComment(array $tokens, int $index): bool
{
for ($i = $index - 1; $i >= 0; $i--) {
$tok = $tokens[$i];
if (is_array($tok) && in_array($tok[0], [T_WHITESPACE, T_COMMENT], true)) {
continue;
}
if (is_array($tok) && in_array($tok[0], [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_STATIC, T_FINAL, T_ABSTRACT], true)) {
continue;
}
if (is_array($tok) && $tok[0] === T_ATTRIBUTE) {
continue;
}
if ($tok === ']') {
continue;
}
if (is_array($tok) && $tok[0] === T_DOC_COMMENT) {
return true;
}
return false;
}
return false;
}
/**
* Prüft, ob es sich bei der Funktion um eine anonyme Funktion handelt.
*
* @param array<int, mixed> $tokens Tokenliste des PHP-Codes.
* @param int $index Position des Funktionstokens.
* @return bool True, wenn die Funktion anonym ist.
*/
function isAnonymousFunction(array $tokens, int $index): bool
{
for ($i = $index + 1, $max = count($tokens); $i < $max; $i++) {
$tok = $tokens[$i];
if (is_array($tok) && $tok[0] === T_WHITESPACE) {
continue;
}
if (is_array($tok) && $tok[0] === T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG) {
continue;
}
if ($tok === '&') {
continue;
}
if ($tok === '(') {
return true;
}
return !(is_array($tok) && $tok[0] === T_STRING);
}
return false;
}
/**
* Extrahiert den Namen einer Funktion aus den Tokens.
*
* @param array<int, mixed> $tokens Tokenliste des PHP-Codes.
* @param int $index Position des Funktionstokens.
* @return string|null Funktionsname oder null, wenn keiner ermittelt werden konnte.
*/
function getFunctionName(array $tokens, int $index): ?string
{
for ($i = $index + 1, $max = count($tokens); $i < $max; $i++) {
$tok = $tokens[$i];
if (is_array($tok) && in_array($tok[0], [T_WHITESPACE, T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG], true)) {
continue;
}
if ($tok === '&') {
continue;
}
if (is_array($tok) && $tok[0] === T_STRING) {
return $tok[1];
}
return null;
}
return null;
}
/**
* Extrahiert die Variablennamen der Funktionsparameter.
*
* @param array<int, mixed> $tokens Tokenliste des PHP-Codes.
* @param int $index Position des Funktionstokens.
* @return array<int, string> Liste der Parameternamen.
*/
function getParameterVariables(array $tokens, int $index): array
{
$params = [];
$inParams = false;
$depth = 0;
for ($i = $index + 1, $max = count($tokens); $i < $max; $i++) {
$tok = $tokens[$i];
$text = is_array($tok) ? $tok[1] : $tok;
if (!$inParams) {
if ($text === '(') {
$inParams = true;
$depth = 1;
}
continue;
}
if ($text === '(') {
$depth++;
continue;
}
if ($text === ')') {
$depth--;
if ($depth === 0) {
break;
}
continue;
}
if ($depth === 1 && is_array($tok) && $tok[0] === T_VARIABLE) {
$name = $tok[1];
if (!in_array($name, $params, true)) {
$params[] = $name;
}
}
}
return $params;
}
/**
* Extrahiert den Rückgabetyp einer Funktion aus der Signatur.
*
* @param array<int, mixed> $tokens Tokenliste des PHP-Codes.
* @param int $index Position des Funktionstokens.
* @return string|null Rückgabetyp oder null, wenn kein Typ angegeben ist.
*/
function getReturnType(array $tokens, int $index): ?string
{
$seenParams = false;
$depth = 0;
for ($i = $index + 1, $max = count($tokens); $i < $max; $i++) {
$tok = $tokens[$i];
$text = is_array($tok) ? $tok[1] : $tok;
if (!$seenParams) {
if ($text === '(') {
$seenParams = true;
$depth = 1;
}
continue;
}
if ($text === '(') {
$depth++;
continue;
}
if ($text === ')') {
$depth--;
if ($depth === 0) {
break;
}
continue;
}
}
if (!$seenParams) {
return null;
}
$i++;
while ($i < $max) {
$tok = $tokens[$i];
if (is_array($tok) && in_array($tok[0], [T_WHITESPACE, T_COMMENT], true)) {
$i++;
continue;
}
break;
}
if ($i >= $max || (is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]) !== ':') {
return null;
}
$i++;
$parts = '';
while ($i < $max) {
$tok = $tokens[$i];
$text = is_array($tok) ? $tok[1] : $tok;
if ($text === '{' || $text === ';') {
break;
}
if (is_array($tok) && in_array($tok[0], [T_WHITESPACE, T_COMMENT], true)) {
$i++;
continue;
}
$parts .= $text;
$i++;
}
$parts = trim($parts);
return $parts !== '' ? $parts : null;
}
/**
* Ermittelt die Einrückung einer Zeile anhand der Zeilennummer.
*
* @param array<int, string> $lines Liste der Quellzeilen.
* @param int $lineNo Zeilennummer im Originalcode.
* @return string Einrückungszeichenfolge.
*/
function getIndentation(array $lines, int $lineNo): string
{
$idx = max(0, $lineNo - 1);
$line = $lines[$idx] ?? '';
if (preg_match('/^(\s*)/', $line, $m) === 1) {
return $m[1];
}
return '';
}
/**
* Erstellt einen generischen DocBlock für eine Funktion.
*
* @param string $indent Einrückung der Funktion.
* @param string $name Funktionsname.
* @param array<int, string> $params Liste der Parameternamen.
* @param string $returnType Rückgabetyp der Funktion.
* @return string Generierter DocBlock als String.
*/
function buildDocblock(string $indent, string $name, array $params, string $returnType): string
{
$doc = $indent . "/**\n";
$doc .= $indent . " * Kurzbeschreibung Funktion {$name}\n";
$doc .= $indent . " *\n";
foreach ($params as $param) {
$doc .= $indent . " * @param mixed {$param}\n";
}
$doc .= $indent . " * @return {$returnType}\n";
$doc .= $indent . " */\n";
return $doc;
}