From acc
Generates Visitor pattern for PHP 8.4 to perform operations on object structures without modifying element classes. Includes visitor interface, concrete visitors, visitable elements, and unit tests.
How this skill is triggered — by the user, by Claude, or both
Slash command
/acc:create-visitorThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Creates Visitor pattern infrastructure for operations on object structures without modifying classes.
Creates Visitor pattern infrastructure for operations on object structures without modifying classes.
| Scenario | Example |
|---|---|
| Operations on object structure | Calculate price/tax on order items |
| Adding operations without modification | Export visitors (JSON, XML, CSV) |
| Different operations on same elements | Validation, transformation, rendering |
| Double dispatch needed | Type-specific behavior without instanceof |
Path: src/Domain/{BoundedContext}/Visitor/
{Name}VisitorInterface.php — Visitor contract with visit methodsPath: src/Domain/{BoundedContext}/Visitor/ or src/Application/{BoundedContext}/
{Operation1}Visitor.php — First operation implementation{Operation2}Visitor.php — Second operation implementation{Operation3}Visitor.php — Third operation implementationPath: src/Domain/{BoundedContext}/
VisitableInterface.php — Element contract with accept() methodPath: src/Domain/{BoundedContext}/
implements VisitableInterface to element classesaccept() method to each element{Operation}VisitorTest.php — Individual visitor tests{Element}AcceptTest.php — Element accept() tests| Component | Path |
|---|---|
| Visitor Interface | src/Domain/{BoundedContext}/Visitor/ |
| Concrete Visitors (Domain) | src/Domain/{BoundedContext}/Visitor/ |
| Concrete Visitors (Application) | src/Application/{BoundedContext}/Visitor/ |
| Visitable Interface | src/Domain/{BoundedContext}/ |
| Unit Tests | tests/Unit/Domain/{BoundedContext}/Visitor/ |
| Component | Pattern | Example |
|---|---|---|
| Visitor Interface | {Name}VisitorInterface | OrderItemVisitorInterface |
| Concrete Visitor | {Operation}Visitor | PriceCalculatorVisitor |
| Visitable Interface | VisitableInterface | VisitableInterface |
| Visit Method | visit{ElementType}() | visitProduct() |
| Accept Method | accept() | accept() |
| Test | {ClassName}Test | PriceCalculatorVisitorTest |
interface {Name}VisitorInterface
{
public function visit{Element1}({Element1} $element): {ReturnType};
public function visit{Element2}({Element2} $element): {ReturnType};
public function visit{Element3}({Element3} $element): {ReturnType};
}
final class {Operation}Visitor implements {Name}VisitorInterface
{
public function visit{Element1}({Element1} $element): {ReturnType}
{
// Element1-specific operation
}
public function visit{Element2}({Element2} $element): {ReturnType}
{
// Element2-specific operation
}
}
interface VisitableInterface
{
public function accept({Name}VisitorInterface $visitor): mixed;
}
final readonly class {Element} implements VisitableInterface
{
public function accept({Name}VisitorInterface $visitor): mixed
{
return $visitor->visit{Element}($this);
}
}
// Create elements
$order = new Order(items: [
new Product(price: 100, quantity: 2),
new Service(price: 50, duration: 1),
new Discount(amount: 20),
]);
// Apply different visitors
$priceVisitor = new PriceCalculatorVisitor();
$taxVisitor = new TaxCalculatorVisitor(rate: 0.2);
$exportVisitor = new JsonExportVisitor();
$totalPrice = $order->accept($priceVisitor);
$totalTax = $order->accept($taxVisitor);
$json = $order->accept($exportVisitor);
| Domain | Visitors |
|---|---|
| Order Items | PriceCalculator, TaxCalculator, DiscountApplier |
| AST/Expression Tree | Evaluator, Formatter, Validator |
| Document Structure | Renderer, Counter, Searcher |
| File System | SizeCalculator, Permissions, Backup |
| Shopping Cart | TotalCalculator, ShippingCost, Export |
| Anti-pattern | Problem | Solution |
|---|---|---|
| instanceof in visitor | Defeats purpose | Use proper visit methods |
| Mutable visitor state | Race conditions | Use readonly classes |
| Too many element types | Visitor interface bloat | Split into multiple visitors |
| Breaking element encapsulation | Tight coupling | Expose getters, not internals |
| Returning void | Limited usefulness | Return operation results |
For complete PHP templates and examples, see:
references/templates.md — Visitor Interface, Concrete Visitor, Visitable Element templatesreferences/examples.md — PriceCalculator, TaxCalculator, Export visitors with testsnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accAdds new operations to stable object structures without modifying element classes, using the Visitor pattern. Useful for AST analysis, code quality tools, and compiler passes.
Generates Composite pattern for PHP 8.4 with interface, leaf, composite classes, and unit tests. For tree structures like menus, file systems, org charts.
Implements the Visitor pattern for adding operations to object structures via double dispatch. Includes TypeScript examples for AST evaluation, printing, and counting.