Search by

moodlehq / moodle-cs

andrewnicolsstronk7

Moodle Coding Sniffer rules

Package info

github.com/moodlehq/moodle-cs

Wiki

Type:phpcodesniffer-standard

pkg:composer/moodlehq/moodle-cs

Statistics

Installs: 4 106 027

Dependents: 12

Suggesters: 0

Stars: 28

Open Issues: 60

v3.9.0 2026-11-05 15:11 UTC

README

Latest Stable Version Release Date of the Latest Version

Unit Tests codecov

License Total Downloads Number of Contributors

Information

This repository contains the Moodle Coding Style configurations, written as PHP CodeSniffer rulesets.

Not: PHP_CodeSniffer 4 dropped the JavaScript and CSS tokenizers, so this project only supports scanning PHP code.

Installation

Using Composer (recommended)

You can install these coding style rules using Composer to make them available globally across your system.

This will install the correct version of phpcs, with the Moodle rules, and their dependencies.

composer global config minimum-stability dev
composer global require moodlehq/moodle-cs

Configuration

Typically configuration is not required. Recent versions of Moodle (3.11 onwards) include a configuration file for the PHP CodeSniffer, which will set the standard when run within a Moodle directory.

Additional configuration can be generated automatically to have PHP CodeSniffer ignore any third-party library code. This can be generated by running:

npx grunt ignorefiles

Available Rulesets

From Moodle 6.0 each Moodle version has its own ruleset. These are named in the form moodle[xyz], for example moodle601 is for Moodle 6.1.

The following rulesets are currently available:

  • moodle600 - for Moodle 6.0
  • moodle - for Moodle 5.3 and earlier

A new ruleset will be introduced for each Major Moodle version, and automatically applied to the Moodle main branch.

The following rulesets have been deprecated and will be removed in the future:

  • moodle-extra

Choosing a ruleset for your plugin

A common approach to developing a Moodle plugin is to have a single codebase for all supported versions of Moodle. With the Moodle Coding style moving to support a new coding style for each version of Moodle you should choose a specific version of the coding style to meet.

You can do this by creating a .phpcs.xml file in your plugin with content such as the following:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MoodlePlugin">
  <rule ref="moodle600"/>
</ruleset>

This will apply the coding style for Moodle 6.0 to your code.

Note: If you are running phpcs from the Moodle root directory then phpcs will use the version for the current version of Moodle. You can either:

  • change directory in to your plugin's directory; or
  • pass the --standard=[standard] argument to phpcs.

If using moodle-plugin-ci then you should pass the --standard argument with your preferred option.

Customising a Ruleset for Moodle development

From Moodle 3.11 onwards you can create a file named .phpcs.xml with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MoodleCore">
  <rule ref="./phpcs.xml"/>
  <rule ref="[another-ruleset-here]"/>
</ruleset>

This will load the phpcs.xml file (generated by npx grunt ignorefiles), and apply additional configuration on top of this, including entire additional rulesets.