assets/skills/refactor/SKILL.md
DDD refactoring workflow with phase-based checks and review loop. Refactor existing code to DDD architecture or improve existing DDD structure. Use when user says "refactor", "clean up", "improve code", "restructure", "migrate to ddd", "refactor ddd".
npx skillsauth add phuthuycoding/moicle refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Refactor existing code into DDD architecture, or improve existing DDD structure. Execute phases sequentially, run rule checks after each phase.
ARGUMENTS: <module> <domain> — e.g., marketing notification, users identity, products catalog
Before starting, MUST read TWO files:
.claude/architecture/ddd-architecture.md| File | Stack Doc |
|------|-----------|
| go.mod | go-backend.md |
| package.json + vite.config.* | react-frontend.md |
| pubspec.yaml | flutter-mobile.md |
| composer.json | laravel-backend.md |
| remix.config.* | remix-fullstack.md |
.claude/architecture/{name}.md # Project-specific (priority)
~/.claude/architecture/{name}.md # Global
PHASE 0: Foundation Check
→ PHASE 1: Analyze Current Module
→ PHASE 2: Build Domain Layer
→ PHASE 3: Build Infrastructure Layer
→ PHASE 4: Build Application Layer
→ PHASE 5: Domain Tests
→ PHASE 6: Integration & Cleanup
→ REVIEW LOOP (run /architect-review, fix, repeat until clean)
Each phase has a Rule Check. DO NOT skip any phase.
Verify DDD foundation exists in the project.
Create shared foundation:
domain/shared/ — base event types, event collector, event dispatcher interfaceVerify foundation is intact:
# Check shared domain types exist
ls {domain_root}/shared/ 2>/dev/null && echo "PASS" || echo "NEED SETUP"
# Check event infrastructure exists (if applicable)
ls {eventbus_path}/ 2>/dev/null && echo "PASS" || echo "NEED SETUP"
If any FAIL → create foundation first before proceeding.
Read ALL source files before making any changes.
Create domain/{domain}/ (if first module in this domain) or add to existing.
valueobjects/)entities/)events/)ports/)usecases/)ports/Run check scripts from architecture doc:
# Build domain layer
{stack_build_command_for_domain}
# Check domain purity
{grep_forbidden_imports_in_domain} && echo "FAIL" || echo "PASS"
# Check no cross-domain imports
{check_cross_domain_imports}
domain/{domain}/ports/# Build infrastructure
{stack_build_command_for_infra}
# Build application layer
{stack_build_command_for_application}
CRITICAL: MUST read old tests before writing new ones. Copy all test cases and business scenarios to domain tests. Do not lose any test coverage.
# Run domain tests
{stack_test_command_for_domain}
# Full build
{stack_full_build_command}
# Verify old module removed
test -d {old_module_path} && echo "FAIL: old module exists" || echo "PASS"
# Verify no old imports remain
grep -r "{old_module_import}" --include="*.{ext}" . && echo "FAIL: old imports" || echo "PASS"
After all phases complete, run the full architecture review. Keep looping until ALL checks pass.
LOOP:
1. Run /architect-review {stack} {domain}
2. Collect violations
3. IF violations with severity >= MEDIUM:
a. Fix all violations
b. Run full build to verify
c. Run all tests to verify
d. GOTO 1
4. IF score >= B:
BREAK → Final Report
When review loop passes with score >= B:
## Refactor Complete: {module} → {domain}
### Files Created
- List all new files
### Files Modified
- List all modified files
### Files Deleted
- List old module files removed
### Endpoints/Screens Preserved
| Before | After | Status |
|--------|-------|--------|
| All old routes | Same routes, new handlers | Verified |
### Domain Events
| Event | Listeners |
|-------|-----------|
### Test Coverage
- X test files, Y test functions
- All old test cases migrated: YES
- Areas covered: value objects, entities, usecases
### Review Status: ALL CHECKS PASS
- Build: PASS
- Lint: PASS
- Domain purity: PASS
- Old module removed: PASS
- No old imports: PASS
- Tests: PASS (X/X)
- Architecture score: {A/B}
| Phase | Agent | Purpose |
|-------|-------|---------|
| ANALYZE | @refactor | Identify refactoring opportunities |
| ANALYZE | @code-reviewer | Code smell detection |
| PLAN | @clean-architect | Architecture alignment |
| REFACTOR | Stack-specific dev agent | Code per architecture |
| TEST | @test-writer | Write domain tests |
| REVIEW | @code-reviewer | Final quality check |
development
Test-Driven Development workflow. Use when doing TDD, writing tests first, or when user says "tdd", "test first", "test driven", "red green refactor".
development
Thorough pull request review workflow with architecture compliance checks. Use when reviewing pull requests, checking code changes, or when user says "review pr", "check pr", "review code", "pr review", "review pull request".
development
Review local branch changes for architecture compliance, conventions, and code quality before pushing/PR. Stack-aware — detects the project stack and applies the matching rules. Use when user says "review changes", "review branch", "check branch", "check changes", "review my code", "review before pr".
testing
DDD architecture compliance review with automated checks and review loop. Use when user says "architect-review", "architecture review", "review architecture", "check architecture", "review ddd", "ddd review".