Claude-skill-registry acc-create-factory
Generates DDD Factory for PHP 8.5. Creates factories for complex domain object instantiation with validation and encapsulated creation logic. Includes unit tests.
install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/acc-create-factory" ~/.claude/skills/majiayu000-claude-skill-registry-acc-create-factory && rm -rf "$T"
manifest:
skills/data/acc-create-factory/SKILL.mdsource content
Factory Generator
Generate DDD-compliant Factories for complex domain object creation.
Factory Characteristics
- Encapsulates Creation: Hides complex instantiation logic
- Validates Input: Ensures valid object creation
- Named Constructors: Provides semantic creation methods
- Domain Layer: Lives in Domain, no infrastructure dependencies
- Returns Valid Objects: Never creates invalid domain objects
- Static or Instance: Static for simple, instance for dependencies
When to Use Factory
| Scenario | Example |
|---|---|
| Complex construction logic | |
| Multiple creation paths | , |
| Aggregate creation | |
| Reconstruction from persistence | |
| Creation with validation | |
Generation Process
Step 1: Determine Factory Type
- Static Factory: No dependencies, simple validation
- Instance Factory: Needs domain services or repositories
Step 2: Generate Factory
Path:
src/Domain/{BoundedContext}/Factory/
— Main factory class{Entity}Factory.php
Step 3: Define Creation Methods
— Primary creation with validationcreate()
— Creation from other objectscreateFrom{Source}()
— Reconstruction from persistence (no validation)reconstitute()
Step 4: Generate Tests
Path:
tests/Unit/Domain/{BoundedContext}/Factory/
File Placement
| Component | Path |
|---|---|
| Factory | |
| Unit Tests | |
Naming Conventions
| Pattern | Example |
|---|---|
| Factory Class | |
| Create Method | , |
| Named Constructor | |
| Reconstitute | |
| Validation | |
Quick Template Reference
Static Factory
final class {Entity}Factory { public static function create({parameters}): {Entity} { self::validate({parameters}); return new {Entity}({constructorArgs}); } public static function createFrom{Source}({SourceType} $source): {Entity} { return new {Entity}({mappedArgs}); } public static function reconstitute({allFields}): {Entity} { return new {Entity}({allArgs}); } private static function validate({parameters}): void { {validationLogic} } }
Instance Factory
final readonly class {Entity}Factory { public function __construct( private {DomainService} $service, private {Repository} $repository ) {} public function create({parameters}): {Entity} { {creationLogicWithDependencies} } }
Anti-patterns to Avoid
| Anti-pattern | Problem | Solution |
|---|---|---|
| Infrastructure in Factory | DB calls in factory | Keep pure domain logic |
| No Validation | Creates invalid objects | Validate before creation |
| Too Many Parameters | Hard to use | Use Value Objects, Builder |
| Mutable Factory | Stateful creation | Make stateless or readonly |
| Missing Reconstitute | Can't hydrate from DB | Add reconstitute method |
References
For complete PHP templates and examples, see:
— Static Factory, Instance Factory, Test templatesreferences/templates.md
— Order, User, Policy factory examples and testsreferences/examples.md