moodle-standards

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Moodle Standards Skill

Moodle编码标准技能

Apply Moodle coding standards (PSR-12 + exceptions) to PHP code.
将Moodle编码标准(PSR-12+例外规则)应用于PHP代码。

Trigger

触发场景

  • PHP files in Moodle plugin directories
  • User requests code review or formatting
  • New file creation in Moodle context
  • Moodle插件目录中的PHP文件
  • 用户请求代码审查或格式化
  • 在Moodle环境中创建新文件

Actions

执行操作

1. Validate Code Style

1. 验证代码风格

Check code against Moodle standards:
  • Line length (132 ideal, 180 max)
  • Array syntax (
    []
    only)
  • else if
    not
    elseif
  • No closing
    ?>
    tag
  • 4-space indentation
对照Moodle标准检查代码:
  • 行长度(理想值132,最大值180)
  • 数组语法(仅允许
    []
  • 使用
    else if
    而非
    elseif
  • 不添加闭合
    ?>
    标签
  • 4空格缩进

2. Fix Naming

2. 修正命名规范

Ensure proper naming:
  • Variables:
    $lowercase
    (no underscores between words)
  • Functions:
    component_function_name()
    (Frankenstyle)
  • Classes:
    lowercase_with_underscores
  • Constants:
    COMPONENT_CONSTANT_NAME
确保命名合规:
  • 变量:
    $lowercase
    (单词间无下划线)
  • 函数:
    component_function_name()
    (Frankenstyle命名法)
  • 类:
    lowercase_with_underscores
    (小写加下划线)
  • 常量:
    COMPONENT_CONSTANT_NAME
    (全大写加下划线)

3. Add PHPDoc

3. 添加PHPDoc注释

Generate required documentation:
  • File header with GPL
  • @package
    tag (MANDATORY)
  • @param
    with types
  • @return
    description
生成必要的文档注释:
  • 包含GPL协议的文件头
  • @package
    标签(必填)
  • 带类型的
    @param
    注释
  • @return
    描述信息

4. Type Hints

4. 添加类型提示

Add mandatory type declarations:
  • Parameter types
  • Return types
  • Nullable types (
    ?type
    )
添加必填的类型声明:
  • 参数类型
  • 返回值类型
  • 可空类型(
    ?type

Validation Command

验证命令

bash
vendor/bin/phpcs --standard=moodle path/to/file.php
bash
vendor/bin/phpcs --standard=moodle path/to/file.php

Quick Fixes

快速修复方案

Missing Package Tag

缺失@package标签

php
/**
 * @package    mod_myplugin
 */
php
/**
 * @package    mod_myplugin
 */

Wrong Array Syntax

错误的数组语法

php
// Before
$arr = array('a', 'b');
// After
$arr = ['a', 'b'];
php
// 修复前
$arr = array('a', 'b');
// 修复后
$arr = ['a', 'b'];

Wrong Else If

错误的else if写法

php
// Before
} elseif ($x) {
// After
} else if ($x) {
php
// 修复前
} elseif ($x) {
// 修复后
} else if ($x) {