create-state

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

59 stars

Best use case

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

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

Teams using create-state 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-state/SKILL.md --create-dirs "https://raw.githubusercontent.com/dykyi-roman/awesome-claude-code/main/skills/create-state/SKILL.md"

Manual Installation

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

How create-state Compares

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

Frequently Asked Questions

What does this skill do?

Generates State pattern for PHP 8.4. Creates state machines with context, state interface, and concrete states for behavior changes. 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

# State Pattern Generator

Creates State pattern infrastructure for objects that change behavior based on internal state.

## When to Use

| Scenario | Example |
|----------|---------|
| Object behavior varies by state | Order (pending, paid, shipped) |
| Many conditionals based on state | Document workflow |
| State-specific transitions | Subscription lifecycle |
| Finite state machines | Payment processing |

## Component Characteristics

### StateInterface
- Defines available actions
- Returns new state after transition
- Encapsulates state-specific behavior

### Context
- Holds current state
- Delegates actions to state
- Manages state transitions

### Concrete States
- Implement behavior for each state
- Handle valid/invalid transitions
- Return appropriate next state

---

## Generation Process

### Step 1: Analyze State Machine

Determine:
- All possible states
- Actions/transitions between states
- Which actions are valid per state
- Terminal states (no outgoing transitions)

### Step 2: Generate State Components

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

1. `{Name}StateInterface.php` — State contract with all actions
2. `Abstract{Name}State.php` — Base class with default (invalid) implementations
3. `{StateName}State.php` — Concrete state for each state (PendingState, PaidState, etc.)
4. `{Name}StateFactory.php` — Factory for reconstitution from storage

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

5. `InvalidStateTransitionException.php` — Exception for invalid transitions

### Step 3: Update Entity

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

Update entity to use state pattern with delegation methods.

### Step 4: Generate Tests

**Path:** `tests/Unit/Domain/{BoundedContext}/State/`

1. `{StateName}StateTest.php` — Test each concrete state
2. `{Entity}StateTransitionTest.php` — Test full state machine

---

## File Placement

| Component | Path |
|-----------|------|
| State Interface | `src/Domain/{BoundedContext}/State/` |
| Concrete States | `src/Domain/{BoundedContext}/State/` |
| State Factory | `src/Domain/{BoundedContext}/State/` |
| Entity with State | `src/Domain/{BoundedContext}/Entity/` |
| Exception | `src/Domain/{BoundedContext}/Exception/` |
| Unit Tests | `tests/Unit/Domain/{BoundedContext}/State/` |

---

## Naming Conventions

| Component | Pattern | Example |
|-----------|---------|---------|
| Interface | `{Name}StateInterface` | `OrderStateInterface` |
| Abstract State | `Abstract{Name}State` | `AbstractOrderState` |
| Concrete State | `{StateName}State` | `PendingState`, `PaidState` |
| Factory | `{Name}StateFactory` | `OrderStateFactory` |
| Exception | `InvalidStateTransitionException` | `InvalidStateTransitionException` |
| Test | `{ClassName}Test` | `PendingStateTest` |

---

## Quick Template Reference

### State Interface

```php
interface {Name}StateInterface
{
    public function getName(): string;
    public function {action1}({Context} $context): self;
    public function {action2}({Context} $context): self;
    public function canTransitionTo(self $state): bool;
    /** @return array<string> */
    public function allowedTransitions(): array;
}
```

### Abstract State

```php
abstract readonly class Abstract{Name}State implements {Name}StateInterface
{
    public function {action1}({Context} $context): {Name}StateInterface
    {
        throw InvalidStateTransitionException::actionNotAllowed('{action1}', $this->getName());
    }

    public function canTransitionTo({Name}StateInterface $state): bool
    {
        return in_array($state->getName(), $this->allowedTransitions(), true);
    }
}
```

### Concrete State

```php
final readonly class {StateName}State extends Abstract{Name}State
{
    public function getName(): string
    {
        return '{state_name}';
    }

    public function {action1}({Context} $context): {Name}StateInterface
    {
        $context->recordEvent(new {Event}($context->id()));
        return new {NextState}State();
    }

    public function allowedTransitions(): array
    {
        return ['{next_state_1}', '{next_state_2}'];
    }
}
```

### State Factory

```php
final class {Name}StateFactory
{
    public static function fromName(string $name): {Name}StateInterface
    {
        return match ($name) {
            'pending' => new PendingState(),
            'confirmed' => new ConfirmedState(),
            default => throw new \InvalidArgumentException("Unknown state: $name"),
        };
    }
}
```

---

## Usage Example

```php
// Entity with state
$order = new Order($id, $customerId, $items);
$order->confirm();  // Pending -> Confirmed
$order->pay();      // Confirmed -> Paid
$order->ship();     // Paid -> Shipped
$order->deliver();  // Shipped -> Delivered

// Check state
if ($order->isInState('delivered')) {
    // ...
}

// Reconstitute from storage
$state = OrderStateFactory::fromName($row['state']);
$order = new Order($id, $customerId, $items, $state);
```

---

## State Diagram Template

```
┌──────────┐  action1   ┌──────────┐  action2   ┌──────────┐
│  State1  │───────────▶│  State2  │───────────▶│  State3  │
└────┬─────┘            └────┬─────┘            └──────────┘
     │                       │
     │ cancel                │ cancel
     │                       │
     ▼                       ▼
┌──────────┐            ┌──────────┐
│Cancelled │            │Cancelled │
└──────────┘            └──────────┘
```

---

## Anti-patterns to Avoid

| Anti-pattern | Problem | Solution |
|--------------|---------|----------|
| Mutable States | Shared state pollution | Make states readonly |
| God State | One state handles all | Split into specific states |
| Missing Transitions | Silent failures | Throw on invalid action |
| State in Entity | Mixed concerns | Extract to State classes |
| No Factory | Hard to reconstitute | Add StateFactory |

---

## References

For complete PHP templates and examples, see:
- `references/templates.md` — State interface, abstract, concrete templates
- `references/examples.md` — Order state machine example and tests

Related Skills

extract-state-machine

59
from dykyi-roman/awesome-claude-code

Detects state machines from enums, status fields, switch/match statements, and transition methods. Extracts states, transitions, guards, and actions to build state diagram data.

create-visitor

59
from dykyi-roman/awesome-claude-code

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.

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.