create-state

为 PHP 8.4 生成状态模式实现:创建包含上下文对象、状态接口和若干具体状态类的状态机,用于在运行时改变行为。输出同时包含相应的单元测试用例。

仓库最近推送
GitHub Stars
109
Forks
26
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "create-state":https://skillpi.com/zh/skills/deepskill-b37fcdffd9d3a265536650ec992d5944e707f7e10901a83f09d783a85f591d8b。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

为 PHP 8.4 生成状态模式实现:创建包含上下文对象、状态接口和若干具体状态类的状态机,用于在运行时改变行为。输出同时包含相应的单元测试用例。

0 下载

介绍来源: third_party · dykyi-roman · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
适合谁
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: create-state
description: Generates State pattern for PHP 8.4. Creates state machines with context, state interface, and concrete states for behavior changes. Includes unit tests.

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/

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

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

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

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

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

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

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值88/100

亮点

  • 提供 PHP 8.4 状态模式的完整生成方案,包含上下文、状态接口、具体状态、异常处理、工厂模式及单元测试
  • 适用于对象行为随状态变化的常见场景(如订单、文档工作流等),具有明确的实用价值和广泛适用性
触发精准92/100

亮点

  • description 明确描述了功能(为 PHP 8.4 生成状态模式,包含状态机、单元测试),触发词清晰,覆盖典型使用场景,边界明确
结构规范95/100

亮点

  • 文档结构完整,包含 When to Use、Component Characteristics、Generation Process(分步骤)、File Placement、Naming Conventions、Quick Template Reference、Usage Example、State Diagram、Anti-patterns、References 等章节
  • 步骤清晰,有出口条件,含端到端示例,frontmatter 完整,单文件未超 500 行
执行可靠90/100

亮点

  • 提供了完整的代码模板(接口、抽象类、具体状态、工厂、异常)、文件路径规范、命名约定、测试结构
  • 工具来源明确(PHP 标准语法),参数正确,覆盖主要异常(InvalidStateTransitionException),不可逆操作(状态转换)有确认机制(canTransitionTo)
证据可验 (前四项均值)91/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。