solid-php
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSOLID PHP - Laravel 12 + PHP 8.5
SOLID PHP - Laravel 12 + PHP 8.5
Agent Workflow (MANDATORY)
Agent工作流(强制要求)
Before ANY implementation, use to spawn 3 agents:
TeamCreate- fuse-ai-pilot:explore-codebase - Analyze existing architecture
- fuse-ai-pilot:research-expert - Verify Laravel/PHP docs via Context7
- mcp__context7__query-docs - Check SOLID patterns
After implementation, run fuse-ai-pilot:sniper for validation.
在进行任何代码实现之前,请使用生成3个Agent:
TeamCreate- fuse-ai-pilot:explore-codebase - 分析现有架构
- fuse-ai-pilot:research-expert - 通过Context7校验Laravel/PHP官方文档
- mcp__context7__query-docs - 检查SOLID设计模式
实现完成后,运行fuse-ai-pilot:sniper进行校验。
DRY - Reuse Before Creating (MANDATORY)
DRY原则 - 编码前优先复用(强制要求)
Before writing ANY new code:
- Grep the codebase for similar class names, methods, or logic
- Check shared locations: ,
FuseCore/Core/App/Services/,FuseCore/Core/App/Traits/FuseCore/[Module]/App/Contracts/ - If similar code exists → extend/reuse instead of duplicate
- If code will be used by 2+ features → create it in shared Services or Traits
- Extract repeated logic (3+ occurrences) into shared helpers or Traits
- Verify no duplication introduced after writing
在编写任何新代码之前:
- 检索整个代码库查找相似的类名、方法或逻辑
- 检查公共代码目录:、
FuseCore/Core/App/Services/、FuseCore/Core/App/Traits/FuseCore/[Module]/App/Contracts/ - 如果存在相似代码 → 继承/复用而不是重复编写
- 如果代码会被2个以上功能使用 → 放在公共Services或Traits目录中
- 将重复出现3次以上的逻辑抽离为公共助手函数或Trait
- 编写完成后校验没有引入重复代码
Auto-Detection (Modular MANDATORY)
自动检测(模块化强制要求)
| Files Detected | Architecture | Interfaces Location |
|---|---|---|
| FuseCore Modular | |
| FuseCore Modular | |
Verification: → Laravel 12.x
Structure: Always FuseCore modular. Shared in .
php artisan --versionFuseCore/Core/App/| 检测到的文件 | 架构类型 | 接口存放位置 |
|---|---|---|
| FuseCore 模块化 | |
模块目录下存在 | FuseCore 模块化 | |
校验方式: → 确认是Laravel 12.x
结构要求:始终采用FuseCore模块化结构,公共代码放在目录下。
php artisan --versionFuseCore/Core/App/Decision Tree: Where to Put Code? (FuseCore Modular)
决策树:代码应该存放在哪里?(FuseCore模块化规范)
New code needed?
├── HTTP validation → FuseCore/[Module]/App/Http/Requests/
├── Single action → FuseCore/[Module]/App/Actions/
├── Business logic → FuseCore/[Module]/App/Services/
├── Data access → FuseCore/[Module]/App/Repositories/
├── Data transfer → FuseCore/[Module]/App/DTOs/
├── Interface → FuseCore/[Module]/App/Contracts/
├── Event → FuseCore/[Module]/App/Events/
└── Authorization → FuseCore/[Module]/App/Policies/需要新增代码?
├── HTTP校验 → FuseCore/[Module]/App/Http/Requests/
├── 单一场景操作 → FuseCore/[Module]/App/Actions/
├── 业务逻辑 → FuseCore/[Module]/App/Services/
├── 数据访问 → FuseCore/[Module]/App/Repositories/
├── 数据传输 → FuseCore/[Module]/App/DTOs/
├── 接口定义 → FuseCore/[Module]/App/Contracts/
├── 事件 → FuseCore/[Module]/App/Events/
└── 权限校验 → FuseCore/[Module]/App/Policies/Decision Tree: Which Pattern?
决策树:应该使用哪种设计模式?
| Need | Pattern | Location | Max Lines |
|---|---|---|---|
| HTTP handling | Controller | Controllers/ | 50 |
| Validation | FormRequest | Requests/ | 50 |
| Single operation | Action | Actions/ | 50 |
| Complex logic | Service | Services/ | 100 |
| Data access | Repository | Repositories/ | 100 |
| Data structure | DTO | DTOs/ | 50 |
| Abstraction | Interface | Contracts/ | 30 |
| 需求场景 | 设计模式 | 存放位置 | 最大行数 |
|---|---|---|---|
| HTTP请求处理 | Controller | Controllers/ | 50 |
| 请求参数校验 | FormRequest | Requests/ | 50 |
| 单一操作逻辑 | Action | Actions/ | 50 |
| 复杂业务逻辑 | Service | Services/ | 100 |
| 数据访问逻辑 | Repository | Repositories/ | 100 |
| 数据结构定义 | DTO | DTOs/ | 50 |
| 抽象层定义 | Interface | Contracts/ | 30 |
Critical Rules (MANDATORY)
核心规则(强制要求)
1. Files < 100 lines
1. 文件行数不超过100行
- Split at 90 lines - Never exceed 100
- Controllers < 50 lines
- Models < 80 lines (excluding relations)
- Services < 100 lines
- 达到90行就拆分 - 绝对不能超过100行
- Controller文件不超过50行
- Model文件不超过80行(关联关系定义除外)
- Service文件不超过100行
2. Interfaces Separated (FuseCore Modular MANDATORY)
2. 接口单独存放(FuseCore模块化强制要求)
FuseCore/[Module]/App/Contracts/ # Module interfaces ONLY
├── UserRepositoryInterface.php
└── PaymentGatewayInterface.php
FuseCore/Core/App/Contracts/ # Shared interfacesFuseCore/[Module]/App/Contracts/ # 仅存放当前模块的接口
├── UserRepositoryInterface.php
└── PaymentGatewayInterface.php
FuseCore/Core/App/Contracts/ # 存放公共接口3. PHPDoc Mandatory
3. 强制要求编写PHPDoc
php
/**
* Create a new user from DTO.
*
* @param CreateUserDTO $dto User data transfer object
* @return User Created user model
* @throws ValidationException If email already exists
*/
public function create(CreateUserDTO $dto): Userphp
/**
* Create a new user from DTO.
*
* @param CreateUserDTO $dto User data transfer object
* @return User Created user model
* @throws ValidationException If email already exists
*/
public function create(CreateUserDTO $dto): UserReference Guide
参考指南
Concepts
概念
| Topic | Reference | When to consult |
|---|---|---|
| SOLID Overview | solid-principles.md | Quick reference all principles |
| SRP | single-responsibility.md | File too long, fat controller/model |
| OCP | open-closed.md | Adding payment methods, channels |
| LSP | liskov-substitution.md | Interface contracts, swapping providers |
| ISP | interface-segregation.md | Fat interfaces, unused methods |
| DIP | dependency-inversion.md | Tight coupling, testing, mocking |
| Anti-Patterns | anti-patterns.md | Code smells detection |
| Decisions | decision-guide.md | Pattern selection |
| PHP 8.5 | php85-features.md | Modern PHP features |
| Structure | laravel12-structure.md | Standard Laravel |
| FuseCore | fusecore-structure.md | Modular architecture |
| 主题 | 参考文档 | 适用场景 |
|---|---|---|
| SOLID 总览 | solid-principles.md | 快速查阅所有SOLID原则 |
| SRP 单一职责原则 | single-responsibility.md | 文件过长、Controller/Model过于臃肿时 |
| OCP 开闭原则 | open-closed.md | 新增支付方式、渠道等扩展场景时 |
| LSP 里氏替换原则 | liskov-substitution.md | 定义接口契约、切换服务提供商时 |
| ISP 接口隔离原则 | interface-segregation.md | 接口过于臃肿、存在大量未使用方法时 |
| DIP 依赖倒置原则 | dependency-inversion.md | 代码耦合度高、需要测试、Mock时 |
| 反模式 | anti-patterns.md | 检测代码坏味道时 |
| 决策指南 | decision-guide.md | 选择设计模式时 |
| PHP 8.5 | php85-features.md | 查阅PHP新特性时 |
| 结构规范 | laravel12-structure.md | 标准Laravel项目参考 |
| FuseCore | fusecore-structure.md | 模块化架构参考 |
Templates
模板
| Template | When to use |
|---|---|
| code-templates.md | Service, DTO, Repository, Interface |
| controller-templates.md | Controller, Action, FormRequest, Policy |
| refactoring-guide.md | Step-by-step migration from legacy code |
| 模板 | 适用场景 |
|---|---|
| code-templates.md | Service、DTO、Repository、Interface编写 |
| controller-templates.md | Controller、Action、FormRequest、Policy编写 |
| refactoring-guide.md | 从 legacy 代码逐步迁移重构 |
Forbidden
禁止行为
| Anti-Pattern | Detection | Fix |
|---|---|---|
| Files > 100 lines | Line count | Split into smaller files |
| Controllers > 50 lines | Line count | Extract to Service |
| Interfaces in impl files | Location | Move to |
| Business logic in Models | Code in model | Extract to Service |
| Concrete dependencies | | Inject via ModuleServiceProvider |
| Missing PHPDoc | No doc block | Add documentation |
| Missing strict_types | No declare | Add to all files |
| Fat classes | > 5 public methods | Split responsibilities |
| Duplicated logic | Same code in 2+ files | Extract to Service/Trait |
| No Grep before coding | Creating without search | Grep codebase first |
| 反模式 | 检测方式 | 修复方案 |
|---|---|---|
| 文件行数超过100行 | 行数统计 | 拆分为更小的文件 |
| Controller行数超过50行 | 行数统计 | 将逻辑抽离到Service |
| 接口定义放在实现类文件中 | 文件位置检查 | 移动到 |
| 业务逻辑写在Model中 | 代码内容检查 | 将逻辑抽离到Service |
| 硬编码依赖具体实现 | 检测 | 通过ModuleServiceProvider注入依赖 |
| 缺失PHPDoc | 检测是否存在文档块 | 补充对应的文档说明 |
| 缺失strict_types声明 | 检测文件头部声明 | 所有文件顶部添加声明 |
| 类过于臃肿 | 公共方法超过5个 | 拆分职责到多个类 |
| 重复逻辑 | 2个以上文件存在相同代码 | 抽离到Service/Trait |
| 编码前未检索代码库 | 未检索就新增代码 | 编码前先检索整个代码库 |
Best Practices
最佳实践
| DO | DON'T |
|---|---|
| Use constructor property promotion | Use property assignment |
| Depend on interfaces | Depend on concrete classes |
Use | Use mutable classes |
Use | Skip type declarations |
| Split at 90 lines | Wait until 100 lines |
| Use DTOs for data transfer | Use arrays |
| 推荐做法 | 禁止做法 |
|---|---|
| 使用构造函数属性提升语法 | 手动编写属性赋值逻辑 |
| 依赖接口 | 依赖具体实现类 |
使用 | 使用可变类 |
声明 | 省略类型声明 |
| 达到90行就拆分 | 等到100行才拆分 |
| 使用DTO进行数据传输 | 使用数组传输数据 |