Skip to content

Commit 15ca009

Browse files
committed
[docs] Mention new moodle-extra style
1 parent ae8ac5a commit 15ca009

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

general/development/tools/phpcs.md

+54-5
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ sidebar_position: 1
99

1010
import { Since } from '@site/src/components';
1111

12-
## Overview
12+
[PHPCodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) is a tool used to analyse PHP code using a set of rules. In many cases these rules can be used to automatically fix the errors they identify.
1313

14-
This document describes the various code sniffing tools that Moodle recommends, their purpose, and their usage.
14+
Moodle has two sets of published rule-sets intended to meet the [Moodle Coding Style](../policies/codingstyle/index.md), and identify any parts of the code do not conform to this style. These are:
1515

16-
[PHPCodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) is a tool used to analyse PHP code using a set of rules. In many cases these rules can be used to automatically fix the errors they identify.
16+
- `moodle` - The standard ruleset which meets all variants of the coding style
17+
- `moodle-extra` - The extended standard which includes recommended best practices
1718

18-
Moodle has published a ruleset intended to meet the [Moodle Coding Style](../policies/codingstyle/index.md), and identify any parts of the code do not conform to this style.
19+
We recommend use of the `moodle-extra` standard, particularly when writing new code.
1920

2021
## Installation
2122

22-
It is recommend that both the phpcs scripts, and the Moodle ruleset, are installed globally using Composer:
23+
The recommended method of installation is via the global composer command:
2324

2425
```console
2526
composer global require moodlehq/moodle-cs
2627
```
2728

29+
This ensures that you have a single copy of the standard used for all code.
30+
2831
### Configuration
2932

3033
<Since versions={["4.1.0", "4.0.1", "3.11.7"]} issueNumber="MDL-74511" />
@@ -33,6 +36,52 @@ A PHPCS configuration is included in the Moodle codebase and ensures that the co
3336

3437
This can be further extended by generating an additional configuration to ignore all third-party libraries using the `grunt ignorefiles` command. See [grunt](./nodejs.md#grunt) for further information on using Grunt.
3538

39+
If you would like to make use of the `moodle-extra` standard then you should create a `.phpcs.xml` file with the following content:
40+
41+
```xml
42+
<?xml version="1.0" encoding="UTF-8"?>
43+
<ruleset name="MoodleCore">
44+
<rule ref="./phpcs.xml"/>
45+
<rule ref="moodle-extra"/>
46+
</ruleset>
47+
```
48+
49+
This will extend the standard configuration, and apply the extra standard on top of it.
50+
51+
#### Moodle 3.10 and earlier
52+
53+
The easiest way to have PHP CodeSniffer pick up your preferred style is via a local configuration file.
54+
55+
You can create a file named `.phpcs.xml` with the following contents:
56+
57+
```xml
58+
<?xml version="1.0" encoding="UTF-8"?>
59+
<ruleset name="MoodleCore">
60+
<rule ref="moodle"/>
61+
</ruleset>
62+
```
63+
64+
If you wish to use the `moodle-extra` coding style, then you can use the following content:
65+
66+
```xml
67+
<?xml version="1.0" encoding="UTF-8"?>
68+
<ruleset name="MoodleCore">
69+
<rule ref="moodle-extra"/>
70+
</ruleset>
71+
```
72+
73+
:::info
74+
75+
Third-party library code will not be ignored with these versions of Moodle.
76+
77+
:::
78+
79+
:::tip Ignoring the file with Git
80+
81+
We recommend configuring your code checkout to ignore the `.phpcs.xml` file by adding a local ignore record to `.git/info/exclude`
82+
83+
:::
84+
3685
#### Community plugins, and older Moodle versions
3786

3887
If you are developing your own plugin outside of the main Moodle directory, or you are working with an older version of Moodle, the easiest way to configure phpcs to use the Moodle ruleset is by creating a local `phpcs.xml.dist` configuration at the root directory of your repository with the following content:

0 commit comments

Comments
 (0)