pekral/cursor-rules/class-refactoring

Claude Code and agent skill by pekral/cursor-rules

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Use when refactor PHP classes to improve structure, readability,

Install

npx vskill@latest install pekral/cursor-rules/class-refactoring

SKILL.md

The same SKILL.md was published earlier as pekral/cursor-rules/class-refactoring.

Class Refactoring

Purpose

Improve code structure and quality without changing behavior.

Focus on:

  • clarity
  • separation of concerns
  • testability
  • maintainability

Modes

This skill runs in one of two modes, selected by the caller via MODE (default apply):

  • `apply` (default) — full refactoring: modify code, author the pre-refactor coverage commit, run fixers / checkers, and chain the After Completion review. Every step below behaves as written unless it is explicitly flagged for MODE=cr.
  • `cr` (read-only lens — invoked by `@skills/code-review/SKILL.md`, `code-review-github`, `code-review-jira`, `code-review-bugsnag`) — never modify code, never author tests, never stage / commit / push, never run fixers or checkers, and never chain any After Completion review. Scope the analysis to the lines added or modified by the PR diff and return the refactoring opportunities as markdown only, for the CR to fold into its Refactoring (DRY / tech debt) and Refactoring proposals sections. Every "apply / extract / split / consolidate" instruction below is emitted as a written proposal, not applied to code; the Test Coverage Gate becomes a read-only audit (report coverage gaps as findings, do not author tests).

Completeness contract (`MODE=cr`). The lens walks the complete guideline set of this skill against the diff — every bullet of Refactoring Guidelines, every bullet of Laravel Context, and the behavior-preservation clauses of the Execution section — not only the DRY / oversized-method subset the caller happens to name in its invocation line. A caller's summary of the lens ("surface DRY duplication and oversized methods") is a description of the common case, never a restriction of the walk. Each matched guideline produces one item, and the caller must render every returned item in the published report — the CR routes it, it never drops it. The routing and de-duplication contract lives in @rules/code-review/general.mdc Refactoring & Tech Debt (DRY) Analysis — diff-scoped detail.


Constraints

  • Apply @rules/refactoring/general.mdc — shared definition of refactoring, recommended incremental process, and "no big-bang rewrite" rule.
  • Apply @rules/php/core-standards.mdc
  • Apply @rules/php/dependency-selection.mdc — when the refactor proposes extracting behavior into an external Composer package or replacing a hand-rolled helper with a library, run the Activity gate + Compatibility gate from that rule before recommending the dependency. A refactor that adopts an archived / abandoned / branch-pinned package is rejected on the spot.
  • If the current project uses Laravel, also apply @rules/laravel/laravel.mdc, @rules/laravel/architecture.mdc, @rules/laravel/filament.mdc, and @rules/laravel/livewire.mdc
  • Apply @rules/code-testing/general.mdc
  • Never change behavior
  • Keep public API stable unless explicitly required

Execution

Read, Map & Verify before refactoring (mandatory pre-flight)
`MODE=cr`: perform Read and Map read-only to ground the proposals in the real code; Verify is the audit you already run. Do not modify code.

Reading, mapping, and verifying come first; refactoring comes last. This pre-flight is blocking — do not edit a single line of production code until all three steps pass, and never act on an assumption you have not confirmed by reading the code.

  1. Read — open and read the actual class being refactored and the code it depends on (callers, called methods, related tests, configuration). Confirm what the code does by reading it, not by guessing from names.
  2. Map — map the change's blast radius: every call site and caller of the touched code, the data-flow paths through it, the public API consumers, and the existing helpers / Services / Actions / layers to reuse instead of reinventing.
  3. Verify — check your assumptions against the real code and its observed behavior before deciding the highest-impact refactoring. If reading and mapping contradict the task framing, stop and surface the discrepancy instead of refactoring on a wrong premise.

Only after Read, Map, and Verify are complete may the Test Coverage Gate and the refactor proceed.

Test Coverage Gate (mandatory pre-flight — issue #493)
`MODE=cr`: do not write tests or commits. Run the coverage check read-only and report any target lines below 100% coverage as a refactoring finding (a refactor cannot land safely without them) — then continue the analysis. The steps below that author tests / commits apply to MODE=apply only.

The gate is blocking. Refactoring may not edit a single line of production code until tests for the target lines reach 100% coverage. Satisfy the Test Coverage Contract defined in @rules/refactoring/general.mdc:

  1. Verify coverage of the current code that the refactor will touch, using the project's available coverage tooling scoped to those files (per @rules/php/core-standards.mdc Testing section). Every line, branch, and condition must already be at 100%.
  2. If coverage is below 100% on the target lines, stop and write the missing tests first. Use @skills/create-test/SKILL.md to author them; commit them in a dedicated test(scope): cover <area> before refactor commit per @rules/git/general.mdc Allowed Types. The pre-refactor coverage commit and the refactor commit are always two separate commits — never squash them and never mix new tests into the refactor commit.
  3. The pre-refactor tests are the behavior-preservation contract for the refactor. Their assertions must continue to pass unchanged through the refactor commit, end to end. If a pre-existing assertion would have to change to make the refactor green, the change is no longer a refactor — it is a behavior change and must be split into a separate commit with its own justification (typically a feat(scope): … / fix(scope): … commit, not the refactor commit).
  4. Only after the coverage gate is green and the assertions are confirmed stable may the refactor proceed.
Refactoring steps
  • Analyze the class and identify the highest-impact refactoring.
  • Follow the incremental process from @rules/refactoring/general.mdc (stabilize → identify entry points → introduce Action pattern → split responsibilities → modernize → DRY → concurrency). Never propose a big-bang rewrite.
  • Fix any obvious pre-existing bugs before refactoring (separate commit).
  • Apply focused refactoring strictly per the applied rules — @rules/refactoring/general.mdc, @rules/php/core-standards.mdc, @rules/code-testing/general.mdc, and (for Laravel projects) @rules/laravel/laravel.mdc + @rules/laravel/architecture.mdc + @rules/laravel/filament.mdc + @rules/laravel/livewire.mdc. The refactor rewrites the existing code into the target architecture (Action / Service / Repository / ModelManager / Data Validator / Data Builder / DTO per project rules) and the target code-style (naming, structure, parameter count, nesting, design principles). Anything that would deviate from the rules is rewritten until it complies; do not invent ad-hoc structure outside the rule set.
  • Concrete refactoring activities:
  • simplify structure
  • reduce complexity
  • improve naming
  • extract responsibilities where needed
  • Test assertion logic must not change during the refactor. The pre-refactor coverage commit fixed the contract; the refactor commit changes structure only. Pre-existing assertions, expected return values, expected exceptions, expected persisted state, and expected emitted events stay byte-for-byte the same — they are the proof that behavior is preserved. The only allowed test edits in the refactor commit are mechanical renames forced by the refactor itself (e.g. namespace move, constructor argument order forced by an extracted DTO), and they must be flagged in the commit body. If an assertion would have to change to make the refactor green, treat that as a signal that you are no longer refactoring and split the behavior change into its own commit instead. New tests that cover newly introduced code paths belong in a separate test(scope): … commit after the refactor.
  • After the refactor — re-verify coverage stayed 100%. Once the refactor commit is in place, run the coverage tooling again scoped to the refactored files and confirm every changed line, branch, and condition is still exercised and the pre-existing assertions still pass unchanged. Coverage must remain 100% — a refactored line that is no longer covered means the refactor introduced an untested path; fix the path (it is usually dead code or a new branch), never restore the number by editing the pre-refactor tests. This is the apply-mode enforcement of step 4 of the Test Coverage Contract in @rules/refactoring/general.mdc.
  • Avoid unnecessary changes outside the scope.
  • Hand over a logically partitioned history. The coverage commit, the pre-existing bug fix, the refactor itself, and any follow-up tests are separate commits by the rules above — before publishing, verify git log actually reflects that split and reshape it when it does not (split a bundled commit, fixup a scattered one, amend a subject that no longer describes its contents, add the commit a change never got), per @rules/git/general.mdc Git Rules.
  • Prefer small, safe transformations over large rewrites.

Refactoring Guidelines

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 95 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
9
Repository forks
1
7-day trend score
12

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/pekral/cursor-rules/class-refactoring/badge)](https://verified-skill.com/skills/pekral/cursor-rules/class-refactoring)

Something wrong with this skill or its page? Tell us.