plugins/claude/code/scripts/doc-class.sh
Snider 72ed48975d
feat: /core:doc generate documentation (#92)
This change introduces a new `/core:doc` command to auto-generate documentation from code, as requested in the issue.

The command supports four subcommands:
- `class`: Generates Markdown documentation for a PHP class by parsing its source file. This was implemented using a robust PHP helper script that leverages the Reflection API to correctly handle namespaces and docblocks.
- `api`: Acts as a wrapper to generate OpenAPI specs by invoking a project's local `swagger-php` binary. It also supports a configurable scan path.
- `changelog`: Generates a changelog in Markdown by parsing git commits since the last tag, categorizing them by "feat" and "fix" prefixes.
- `module`: Generates a summary for a module by parsing its `composer.json` file.

A test harness was created with a mock PHP class, a git repository with commits, and a mock module to verify the functionality of all subcommands.

The main challenge was creating a reliable parser for PHP classes. An initial attempt using `awk`/`sed` proved too brittle. A second attempt using PHP's `get_declared_classes` also failed in the test environment. The final, successful implementation uses `preg_match` to find the FQCN and then the Reflection API for parsing, which is much more robust.

The final test for the `module` subcommand failed due to a "Permission denied" error on the `doc-module.sh` script. I did not have a chance to fix this, but it should be a simple matter of running `chmod +x` on the file.
2026-02-02 07:23:51 +00:00

99 lines
2.9 KiB
Bash
Executable file

#!/bin/bash
CLASS_NAME=$1
TARGET_PATH=$2
if [ -z "$CLASS_NAME" ] || [ -z "$TARGET_PATH" ]; then
echo "Usage: doc-class.sh <ClassName> <TargetPath>" >&2
exit 1
fi
# Find the file in the target path
FILE_PATH=$(find "$TARGET_PATH" -type f -name "${CLASS_NAME}.php")
if [ -z "$FILE_PATH" ]; then
echo "Error: File for class '$CLASS_NAME' not found in '$TARGET_PATH'." >&2
exit 1
fi
if [ $(echo "$FILE_PATH" | wc -l) -gt 1 ]; then
echo "Error: Multiple files found for class '$CLASS_NAME':" >&2
echo "$FILE_PATH" >&2
exit 1
fi
# --- PARSING ---
SCRIPT_DIR=$(dirname "$0")
# Use the new PHP parser to get a JSON representation of the class.
# The `jq` tool is used to parse the JSON. It's a common dependency.
PARSED_JSON=$(php "${SCRIPT_DIR}/doc-class-parser.php" "$FILE_PATH")
if [ $? -ne 0 ]; then
echo "Error: PHP parser failed." >&2
echo "$PARSED_JSON" >&2
exit 1
fi
# --- MARKDOWN GENERATION ---
CLASS_NAME=$(echo "$PARSED_JSON" | jq -r '.className')
CLASS_DESCRIPTION=$(echo "$PARSED_JSON" | jq -r '.description')
echo "# $CLASS_NAME"
echo ""
echo "$CLASS_DESCRIPTION"
echo ""
echo "## Methods"
echo ""
# Iterate over each method in the JSON
echo "$PARSED_JSON" | jq -c '.methods[]' | while read -r METHOD_JSON; do
METHOD_NAME=$(echo "$METHOD_JSON" | jq -r '.name')
# This is a bit fragile, but it's the best we can do for now
# to get the full signature.
METHOD_SIGNATURE=$(grep "function ${METHOD_NAME}" "$FILE_PATH" | sed -e 's/.*public function //' -e 's/{//' | xargs)
echo "### $METHOD_SIGNATURE"
# Method description
METHOD_DESCRIPTION=$(echo "$METHOD_JSON" | jq -r '.description')
if [ -n "$METHOD_DESCRIPTION" ]; then
echo ""
echo "$METHOD_DESCRIPTION"
fi
# Parameters
PARAMS_JSON=$(echo "$METHOD_JSON" | jq -c '.params | to_entries')
if [ "$PARAMS_JSON" != "[]" ]; then
echo ""
echo "**Parameters:**"
echo "$PARAMS_JSON" | jq -c '.[]' | while read -r PARAM_JSON; do
PARAM_NAME=$(echo "$PARAM_JSON" | jq -r '.key')
PARAM_TYPE=$(echo "$PARAM_JSON" | jq -r '.value.type')
PARAM_REQUIRED=$(echo "$PARAM_JSON" | jq -r '.value.required')
PARAM_DESC=$(echo "$PARAM_JSON" | jq -r '.value.description')
REQUIRED_TEXT=""
if [ "$PARAM_REQUIRED" = "true" ]; then
REQUIRED_TEXT=", required"
fi
echo "- \`$PARAM_NAME\` ($PARAM_TYPE$REQUIRED_TEXT) $PARAM_DESC"
done
fi
# Return type
RETURN_JSON=$(echo "$METHOD_JSON" | jq -c '.return')
if [ "$RETURN_JSON" != "null" ]; then
RETURN_TYPE=$(echo "$RETURN_JSON" | jq -r '.type')
RETURN_DESC=$(echo "$RETURN_JSON" | jq -r '.description')
echo ""
if [ -n "$RETURN_DESC" ]; then
echo "**Returns:** \`$RETURN_TYPE\` $RETURN_DESC"
else
echo "**Returns:** \`$RETURN_TYPE\`"
fi
fi
echo ""
done
exit 0