-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize_docblocks.php
More file actions
170 lines (147 loc) · 4.79 KB
/
Copy pathnormalize_docblocks.php
File metadata and controls
170 lines (147 loc) · 4.79 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
<?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/normalize_docblocks.php <file1.php> [file2.php ...]\n");
exit(1);
}
foreach (array_slice($argv, 1) as $file) {
if (!is_file($file)) {
continue;
}
$content = file_get_contents($file);
if ($content === false) {
continue;
}
$new = $content;
// Move generated docblocks before modifiers/signature.
$new = preg_replace(
'/((?:(?:public|protected|private|static|final|abstract)\s+)+)(\/\*\*\R\s*\* Kurzbeschreibung Funktion[\s\S]*?\*\/\R)\s*function/s',
'$2$1function',
$new
);
// If a manual docblock already exists, drop the generated generic one.
$new = preg_replace(
'/(\/\*\*(?:(?!\*\/)[\s\S])*?\*\/\R\s*)(\/\*\*\R\s*\* Kurzbeschreibung Funktion[\s\S]*?\*\/\R)\s*((?:(?:public|protected|private|static|final|abstract)\s+)*function)/s',
'$1$3',
$new
);
// Collapse excessive spaces between visibility/modifier tokens and function.
$new = preg_replace(
'/\b(public|protected|private|static|final|abstract)\h+(?=(?:public|protected|private|static|final|abstract|function)\b)/',
'$1 ',
$new
);
// Re-indent docblocks directly above function signatures to match signature indentation.
$new = normalizeFunctionDocblocks($new);
if ($new !== $content) {
file_put_contents($file, $new);
}
}
/**
* Normalize docblock line prefixes and set a specific base indentation.
*
* @param string $doc Raw docblock content including trailing newline.
* @param string $indent Base indentation that should be used.
* @return string Normalized docblock.
*/
/**
* Normalisiert die Einrückung eines DocBlocks auf einen festgelegten Basisindent.
*
* @param string $doc Inhalt des DocBlocks inklusive Zeilenumbrüchen.
* @param string $indent Basis-Einrückung.
* @return string Normalisierter DocBlock.
*/
function normalizeDocblockIndentation(string $doc, string $indent): string
{
$trimmed = rtrim($doc, "\r\n");
$lines = preg_split('/\R/', $trimmed) ?: [];
if ($lines === []) {
return $doc;
}
$out = [];
$lastIndex = count($lines) - 1;
foreach ($lines as $i => $line) {
if ($i === 0) {
$out[] = $indent . '/**';
continue;
}
if ($i === $lastIndex) {
$out[] = $indent . ' */';
continue;
}
$t = ltrim($line);
if ($t === '') {
$out[] = $indent . ' *';
continue;
}
if ($t[0] === '*') {
$out[] = $indent . ' ' . $t;
continue;
}
$out[] = $indent . ' * ' . $t;
}
return implode("\n", $out) . "\n";
}
/**
* Normalize function docblocks line-based to avoid cross-block regex corruption.
*
* @param string $content Full PHP file content.
* @return string Updated file content.
*/
/**
* Normalisiert DocBlocks direkt vor Funktionssignaturen im Inhalt einer Datei.
*
* @param string $content Voller Inhalt einer PHP-Datei.
* @return string Aktualisierter Dateiinhalt.
*/
function normalizeFunctionDocblocks(string $content): string
{
$lines = preg_split('/\R/', $content) ?: [];
if ($lines === []) {
return $content;
}
for ($i = 0; $i < count($lines); $i++) {
if (!preg_match('/^(?P<indent>[\t ]*)(?:(?:public|protected|private|static|final|abstract)\s+)*function\b/', $lines[$i], $m)) {
continue;
}
$indent = $m['indent'];
$end = $i - 1;
while ($end >= 0 && trim($lines[$end]) === '') {
$end--;
}
if ($end < 0 || trim($lines[$end]) !== '*/') {
continue;
}
$start = $end - 1;
$scan = 0;
while ($start >= 0 && $scan < 80 && trim($lines[$start]) !== '/**') {
$start--;
$scan++;
}
if ($start < 0 || trim($lines[$start]) !== '/**') {
continue;
}
$rawDoc = implode("\n", array_slice($lines, $start, $end - $start + 1)) . "\n";
$normalized = normalizeDocblockIndentation($rawDoc, $indent);
$replacement = preg_split('/\R/', rtrim($normalized, "\r\n")) ?: [];
array_splice($lines, $start, $end - $start + 1, $replacement);
$i = $start + count($replacement);
}
return implode("\n", $lines);
}