This repository contains the Moodle Coding Style configurations, written as PHP CodeSniffer rulesets.
Two coding styles are included:
moodle
- the main ruleset for the Moodle Coding Stylemoodle-extra
- extended ruleset which includes recommended best practices- extends the main
moodle
ruleset
- extends the main
Currently this only includes the configuration for PHP Coding style, but this may be extended to include custom rules for JavaScript, and any other supported languages or syntaxes.
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
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
The recommended way of configuring PHP CodeSniffer to use the moodle-extra
coding style is to provide an additional configuration file.
For 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="moodle-extra"/>
</ruleset>
This will load the phpcs.xml
file (generated by npx grunt ignorefiles
), and apply the moodle-extra
configuration on top.
The easiset way to have PHP CodeSniffer pick up your preferred style, you can create a file named phpcs.xml
with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MoodleCore">
<rule ref="moodle"/>
</ruleset>
If you wish to use the moodle-extra
coding style, then you can use the following content:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MoodleCore">
<rule ref="moodle-extra"/>
</ruleset>
Note: Third-party library code will not be ignored with these versions of Moodle.