php-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PHP Best Practices

PHP最佳实践

Use this skill as a rulebook for building and migrating PHP backend components following TreeNation's DDD architecture.
本技能作为遵循TreeNation的DDD架构构建和迁移PHP后端组件的规则手册使用。

Workflow

工作流程

  1. Open
    AGENTS.md
    .
  2. Identify which components need to be created or changed (controller, service, DTO, repository, response, test).
  3. Load only the relevant rule files from
    rules/
    .
  4. Implement the smallest correct change that satisfies the requirement.
  5. Run PHP CS Fixer and PHPStan before finishing.
  1. 打开
    AGENTS.md
  2. 确定需要创建或修改的组件(控制器、服务、DTO、存储库、响应类、测试)。
  3. 仅加载
    rules/
    目录下的相关规则文件。
  4. 实现满足需求的最小正确变更。
  5. 完成前运行PHP CS Fixer和PHPStan。

Repository Guardrails

仓库规范

  • New PHP files go inside
    src/
    except controllers (which stay in
    app/Http/Controllers/
    ).
  • Controllers must not contain business logic; delegate to a service.
  • Services must be unit-tested; mock all dependencies.
  • Always register new repository interfaces in
    app/Providers/TreeNationProvider.php
    .
  • Use strict typing (
    declare(strict_types=1)
    ) in all new files.
  • Do not add comments unless the user explicitly asks.
  • Follow the Criteria pattern for all repository queries.
  • Access model data via getter methods, not direct property access (see
    rules/model-getters.md
    ).
  • Commits follow pattern:
    TNIT-####: description
    .
  • 新增PHP文件需放在
    src/
    目录下,控制器除外(控制器保留在
    app/Http/Controllers/
    目录)。
  • 控制器不得包含业务逻辑,需将逻辑委托给服务处理。
  • 服务必须进行单元测试,所有依赖项需使用Mock。
  • 所有新增的存储库接口必须在
    app/Providers/TreeNationProvider.php
    中注册。
  • 所有新增文件需启用严格类型(
    declare(strict_types=1)
    )。
  • 除非用户明确要求,否则不要添加注释。
  • 所有存储库查询需遵循Criteria模式。
  • 通过getter方法访问模型数据,不要直接访问属性(参考
    rules/model-getters.md
    )。
  • 提交信息遵循如下格式:
    TNIT-####: description

Output Expectations

输出要求

  • Explain which rules were applied and why.
  • Generate all required files for the feature (controller, request, DTO if needed, service, response, repository interface, repository implementation, test).
  • Keep changes incremental; avoid broad rewrites unless asked.
  • 解释应用了哪些规则以及应用原因。
  • 生成该功能所需的所有文件(控制器、请求类、必要的DTO、服务、响应类、存储库接口、存储库实现、测试)。
  • 保持变更增量式,除非用户要求,否则避免大范围重写。