code-pattern-checker

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code Pattern Checker

代码模式检查器

Validate code against Drupal standards and best practices.
根据Drupal标准和最佳实践验证代码。

Required References

必要参考资料

Load these before checking code:
ReferenceChecks
references/solid-drupal.md
SOLID principles
references/dry-patterns.md
DRY patterns
dev-guides: drupal/security/
Security practices (online)
dev-guides: drupal/sdc/ + drupal/js-development/
CSS/JS/SDC standards (online)
references/quality-gates.md
Gate 1 requirements
For security and frontend checks, WebFetch from
https://camoa.github.io/dev-guides/
instead of reading bundled files.
检查代码前请加载以下内容:
参考资料检查内容
references/solid-drupal.md
SOLID原则
references/dry-patterns.md
DRY模式
dev-guides: drupal/security/
安全实践(在线)
dev-guides: drupal/sdc/ + drupal/js-development/
CSS/JS/SDC标准(在线)
references/quality-gates.md
第一关要求
对于安全和前端检查,请从
https://camoa.github.io/dev-guides/
通过WebFetch获取,而非读取捆绑文件。

Activation

触发时机

Activate when you detect:
  • Before committing code
  • After implementation, before task completion
  • /drupal-dev-framework:validate
    command
  • "Check my code" or "Review this"
  • Invoked by
    task-completer
    skill
在检测到以下情况时激活:
  • 代码提交前
  • 实现完成后、任务结束前
  • 执行
    /drupal-dev-framework:validate
    命令
  • 用户提出“检查我的代码”或“审核此内容”
  • task-completer
    技能调用

Gate Enforcement

关卡强制执行

This skill enforces Gate 1: Code Standards from
references/quality-gates.md
. Code CANNOT be committed until Gate 1 passes.
本技能强制执行
references/quality-gates.md
中的第一关:代码标准。 只有通过第一关后,代码才能提交。

Workflow

工作流程

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
Bash
with
git diff --name-only
to get changed files if option 1.
若内容不明确,请询问用户:
需要检查哪些文件?
1. 所有已修改文件(git diff)
2. 指定文件
3. 某个组件下的所有文件

你的选择:
如果用户选择选项1,使用
Bash
命令
git diff --name-only
获取已修改文件。

2. Read and Analyze Files

2. 读取并分析文件

Use
Read
on each file. For each, check:
PHP 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
    \Drupal::service()
    in new code (BLOCKING)
  • 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
    !important
    (BLOCKING)
  • No
    @extend
    (BLOCKING)
  • 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
undefined

PHP 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
undefined
npm run lint:scss
undefined

4. Report Findings

4. 报告检查结果

Format output as:
undefined
输出格式如下:
undefined

Code Check: {file or component}

代码检查:{文件或组件}

Status: PASS / ISSUES FOUND

状态:通过 / 发现问题

Standards Check

标准检查

CheckStatusNotes
PSR-12PASS-
DocblocksISSUEMissing on processData()
Type hintsPASS-
检查项状态备注
PSR-12通过-
文档块存在问题processData()方法缺少文档块
类型提示通过-

SOLID Principles

SOLID原则

PrincipleStatus
Single ResponsibilityPASS
Dependency InversionPASS
原则状态
单一职责通过
依赖倒置通过

Security

安全检查

CheckStatusNotes
SQL InjectionPASSUses query builder
XSSPASSOutput escaped
Access ControlISSUEMissing on /admin/custom route
检查项状态备注
SQL注入风险通过使用了查询构建器
XSS风险通过输出已转义
访问控制存在问题/admin/custom路由缺少访问检查

DRY Check

DRY检查

IssueLocation
Duplicate logiclines 45-52 and 78-85
问题位置
重复逻辑第45-52行和第78-85行

Issues to Fix (Priority Order)

待修复问题(优先级排序)

  1. Security: Add access check to admin route
  2. Standards: Add docblock to processData()
  3. DRY: Extract duplicate logic to private method
  1. 安全问题:为管理员路由添加访问检查
  2. 标准问题:为processData()添加文档块
  3. 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
  • 合并前修复安全问题
  • 其他问题:立即修复或创建后续任务
是否允许提交:否(先修复安全问题) / 是
undefined

5. 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)
在以下情况时暂停并等待用户操作:
  • 询问待检查文件后
  • 展示检查结果后
  • 应用每个修复方案前
  • 发现安全问题时(重点强调需修复)