craft-php-guidelines
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCraft CMS 5 PHP Guidelines
Craft CMS 5 PHP开发指南
Complete PHP coding standards and conventions for active coding sessions.
适用于日常编码工作的完整PHP编码标准与约定。
Common Pitfalls
常见误区
- is the convention in
addSelect()— safely additive when multiple extensions contribute columns. Craft'sbeforePrepare()placeholder merges defaults regardless, but**prevents conflicts.addSelect() - is not a Craft convention — private properties use underscore prefix but meaningful names like
$_instances,$_items.$_sections - Records use the same class name as models (namespace distinguishes). Alias when importing both: .
use ...\records\MyEntity as MyEntityRecord; - Queue jobs have no "Job" suffix — , not
ResaveElements.ResaveElementsJob - is NOT used in plugin source files. Only in standalone config files like
declare(strict_types=1).ecs.php - goes on classes and methods only — never on properties.
@author - Don't use — use
string|null(short nullable notation).?string - Forget and you lose all inherited validation.
parent::defineRules() - in elements/queries,
DateTimeHelperin services — never mix in the same class.Carbon - Missing chains — document exceptions from called methods too, not just your own throws.
@throws
- 是
addSelect()中的约定用法 — 当多个扩展都需要添加查询字段时可以安全地增量添加。虽然Craft的beforePrepare()占位符无论如何都会合并默认值,但**可以避免冲突。addSelect() - 不符合Craft的约定 — 私有属性使用下划线前缀,但要使用有意义的名称,比如
$_instances、$_items。$_sections - 记录类(Record)与模型类(Model)使用相同的类名(通过命名空间区分)。同时导入二者时使用别名:。
use ...\records\MyEntity as MyEntityRecord; - 队列任务类不带"Job"后缀 — 命名为,而非
ResaveElements。ResaveElementsJob - 插件源文件中不使用,仅在
declare(strict_types=1)这类独立配置文件中使用。ecs.php - 仅标注在类和方法上 — 永远不要加在属性上。
@author - 不要使用— 改用
string|null(简短可空类型语法)。?string - 遗漏会导致丢失所有继承来的校验规则。
parent::defineRules() - 元素/查询类中使用,服务类中使用
DateTimeHelper— 不要在同一个类中混用二者。Carbon - 不要遗漏调用链 — 你调用的方法抛出的异常也要文档化,而不仅仅是你自己代码抛出的异常。
@throws
Documentation
参考文档
- Official coding guidelines: https://craftcms.com/docs/5.x/extend/coding-guidelines.html
- Class reference: https://docs.craftcms.com/api/v5/
- Generator reference: https://craftcms.com/docs/5.x/extend/generator.html
When unsure about a convention, the coding guidelines page for the authoritative answer.
web_fetch- 官方编码规范:https://craftcms.com/docs/5.x/extend/coding-guidelines.html
- 类参考文档:https://docs.craftcms.com/api/v5/
- 生成器参考文档:https://craftcms.com/docs/5.x/extend/generator.html
如果对某条约定有疑问,使用获取编码规范页面的权威答案。
web_fetchCritical Rules
核心规则
- PHPDocs on everything: classes, methods, properties. No exceptions.
- chains: document every exception including uncaught from called methods.
@throws - and
@authorat the bottom of class/method docblocks, after a blank line.@since - Section headers with on every class.
// ========================================================================= - is NOT used in plugin source files.
declare(strict_types=1) - Private methods/properties prefixed with underscore: ,
_registerCpUrlRules().$_items - convention in
addSelect()(additive across extensions).beforePrepare() - in elements/queries,
DateTimeHelperin services.Carbon - Always scaffold with , then customize.
ddev craft make <type> --with-docblocks - and
ddev composer check-csmust pass before every commit.ddev composer phpstan
- 所有代码都要加PHPDoc:类、方法、属性,没有例外。
- 调用链:所有异常都要文档化,包括调用的方法抛出的未捕获异常。
@throws - 和
@author放在类/方法文档块的底部,前面空一行。@since - 每个类都要用添加章节分隔头。
// ========================================================================= - 插件源文件中不使用。
declare(strict_types=1) - 私有方法/属性以下划线为前缀:比如、
_registerCpUrlRules()。$_items - 中约定使用
beforePrepare()(支持多扩展增量添加字段)。addSelect() - 元素/查询类中使用,服务类中使用
DateTimeHelper。Carbon - 始终使用生成基础代码,再进行自定义修改。
ddev craft make <type> --with-docblocks - 每次提交前必须通过和
ddev composer check-cs检查。ddev composer phpstan
Section Header Order
章节标题顺序
// Traits
// Const Properties
// Static Properties
// Public Properties
// Protected Properties
// Private Properties
// Public Methods
// Protected Methods
// Private MethodsOnly include sections that have content. Blank line after the separator, before the first item.
// Traits
// Const Properties
// Static Properties
// Public Properties
// Protected Properties
// Private Properties
// Public Methods
// Protected Methods
// Private Methods仅保留有内容的章节。分隔符之后、第一个条目之前空一行。
Naming Quick-Reference
命名速查表
- Services (resource): Plural — ,
Entries,VolumesUsers - Services (utility): Domain noun — ,
Auth,SearchGc - Queue jobs: Action verb, no suffix — ,
ResaveElementsUpdateSearchIndex - Records: Same name as model — namespace distinguishes
- Events: Three patterns — ,
SectionEvent,RegisterUrlRulesEventDefineHtmlEvent - Element actions: Action verb, no suffix — ,
Delete,DuplicateSetStatus - Enums: PascalCase cases, string/int backed — ,
PropagationMethodCmsEdition
- 资源类服务:使用复数形式 — 、
Entries、VolumesUsers - 工具类服务:使用领域名词 — 、
Auth、SearchGc - 队列任务:使用动作动词,无后缀 — 、
ResaveElementsUpdateSearchIndex - 记录类:与模型类同名 — 通过命名空间区分
- 事件类:三种命名模式 — 、
SectionEvent、RegisterUrlRulesEventDefineHtmlEvent - 元素动作:使用动作动词,无后缀 — 、
Delete、DuplicateSetStatus - 枚举类:枚举值使用大驼峰命名,基于字符串/整型 — 、
PropagationMethodCmsEdition
Verification Checklist
提交前检查清单
Before every commit:
- passes
ddev composer check-cs - passes
ddev composer phpstan - Tests green
- PHPDocs complete on all new/modified code
- chains verified
@throws - Section headers present and correct
- Imports alphabetical and grouped
每次提交前:
- 检查通过
ddev composer check-cs - 检查通过
ddev composer phpstan - 测试全部通过
- 所有新增/修改代码的PHPDoc都已补全
- 调用链已核实
@throws - 章节分隔头已添加且格式正确
- 导入语句已按字母顺序排序且分组合理