code-pattern-checker
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Pattern Checker
代码模式检查器
Validate code against Drupal standards and best practices.
根据Drupal标准和最佳实践验证代码。
Required References
必要参考资料
Load these before checking code:
| Reference | Checks |
|---|---|
| SOLID principles |
| DRY patterns |
| Security practices (online) |
| CSS/JS/SDC standards (online) |
| Gate 1 requirements |
For security and frontend checks, WebFetch frominstead of reading bundled files.https://camoa.github.io/dev-guides/
检查代码前请加载以下内容:
| 参考资料 | 检查内容 |
|---|---|
| SOLID原则 |
| DRY模式 |
| 安全实践(在线) |
| CSS/JS/SDC标准(在线) |
| 第一关要求 |
对于安全和前端检查,请从通过WebFetch获取,而非读取捆绑文件。https://camoa.github.io/dev-guides/
Activation
触发时机
Activate when you detect:
- Before committing code
- After implementation, before task completion
- command
/drupal-dev-framework:validate - "Check my code" or "Review this"
- Invoked by skill
task-completer
在检测到以下情况时激活:
- 代码提交前
- 实现完成后、任务结束前
- 执行命令
/drupal-dev-framework:validate - 用户提出“检查我的代码”或“审核此内容”
- 被技能调用
task-completer
Gate Enforcement
关卡强制执行
This skill enforces Gate 1: Code Standards from .
Code CANNOT be committed until Gate 1 passes.
references/quality-gates.md本技能强制执行中的第一关:代码标准。
只有通过第一关后,代码才能提交。
references/quality-gates.mdWorkflow
工作流程
1. Identify Files to Check
1. 确定待检查文件
Ask if not clear:
Which files should I check?
1. All changed files (git diff)
2. Specific file(s)
3. All files in a component
Your choice:Use with to get changed files if option 1.
Bashgit diff --name-only若内容不明确,请询问用户:
需要检查哪些文件?
1. 所有已修改文件(git diff)
2. 指定文件
3. 某个组件下的所有文件
你的选择:如果用户选择选项1,使用命令获取已修改文件。
Bashgit diff --name-only2. Read and Analyze Files
2. 读取并分析文件
Use on each file. For each, check:
ReadPHP Files:
- PSR-12 / Drupal coding standards
- Docblocks on classes and public methods
- Type hints on parameters and returns
- No deprecated functions
- Naming: PascalCase classes, camelCase methods
SOLID Principles (references/solid-drupal.md):
- Single Responsibility - one purpose per class
- Dependency Inversion - inject dependencies via services.yml
- No in new code (BLOCKING)
\Drupal::service() - Interfaces defined for services
DRY Check (references/dry-patterns.md):
- No duplicate code blocks (BLOCKING)
- Shared logic in services/traits
- Leverages Drupal base classes
Security (dev-guides drupal/security/):
- No raw SQL with user input (BLOCKING)
- Output escaped (Twig auto, Html::escape)
- Form tokens present (Form API handles)
- Access checks on routes (BLOCKING)
- Input validated via Form API
CSS/SCSS (dev-guides drupal/sdc/ + drupal/js-development/):
- Mobile-first media queries
- No (BLOCKING)
!important - No (BLOCKING)
@extend - BEM naming convention
- Drupal behaviors pattern for JS
读取每个文件并进行以下检查:
PHP文件:
- PSR-12 / Drupal编码标准
- 类和公共方法的文档块(Docblocks)
- 参数和返回值的类型提示
- 无已弃用函数
- 命名规范:类使用大驼峰式(PascalCase),方法使用小驼峰式(camelCase)
SOLID原则(参考references/solid-drupal.md):
- 单一职责原则——每个类仅有一种用途
- 依赖倒置原则——通过services.yml注入依赖
- 新代码中禁止使用(阻塞项)
\Drupal::service() - 为服务定义接口
DRY检查(参考references/dry-patterns.md):
- 无重复代码块(阻塞项)
- 共享逻辑封装在服务/特质(traits)中
- 复用Drupal基础类
安全检查(参考dev-guides drupal/security/):
- 禁止在包含用户输入的原生SQL中直接使用(阻塞项)
- 输出已转义(Twig自动处理,或使用Html::escape)
- 表单令牌已存在(由Form API处理)
- 路由已添加访问检查(阻塞项)
- 通过Form API验证输入
CSS/SCSS检查(参考dev-guides drupal/sdc/ + drupal/js-development/):
- 移动端优先的媒体查询
- 禁止使用(阻塞项)
!important - 禁止使用(阻塞项)
@extend - BEM命名规范
- JS使用Drupal behaviors模式
3. Run Automated Tools
3. 运行自动化工具
Suggest running (user executes):
bash
undefined建议用户运行以下命令(由用户执行):
bash
undefinedPHP CodeSniffer
PHP代码嗅探器
ddev exec vendor/bin/phpcs --standard=Drupal,DrupalPractice {path}
ddev exec vendor/bin/phpcs --standard=Drupal,DrupalPractice {path}
PHPStan (if configured)
PHPStan(若已配置)
ddev exec vendor/bin/phpstan analyze {path}
ddev exec vendor/bin/phpstan analyze {path}
SCSS Lint (if applicable)
SCSS代码检查(若适用)
npm run lint:scss
undefinednpm run lint:scss
undefined4. Report Findings
4. 报告检查结果
Format output as:
undefined输出格式如下:
undefinedCode Check: {file or component}
代码检查:{文件或组件}
Status: PASS / ISSUES FOUND
状态:通过 / 发现问题
Standards Check
标准检查
| Check | Status | Notes |
|---|---|---|
| PSR-12 | PASS | - |
| Docblocks | ISSUE | Missing on processData() |
| Type hints | PASS | - |
| 检查项 | 状态 | 备注 |
|---|---|---|
| PSR-12 | 通过 | - |
| 文档块 | 存在问题 | processData()方法缺少文档块 |
| 类型提示 | 通过 | - |
SOLID Principles
SOLID原则
| Principle | Status |
|---|---|
| Single Responsibility | PASS |
| Dependency Inversion | PASS |
| 原则 | 状态 |
|---|---|
| 单一职责 | 通过 |
| 依赖倒置 | 通过 |
Security
安全检查
| Check | Status | Notes |
|---|---|---|
| SQL Injection | PASS | Uses query builder |
| XSS | PASS | Output escaped |
| Access Control | ISSUE | Missing on /admin/custom route |
| 检查项 | 状态 | 备注 |
|---|---|---|
| SQL注入风险 | 通过 | 使用了查询构建器 |
| XSS风险 | 通过 | 输出已转义 |
| 访问控制 | 存在问题 | /admin/custom路由缺少访问检查 |
DRY Check
DRY检查
| Issue | Location |
|---|---|
| Duplicate logic | lines 45-52 and 78-85 |
| 问题 | 位置 |
|---|---|
| 重复逻辑 | 第45-52行和第78-85行 |
Issues to Fix (Priority Order)
待修复问题(优先级排序)
- Security: Add access check to admin route
- Standards: Add docblock to processData()
- DRY: Extract duplicate logic to private method
- 安全问题:为管理员路由添加访问检查
- 标准问题:为processData()添加文档块
- DRY问题:将重复逻辑提取为私有方法
Recommendation
建议
- Fix security issue before merge
- Other issues: fix now or create follow-up task
Approved for commit: NO (fix security first) / YES
undefined- 合并前修复安全问题
- 其他问题:立即修复或创建后续任务
是否允许提交:否(先修复安全问题) / 是
undefined5. Offer Fixes
5. 提供修复方案
For each issue, offer to help:
Issue: Missing docblock on processData()
Suggested fix:
/**
* Process the input data and return results.
*
* @param array $data
* The input data array.
*
* @return array
* The processed results.
*/
Apply this fix? (yes/no/skip)针对每个问题,主动提供帮助:
问题:processData()方法缺少文档块
建议修复方案:
/**
* 处理输入数据并返回结果。
*
* @param array $data
* 输入数据数组。
*
* @return array
* 处理后的结果。
*/
是否应用此修复?(是/否/跳过)Stop Points
暂停节点
STOP and wait for user:
- After asking which files to check
- After presenting findings
- Before applying each fix
- If security issues found (emphasize fixing)
在以下情况时暂停并等待用户操作:
- 询问待检查文件后
- 展示检查结果后
- 应用每个修复方案前
- 发现安全问题时(重点强调需修复)