Awesome-claude-code create-policy
Generates Policy pattern for PHP 8.4. Creates encapsulated business rules for authorization, validation, and domain constraints. Includes unit tests.
install
source · Clone the upstream repo
git clone https://github.com/dykyi-roman/awesome-claude-code
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/dykyi-roman/awesome-claude-code "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/create-policy" ~/.claude/skills/dykyi-roman-awesome-claude-code-create-policy && rm -rf "$T"
manifest:
skills/create-policy/SKILL.mdsource content
Policy Pattern Generator
Creates Policy pattern infrastructure for encapsulating business rules and authorization logic.
When to Use
| Scenario | Example |
|---|---|
| Authorization checks | Can user cancel order? |
| Business rule validation | Is discount applicable? |
| Complex conditions | Multiple rules combined |
| Auditable decisions | Log why access denied |
Component Characteristics
PolicyInterface
- Single responsibility rule
- Returns authorization result
- Provides denial reasons
Policy Implementation
- Encapsulates one business rule
- Stateless evaluation
- Composable with other policies
PolicyResult
- Success/failure status
- Denial reasons
- Metadata for logging
Generation Process
Step 1: Generate Shared Components
Path:
src/Domain/Shared/Policy/
— Result value object with and/or compositionPolicyResult.php
— Enum for AllMustPass/AnyMustPassCompositionMode.php
Step 2: Generate Policy Interface
Path:
src/Domain/{BoundedContext}/Policy/
— Policy contract{Name}PolicyInterface.php
Step 3: Generate Concrete Policies
Path:
src/Domain/{BoundedContext}/Policy/
— First rule implementation{Rule1}Policy.php
— Second rule implementation{Rule2}Policy.php
— Composite policy combining rules{Name}Policy.php
Step 4: Generate Exception
Path:
src/Domain/Shared/Exception/
— Exception with policy contextPolicyViolationException.php
Step 5: Generate Tests
— Individual rule tests{Rule}PolicyTest.php
— Composite policy tests{Name}PolicyTest.php
— Result composition testsPolicyResultTest.php
File Placement
| Component | Path |
|---|---|
| Policy Interface | |
| Policy Implementation | |
| PolicyResult | |
| Exception | |
| Unit Tests | |
Naming Conventions
| Component | Pattern | Example |
|---|---|---|
| Interface | | |
| Implementation | | |
| Composite | | |
| Result | | |
| Exception | | |
| Test | | |
Quick Template Reference
PolicyInterface
interface {Name}PolicyInterface { public function evaluate({SubjectType} $subject, {ResourceType} $resource): PolicyResult; public function getRuleName(): string; }
PolicyResult
final readonly class PolicyResult { public static function allow(): self; public static function deny(string $reason, array $metadata = []): self; public function isAllowed(): bool; public function isDenied(): bool; public function and(self $other): self; // Both must pass public function or(self $other): self; // Either can pass }
Policy Implementation
final readonly class {Rule}Policy implements {Name}PolicyInterface { public function evaluate({Subject} $subject, {Resource} $resource): PolicyResult { if ({condition}) { return PolicyResult::allow(); } return PolicyResult::deny('{reason}', ['context' => 'data']); } public function getRuleName(): string { return '{rule_name}'; } }
Composite Policy
final readonly class {Name}Policy implements {Name}PolicyInterface { public function evaluate({Subject} $subject, {Resource} $resource): PolicyResult { return $this->rule1Policy->evaluate($subject, $resource) ->and($this->rule2Policy->evaluate($subject, $resource)) ->and($this->rule3Policy->evaluate($subject, $resource)); } }
Usage Example
// In UseCase $result = $this->cancellationPolicy->evaluate($user, $order); if ($result->isDenied()) { throw new PolicyViolationException( $this->cancellationPolicy->getRuleName(), $result->getReason(), $result->metadata ); } $order->cancel($reason);
Anti-patterns to Avoid
| Anti-pattern | Problem | Solution |
|---|---|---|
| Side Effects | Policy modifies state | Keep evaluation pure |
| Boolean Returns | No denial reason | Use PolicyResult |
| Fat Policies | Too many rules | Split into composable policies |
| Hardcoded Values | Can't configure | Inject thresholds |
| No Logging Context | Can't debug | Include metadata |
References
For complete PHP templates and examples, see:
— Policy, composite, result templatesreferences/templates.md
— Order cancellation policies and testsreferences/examples.md