You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: general/development/tools/phpcs.md
+54-5
Original file line number
Diff line number
Diff line change
@@ -9,22 +9,25 @@ sidebar_position: 1
9
9
10
10
import { Since } from '@site/src/components';
11
11
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.
13
13
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:
15
15
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
17
18
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.
19
20
20
21
## Installation
21
22
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:
23
24
24
25
```console
25
26
composer global require moodlehq/moodle-cs
26
27
```
27
28
29
+
This ensures that you have a single copy of the standard used for all code.
@@ -33,6 +36,52 @@ A PHPCS configuration is included in the Moodle codebase and ensures that the co
33
36
34
37
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.
35
38
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
+
<rulesetname="MoodleCore">
44
+
<ruleref="./phpcs.xml"/>
45
+
<ruleref="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
+
<rulesetname="MoodleCore">
60
+
<ruleref="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
+
<rulesetname="MoodleCore">
69
+
<ruleref="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
+
36
85
#### Community plugins, and older Moodle versions
37
86
38
87
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