} */ function processFile(string $content, bool $restore): array { $lineChanges = []; $counter = 1; $pattern = '/\* @api \{([^\}]+)\}\s+([^\s]+)([^\r\n]*)(\r?\n)/'; $updated = preg_replace_callback( $pattern, function (array $matches) use ($restore, &$counter, &$lineChanges) { $method = trim($matches[1]); if (!in_array(strtolower($method), ['get', 'post'], true)) { return $matches[0]; } $endpoint = trim($matches[2]); $suffix = normalizeDescription(stripExistingNumbering($matches[3])); if (!$restore) { $numberedSuffix = formatNumber($counter) . '.'; if ($suffix !== '') { $numberedSuffix .= ' ' . $suffix; } $counter++; } else { $numberedSuffix = $suffix; } $newLine = renderAnnotation($method, $endpoint, $numberedSuffix); if ($newLine !== rtrim($matches[0], "\r\n")) { $lineChanges[] = $newLine; } return $newLine . $matches[4]; }, $content ); if ($updated === null) { return [$content, []]; } return [$updated, $lineChanges]; } /** * 生成格式化后的注释行 */ function renderAnnotation(string $method, string $endpoint, string $suffix = ''): string { $line = "* @api {" . $method . "} " . $endpoint; if ($suffix !== '') { if ($suffix[0] !== ' ') { $line .= ' '; } $line .= $suffix; } return $line; } /** * 移除已有编号部分 */ function stripExistingNumbering(string $text): string { $trimmed = ltrim($text); $pattern = '/^\d+\.\s*/'; return preg_replace($pattern, '', $trimmed) ?? $trimmed; } /** * 压缩多余空格 */ function normalizeDescription(string $text): string { $text = trim($text); if ($text === '') { return ''; } return preg_replace('/\s+/', ' ', $text) ?? $text; } /** * 生成固定宽度的数字 */ function formatNumber(int $number): string { return str_pad((string) $number, NUMBER_WIDTH, '0', STR_PAD_LEFT); }