create-command
Command Generator
Generate CQRS-compliant Commands and Command Handlers with tests.
Command Characteristics
- Immutable:
final readonly class - Imperative naming: Verb + noun (CreateOrder, ConfirmPayment)
- Self-validating: Validates invariants in constructor
- Intent-revealing: Name describes what should happen
- Returns void or ID: Never returns data
Generation Process
Step 1: Generate Command
Path: src/Application/{BoundedContext}/Command/
{Name}Command.php— Immutable command DTO
Step 2: Generate Handler
Path: src/Application/{BoundedContext}/Handler/
{Name}Handler.php— Command processor
Step 3: Generate Tests
Path: tests/Unit/Application/{BoundedContext}/
File Placement
| Component | Path |
|---|---|
| Command | src/Application/{BoundedContext}/Command/ |
| Handler | src/Application/{BoundedContext}/Handler/ |
| Unit Tests | tests/Unit/Application/{BoundedContext}/ |
Command Naming Conventions
| Action | Command Name | Returns |
|---|---|---|
| Create new | CreateOrderCommand |
ID |
| Confirm/Approve | ConfirmOrderCommand |
void |
| Cancel/Reject | CancelOrderCommand |
void |
| Update property | UpdateShippingAddressCommand |
void |
| Add child | AddOrderLineCommand |
void |
| Remove child | RemoveOrderLineCommand |
void |
Quick Template Reference
Command
final readonly class {Name}Command
{
public function __construct(
public {ValueObject} $id,
public string $data
) {
if (empty($data)) {
throw new \InvalidArgumentException('Data is required');
}
}
public static function fromArray(array $data): self
{
return new self(
id: new {ValueObject}($data['id']),
data: $data['data']
);
}
}
Handler (Update Flow)
final readonly class {Name}Handler
{
public function __construct(
private {Repository}Interface $repository,
private EventDispatcherInterface $events
) {}
public function __invoke({Name}Command $command): void
{
$aggregate = $this->repository->findById($command->id);
if ($aggregate === null) {
throw new NotFoundException($command->id);
}
$aggregate->doSomething($command->data);
$this->repository->save($aggregate);
foreach ($aggregate->releaseEvents() as $event) {
$this->events->dispatch($event);
}
}
}
Handler (Create Flow)
public function __invoke(CreateCommand $command): AggregateId
{
$aggregate = Aggregate::create(
id: $this->repository->nextIdentity(),
...
);
$this->repository->save($aggregate);
foreach ($aggregate->releaseEvents() as $event) {
$this->events->dispatch($event);
}
return $aggregate->id();
}
Anti-patterns to Avoid
| Anti-pattern | Problem | Solution |
|---|---|---|
| Returning Data | Query through command | Use Query for reads |
| No Validation | Invalid commands | Validate in constructor |
| Business Logic | Handler has decisions | Delegate to aggregate |
| Missing Events | Events not dispatched | Always dispatch after save |
| Direct Persistence | Bypassing aggregate | Always use aggregate methods |
References
For complete PHP templates and examples, see:
references/templates.md— Command, Handler, Test templates with patternsreferences/examples.md— CreateOrder, ConfirmOrder, CancelOrder examples and tests
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12