psr12-moodle
4
总安装量
4
周安装量
#51334
全站排名
安装命令
npx skills add https://github.com/astoeffer/plugin-marketplace --skill psr12-moodle
Agent 安装分布
opencode
4
claude-code
4
codex
3
gemini-cli
3
replit
2
cursor
2
Skill 文档
PSR-12 Moodle Compliance Skill
Automatic Activation Triggers
This skill activates automatically when:
- Writing or editing PHP files in Moodle plugin directories
- User mentions “code standards”, “PSR-12”, “phpcs”, or “coding style”
- Discussions about refactoring or code quality
- After implementing new Moodle functions or classes
Moodle-Specific PSR-12 Rules
Core Principle
Moodle follows PSR-12 with specific exceptions for legacy compatibility.
Naming Conventions (EXCEPTIONS to PSR-12)
Classes
// â PSR-12 Standard (PascalCase)
class FolderBrowser {}
// â
Moodle Standard (lowercase_with_underscores)
class folder_browser {}
Functions & Methods
// â PSR-12 Standard (camelCase)
public function getUserData() {}
// â
Moodle Standard (lowercase_with_underscores)
public function get_user_data() {}
Variables
// â PSR-12 Standard (camelCase)
$userData = [];
// â
Moodle Standard (lowercase_with_underscores)
$user_data = [];
Frankenstyle Naming (REQUIRED)
All functions, classes, and namespaces must include component prefix:
// â Missing component prefix
function get_folder_contents() {}
class folder_browser {}
// â
With frankenstyle prefix
function mod_nextcloudfolder_get_folder_contents() {}
class mod_nextcloudfolder_folder_browser {}
PSR-12 Rules FOLLOWED by Moodle
1. Indentation: 4 spaces
// â
Correct
function example() {
if ($condition) {
do_something();
}
}
2. Line Length: 180 characters max (Moodle extends PSR-12’s 120)
// â ï¸ Moodle allows up to 180 characters per line
$result = $DB->get_record_sql('SELECT * FROM {table} WHERE field1 = ? AND field2 = ? AND field3 = ?', [$param1, $param2, $param3]);
3. Opening Braces: Same line for control structures
// â
Correct
if ($condition) {
// code
}
// â
New line for functions/classes
function my_function()
{
// code
}
4. Namespaces
// â
Correct namespace with frankenstyle
namespace mod_nextcloudfolder\local;
class helper {
// ...
}
5. Use statements
// â
One per line, alphabetically sorted
use mod_nextcloudfolder\local\api;
use mod_nextcloudfolder\local\helper;
Validation Workflow
Step 1: Read Current Code
# Use Read tool to examine PHP file
Step 2: Identify Violations
Check for:
- camelCase naming â lowercase_with_underscores
- Missing frankenstyle prefixes
- Incorrect indentation (not 4 spaces)
- Lines exceeding 180 characters
- Missing or incorrect PHPDoc blocks
- Improper brace placement
Step 3: Run phpcs
# Moodle code checker
vendor/bin/phpcs --standard=moodle path/to/plugin/
# Or use dev helper if available
./dev.sh check
Step 4: Apply Fixes
Automatic fixes:
vendor/bin/phpcbf --standard=moodle path/to/plugin/
Manual fixes: Use Edit tool for:
- Renaming violations
- Adding frankenstyle prefixes
- Fixing complex structural issues
Step 5: Verify
# Rerun phpcs to confirm clean
vendor/bin/phpcs --standard=moodle path/to/plugin/
Common Violations & Fixes
1. camelCase Function Names
// â Before
function getUserFolders($userid) {
return $DB->get_records('folders', ['userid' => $userid]);
}
// â
After
function mod_nextcloudfolder_get_user_folders($userid) {
return $DB->get_records('nextcloudfolder', ['userid' => $userid]);
}
2. Missing PHPDoc
// â Before
function get_folders() {
// ...
}
// â
After
/**
* Get all folders for current user.
*
* @return array Array of folder objects
*/
function mod_nextcloudfolder_get_folders() {
// ...
}
3. Class Naming
// â Before
class FolderApi {
// ...
}
// â
After
namespace mod_nextcloudfolder\local;
/**
* Folder API helper class.
*
* @package mod_nextcloudfolder
* @copyright 2024 Your Name
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class folder_api {
// ...
}
4. Indentation Issues
// â Before (2 spaces or tabs)
function example() {
if ($condition) {
do_something();
}
}
// â
After (4 spaces)
function example() {
if ($condition) {
do_something();
}
}
5. Long Lines
// â Before (>180 chars)
$result = $DB->get_record_sql('SELECT * FROM {table} WHERE field1 = ? AND field2 = ? AND field3 = ? AND field4 = ? AND field5 = ?', [$param1, $param2, $param3, $param4, $param5]);
// â
After (split logically)
$sql = 'SELECT * FROM {table}
WHERE field1 = ? AND field2 = ?
AND field3 = ? AND field4 = ?
AND field5 = ?';
$params = [$param1, $param2, $param3, $param4, $param5];
$result = $DB->get_record_sql($sql, $params);
Output Format
After validation and fixes:
â
PSR-12 Moodle Compliance Check
File: mod/nextcloudfolder/lib.php
Status: â
PASSED (or â FAILED)
Issues Fixed:
- â Renamed getUserData() â get_user_data()
- â Added frankenstyle prefix to class folder_browser
- â Fixed indentation (27 lines)
- â Added missing PHPDoc blocks (5 functions)
- â Split 3 lines exceeding 180 characters
Remaining Issues: 0
Next: Run `vendor/bin/phpcs --standard=moodle mod/nextcloudfolder/` to verify.
Integration with Development Workflow
- Before Commit: Auto-run this skill on all modified PHP files
- During Code Review: Validate pull requests
- CI/CD Pipeline: Automated standards checking
- IDE Integration: Real-time validation