create-visitor

Generates Visitor pattern for PHP 8.4. Creates operations on object structures without modifying element classes, with visitor interface, concrete visitors, and visitable elements. Includes unit tests.

59 stars

Best use case

create-visitor is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Generates Visitor pattern for PHP 8.4. Creates operations on object structures without modifying element classes, with visitor interface, concrete visitors, and visitable elements. Includes unit tests.

Teams using create-visitor should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/create-visitor/SKILL.md --create-dirs "https://raw.githubusercontent.com/dykyi-roman/awesome-claude-code/main/skills/create-visitor/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/create-visitor/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How create-visitor Compares

Feature / Agentcreate-visitorStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Generates Visitor pattern for PHP 8.4. Creates operations on object structures without modifying element classes, with visitor interface, concrete visitors, and visitable elements. Includes unit tests.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Visitor Pattern Generator

Creates Visitor pattern infrastructure for operations on object structures without modifying classes.

## When to Use

| 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 |

## Component Characteristics

### Visitor Interface
- Declares visit methods for each element type
- One method per visitable element
- Returns operation result
- Enables double dispatch

### Concrete Visitors
- Implement specific operations
- Process each element type differently
- Encapsulate algorithm logic
- Can accumulate state during traversal

### Visitable Elements
- Accept visitor via accept() method
- Call visitor's visit method with self
- Enable operation without modification
- Maintain element structure

---

## Generation Process

### Step 1: Generate Visitor Interface

**Path:** `src/Domain/{BoundedContext}/Visitor/`

1. `{Name}VisitorInterface.php` — Visitor contract with visit methods

### Step 2: Generate Concrete Visitors

**Path:** `src/Domain/{BoundedContext}/Visitor/` or `src/Application/{BoundedContext}/`

1. `{Operation1}Visitor.php` — First operation implementation
2. `{Operation2}Visitor.php` — Second operation implementation
3. `{Operation3}Visitor.php` — Third operation implementation

### Step 3: Generate Visitable Interface

**Path:** `src/Domain/{BoundedContext}/`

1. `VisitableInterface.php` — Element contract with accept() method

### Step 4: Update Existing Elements

**Path:** `src/Domain/{BoundedContext}/`

1. Add `implements VisitableInterface` to element classes
2. Add `accept()` method to each element

### Step 5: Generate Tests

1. `{Operation}VisitorTest.php` — Individual visitor tests
2. `{Element}AcceptTest.php` — Element accept() tests

---

## File Placement

| 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/` |

---

## Naming Conventions

| 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` |

---

## Quick Template Reference

### Visitor Interface

```php
interface {Name}VisitorInterface
{
    public function visit{Element1}({Element1} $element): {ReturnType};
    public function visit{Element2}({Element2} $element): {ReturnType};
    public function visit{Element3}({Element3} $element): {ReturnType};
}
```

### Concrete Visitor

```php
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
    }
}
```

### Visitable Interface

```php
interface VisitableInterface
{
    public function accept({Name}VisitorInterface $visitor): mixed;
}
```

### Visitable Element

```php
final readonly class {Element} implements VisitableInterface
{
    public function accept({Name}VisitorInterface $visitor): mixed
    {
        return $visitor->visit{Element}($this);
    }
}
```

---

## Usage Example

```php
// 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);
```

---

## Common Visitor Operations

| 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-patterns to Avoid

| 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 |

---

## References

For complete PHP templates and examples, see:
- `references/templates.md` — Visitor Interface, Concrete Visitor, Visitable Element templates
- `references/examples.md` — PriceCalculator, TaxCalculator, Export visitors with tests

Related Skills

create-value-object

59
from dykyi-roman/awesome-claude-code

Generates DDD Value Objects for PHP 8.4. Creates immutable, self-validating objects with equality comparison. Includes unit tests.

create-use-case

59
from dykyi-roman/awesome-claude-code

Generates Application Use Cases for PHP 8.4. Creates orchestration services that coordinate domain objects, handle transactions, and dispatch events. Includes unit tests.

create-unit-test

59
from dykyi-roman/awesome-claude-code

Generates PHPUnit unit tests for PHP 8.4. Creates isolated tests with AAA pattern, proper naming, attributes, and one behavior per test. Supports Value Objects, Entities, Services.

create-unit-of-work

59
from dykyi-roman/awesome-claude-code

Generates Unit of Work pattern components for PHP 8.4. Creates transactional consistency infrastructure with aggregate tracking, flush/rollback, domain event collection, and unit tests.

create-timeout

59
from dykyi-roman/awesome-claude-code

Generates Timeout pattern components for PHP 8.4. Creates execution time limit infrastructure with configurable timeouts, fallback support, stream timeouts, and unit tests.

create-test-double

59
from dykyi-roman/awesome-claude-code

Generates test doubles (Mocks, Stubs, Fakes, Spies) for PHP 8.4. Creates appropriate double type based on testing needs with PHPUnit MockBuilder patterns.

create-test-builder

59
from dykyi-roman/awesome-claude-code

Generates Test Data Builder and Object Mother patterns for PHP 8.4. Creates fluent builders with sensible defaults and factory methods for test data creation.

create-template-method

59
from dykyi-roman/awesome-claude-code

Generates Template Method pattern for PHP 8.4. Creates abstract algorithm skeleton with customizable steps, allowing subclasses to override specific parts without changing structure. Includes unit tests.

create-structured-logger

59
from dykyi-roman/awesome-claude-code

Generates Structured Logger for PHP 8.4. Creates PSR-3 structured logging setup with Monolog processors, correlation ID propagation, and context middleware. Includes unit tests.

create-strategy

59
from dykyi-roman/awesome-claude-code

Generates Strategy pattern for PHP 8.4. Creates interchangeable algorithm families with context class, strategy interface, and concrete implementations. Includes unit tests.

create-state

59
from dykyi-roman/awesome-claude-code

Generates State pattern for PHP 8.4. Creates state machines with context, state interface, and concrete states for behavior changes. Includes unit tests.

create-specification

59
from dykyi-roman/awesome-claude-code

Generates DDD Specification for PHP 8.4. Creates reusable business rule objects for validation, filtering, and querying with composite pattern support. Includes unit tests.