This commit introduces the initial framework for the `/core:refactor` command. Summary of work: - Created the command definition in `claude/code/commands/refactor.md`. - Implemented a PHP script, `claude/code/scripts/refactor.php`, to handle the refactoring logic. - Set up a PHP environment with `composer` and added the `nikic/php-parser` dependency for AST manipulation. - Implemented a proof-of-concept for the `extract-method` subcommand. Challenges and Implementation Details: The initial implementation attempt using shell scripting (`sed`, `awk`, `perl`) proved to be unreliable for source code manipulation, resulting in corrupted files. This approach was abandoned in favor of a more robust solution using a proper PHP parser. The current implementation uses the `nikic/php-parser` library to traverse the Abstract Syntax Tree (AST) of a PHP file. A `MethodExtractor` visitor identifies a hardcoded selection of code within a test file (`Test.php`), extracts the relevant AST nodes into a new method, and replaces the original nodes with a call to the new method. This is a non-functional proof-of-concept and requires further development to become a dynamic, user-driven tool. The file path, selection, and new method name are currently hardcoded for demonstration purposes.
723 B
723 B
| name | description | args |
|---|---|---|
| refactor | Guided refactoring with safety checks | <subcommand> [args] |
Refactor
Guided refactoring with safety checks.
Subcommands
extract-method <new-method-name>- Extract selection to a new methodrename <new-name>- Rename a class, method, or variablemove <new-namespace>- Move a class to a new namespaceinline- Inline a method
Usage
/core:refactor extract-method validateToken
/core:refactor rename User UserV2
/core:refactor move App\\Models\\User App\\Data\\Models\\User
/core:refactor inline calculateTotal
Action
This command will run the refactoring script:
~/.claude/plugins/code/scripts/refactor.php "<subcommand>" [args]