Skip to content

moodlehq/moodle-cs

Repository files navigation

Moodle Coding Style

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.

Two coding styles are included:

  • moodle - the main ruleset for the Moodle Coding Style
  • moodle-extra - extended ruleset which includes recommended best practices
    • extends the main moodle ruleset

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.

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

Using the moodle-extra coding style

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.

Moodle 3.10 and earlier

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.