Skip to content

Commit 5664a7e

Browse files
authored
Merge pull request #742 from andrewnicols/phpcs_updates
Updates for phpcs documentation
2 parents 7164174 + 15ca009 commit 5664a7e

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

general/development/tools/phpcs.md

+50-43
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,9 +36,23 @@ 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

36-
#### Community plugins, and older Moodle versions
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:
3740

38-
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:
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:
3956

4057
```xml
4158
<?xml version="1.0" encoding="UTF-8"?>
@@ -44,48 +61,42 @@ If you are developing your own plugin outside of the main Moodle directory, or y
4461
</ruleset>
4562
```
4663

47-
If you do not wish to include this file in your repository, or you are using an older version of Moodle, then you should add this to your local gitignore files. On a Unix-like system this can be achieved with:
64+
If you wish to use the `moodle-extra` coding style, then you can use the following content:
4865

49-
```console
50-
echo phpcs.xml.dist >> .git/info/exclude
66+
```xml
67+
<?xml version="1.0" encoding="UTF-8"?>
68+
<ruleset name="MoodleCore">
69+
<rule ref="moodle-extra"/>
70+
</ruleset>
5171
```
5272

5373
:::info
5474

55-
The `.git/info/exclude` file is a per-repository version of the `.gitignore` file. Whilst `.gitignore` is tracked within the Moodle codebase and a version is shipped with Moodle, the `.git/info/exclude` file is local to your git clone.
56-
57-
See the [gitignore](https://git-scm.com/docs/gitignore) documentation for more information on the gitignore feature.
75+
Third-party library code will not be ignored with these versions of Moodle.
5876

5977
:::
6078

61-
#### System-side default standard
62-
63-
Although not recommended, you can configure the Moodle ruleset as the system-wide default for phpcs:
64-
65-
```php
66-
phpcs --config-set default_standard moodle
67-
```
68-
69-
:::important Not recommended
79+
:::tip Ignoring the file with Git
7080

71-
This approach is **not recommended** and is only preserved for reference.
81+
We recommend configuring your code checkout to ignore the `.phpcs.xml` file by adding a local ignore record to `.git/info/exclude`
7282

7383
:::
7484

75-
## Moodle plugin
76-
77-
Moodle includes a copy of the PHPCodeSniffer package, and the Moodle ruleset, as part of the [`moodle-local_codechecker`](https://github.com/moodlehq/moodle-local_codechecker) Moodle plugin. This makes the code checker available via a web-based interface for checking the syntax of a given file or folder.
85+
#### Community plugins, and older Moodle versions
7886

79-
One way to install this plugin is using `git clone`:
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:
8088

81-
```console
82-
git clone https://github.com/moodlehq/moodle-local_codechecker.git local/codechecker
89+
```xml
90+
<?xml version="1.0" encoding="UTF-8"?>
91+
<ruleset name="MoodleCore">
92+
<rule ref="moodle"/>
93+
</ruleset>
8394
```
8495

85-
It is recommended that you add the plugin to your _local_ git ignore:
96+
If you do not wish to include this file in your repository, or you are using an older version of Moodle, then you should add this to your local gitignore files. On a Unix-like system this can be achieved with:
8697

8798
```console
88-
echo local/codechecker >> .git/info/exclude
99+
echo phpcs.xml.dist >> .git/info/exclude
89100
```
90101

91102
:::info
@@ -96,23 +107,19 @@ See the [gitignore](https://git-scm.com/docs/gitignore) documentation for more i
96107

97108
:::
98109

99-
:::note
100-
101-
If you are not installing the moodle ruleset globally, and are instead using the [`local_codechecker`](https://github.com/moodlehq/moodle-local_codechecker) plugin, then you _must_ also use the version of phpcs distributed in the plugin.
102-
103-
This is located at `local/codechecker/phpcs/bin/phpcs`.
110+
#### System-side default standard
104111

105-
:::
112+
Although not recommended, you can configure the Moodle ruleset as the system-wide default for phpcs:
106113

107-
Once installed a new codechecker option will appear in the Site administration -> Development page.
114+
```php
115+
phpcs --config-set default_standard moodle
116+
```
108117

109-
This page allows for the code in a specified directory to be checked, for example if you wanted to check the code for the `shortanswer` question type you would enter
118+
:::important Not recommended
110119

111-
```
112-
/question/type/shortanswer
113-
```
120+
This approach is **not recommended** and is only preserved for reference.
114121

115-
You would then be presented with a list of the count of files processed and any warnings or errors.
122+
:::
116123

117124
## Editor integrations
118125

0 commit comments

Comments
 (0)