/goaop:aspect-oriented-refactoringfrom goaop
Refactors existing PHP code into aspects using goaop/framework (Go! AOP). Identifies scattered or duplicated cross-cutting concerns (logging, transactions, caching, retries) and extracts them into aspects without changing behavior.
Ships in goaop. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This skill turns code that already exists into aspects. The business methods keep what they are about, and the code that repeated around it moves into one place. It is a refactoring: the observable behaviour stays the same, and each step is small enough to verify.
It builds on the aspect-oriented-development skill of the same plugin, which describes the framework itself:
pointcut syntax, advice types, joinpoints, registration of aspects, framework bridges, troubleshooting. Load that
skill when a step here needs those details, and make sure the framework is installed and its kernel runs before you
extract anything (its "Step 0").
try/catch in dozens of methods. Changing the concern means editing all of them.They come together, and an aspect removes both. A concern that is neither scattered nor tangled is not a candidate.
Work through the steps in order. Steps 1 to 4 change nothing and end with a proposal to the user; do not start editing before they agreed to it.
A refactoring is only safe when a change of behaviour would be noticed. Run the framework in debug mode while you refactor: in production mode woven classes come from a cache, and your edits would have no effect until it is rebuilt.
Run the test suite and note the result. Where the methods you are going to touch have no tests, write characterization tests first, or at least a script that calls them and records everything observable: return values, exceptions, log records, queries, cache entries. The extracted code is mostly side effects, so a test that only checks return values proves little.
Search the source directories for code that repeats across methods, with whatever search and analysis tools you have. Leave the aspect classes of the project out: their advices repeat joinpoint calls by design. For every hit note the file, the method and where in the method it sits.
Look for three kinds of evidence:
exec() call
with a different query each time) only look alike and are business code.beginTransaction, commit, rollBack;
isGranted; microtime. Searching for them finds the occurrences that are written differently in each place, and
gives the list of files. references/catalog.md has the search terms per concern. Always search for what the user
named, even when nothing else pointed at it: a concern can live in two methods only, or in one, such as a single
retry loop.catch handlers, a try/finally around a whole
method body, a loop that contains a try, the same first or last statement in many methods.Then read the methods that came up most often from top to bottom. Tangling shows there: how many of their lines belong to the business logic, and how many to the candidates.
A list of hits does not answer two questions, so answer them by reading:
beginTransaction() and the catch with rollBack() in the same methods are two
halves of one concern. A permission check in those same methods is another concern that happens to guard them.Open the reported places and read them. For each candidate decide:
| Question | If not |
|---|---|
| Is it the same concern in every place, not just similar text? | Drop it, or split the group |
| Is it independent of the business logic around it? Would the method still be correct, only less observable, guarded or fast, without it? | It is business logic: leave it, or use Extract Method |
| Does it sit at a method boundary: before the work, after it, or around it? | "Extract an advice" has a preparatory step for fragments in the middle |
| Does it only need what a joinpoint offers: the arguments, the object, the result, the exception? | See "What an advice can reach" in references/extract-advice.md |
| Is it in three or more places, or certain to spread? | For one or two places an aspect costs more than it saves. Say so, but extract it when the user asked for that concern |
Then name the concern and choose the advice type from where the code sits:
| Where the repeated code sits | Advice |
|---|---|
| Only before the work, needs arguments or the object | #[Before] |
| Only before the work, and prevents it by throwing (guard, permission check) | #[Before] |
In a finally, or after the work whatever the outcome | #[After] |
In a catch that reports and rethrows | #[AfterThrowing] |
| Before and after with shared state (start time, transaction, lock) | #[Around] |
| Uses or replaces the result (cache, conversion) | #[Around] |
| Catches and recovers, retries, or skips the work | #[Around] |
Prefer the weakest type that does the job: a #[Before] advice cannot forget to call proceed().
references/catalog.md lists the usual concerns with their code shapes, advice types and pitfalls.
Tell the user what you found before changing code: the concerns, where each one occurs (count and files), the aspect and marker attribute you would create for each, the advice type, what stays in the methods, and anything that would change behaviour (see "Keep the quirks" below). Recommend an order of extraction, as described in step 5.
Use the two refactorings. Each has its mechanics in a reference file; follow them step by step.
references/extract-aspect.md.references/extract-advice.md.Order. Among concerns that never meet on a method, start with the most scattered and least risky one, usually
logging or timing. Where several concerns share methods, their order is given by the code: go from the outside in,
and extract a guard (code that throws before the work starts) before any concern that reacts to exceptions. Otherwise
the intermediate state differs from the original: an #[AfterThrowing] advice for failures would report the
exception of a guard that is still in the method body, which the original try never saw. When such an intermediate
difference cannot be avoided, predict it before the step and check that the diff is exactly that.
After every concern: run the tests or the characterization script, compare with step 1, and commit or otherwise checkpoint. Never extract two concerns in one step: when the output differs, you want one suspect.
Repeat the scan of step 2: the extracted lines are gone from the lists, and what remains is either the next concern or business code. Remove what the classes no longer need (see below), and report to the user what moved where.
An aspect acts at a distance: the method no longer shows what happens around it. The remedy is a marker attribute on the method and an attribute pointcut in the aspect.
#[Transactional]
#[RequiresRole('ROLE_ADMIN')]
public function cancel(int $orderId): void
#[Around('@execution(App\Attribute\Transactional)')]
execution(public App\Service\*->save*(*)) fail in both ways; this is known as the fragile
pointcut problem.Use a name or namespace pointcut only when the rule really is "everything here, including future code", for example an audit log for every public method of a repository layer, and say so in the proposal.
Name the attribute after what it promises, not after how the aspect works: #[Transactional], #[Cacheable],
#[Retry], #[RequiresRole], #[Loggable], #[Timed]. Put attributes in their own namespace (App\Attribute),
aspects in theirs (App\Aspect), one aspect per concern.
#[Before] advices run first, then the #[Around] advices nest, each kind sorted by
order (lower first, and outermost for #[Around]). Set order explicitly wherever two extracted concerns meet
on one method, and write down why.null, one forgets the rollback. Decide per difference: it is a setting (attribute argument), a bug (ask the
user), or a sign that the place is a different concern (leave it out). Do not silently unify. Extending a concern
to methods that did not have it is a behaviour change too.__METHOD__ becomes $method->class . '::' . $method->name from
$invocation->getMethod(); func_get_args() becomes $invocation->getArguments(); $this becomes
$invocation->getThis(), with access to its public members only.#[Before] advice skip the other advices, including #[After] and
#[AfterThrowing]. A guard that moved into a #[Before] advice is therefore not reported by an
#[AfterThrowing] advice. Check that this matches the original order of guard and try.$this are intercepted.use statements the class needed only for
the extracted concern, and update the places that construct the class (container configuration, factories, tests).
PHP ignores surplus arguments of a new call without any error, and a removed parameter in the middle shifts the
following ones: find every construction, do not wait for a failure. A service that loses its logger and its
security dependency is the visible benefit of the refactoring.| Read | When |
|---|---|
references/extract-aspect.md | Performing "Extract an aspect": mechanics, a complete before/after example, checklist |
references/extract-advice.md | Performing "Extract an advice": fragment position to advice type, what an advice can reach, fragments in the middle of a method, fragments that cannot be extracted |
references/catalog.md | Recognizing a concern: code shapes, advice type, attribute, pitfalls; related refactorings (introductions, replacing decorators and base classes); lessons from AspectJ and Spring |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub goaop/framework --plugin goaop2 skills in this plugin
Other plugins' skills for the same job