|
16 | 16 |
|
17 | 17 | ## Information |
18 | 18 |
|
19 | | -This repository contains the Moodle Coding Style configuration. |
| 19 | +This repository contains the Moodle Coding Style configurations, written as [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) rulesets. |
20 | 20 |
|
21 | | -Currently this only includes the configuration for PHP Coding style, but this |
22 | | -may be extended to include custom rules for JavaScript, and any other supported |
23 | | -languages or syntaxes. |
| 21 | +Two coding styles are included: |
24 | 22 |
|
| 23 | +- `moodle` - the main ruleset for the [Moodle Coding Style](https://moodledev.io/general/development/policies/codingstyle) |
| 24 | +- `moodle-extra` - extended ruleset which includes recommended best practices |
| 25 | + - extends the main `moodle` ruleset |
| 26 | + |
| 27 | +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. |
25 | 28 |
|
26 | 29 | ## Installation |
27 | 30 |
|
28 | | -### Using Composer |
| 31 | +### Using Composer (recommended) |
29 | 32 |
|
30 | | -You can include these coding style rules using Composer to make them available |
31 | | -globally across your system. |
| 33 | +You can install these coding style rules using Composer to make them available globally across your system. |
32 | 34 |
|
33 | | -This will install the correct version of phpcs, with the Moodle rules, and their |
34 | | -dependencies. |
| 35 | +This will install the correct version of phpcs, with the Moodle rules, and their dependencies. |
35 | 36 |
|
36 | | -``` |
| 37 | +```shell |
37 | 38 | composer global require moodlehq/moodle-cs |
38 | 39 | ``` |
39 | 40 |
|
40 | | -### As a part of moodle-local_codechecker |
41 | | - |
42 | | -This plugin is included as part of the [moodle-local_codechecker |
43 | | -plugin](https://github.com/moodlehq/moodle-local_codechecker). |
44 | | - |
45 | | - |
46 | 41 | ## Configuration |
47 | 42 |
|
48 | | -You can set the Moodle standard as the system default: |
49 | | -``` |
50 | | -phpcs --config-set default_standard moodle |
51 | | -``` |
52 | | - |
53 | | -This will inform most IDEs automatically. |
54 | | -Alternatively you can configuration your IDE to use phpcs with the Moodle |
55 | | -ruleset as required. |
56 | | - |
| 43 | +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. |
57 | 44 |
|
58 | | -### IDE Integration |
| 45 | +Additional configuration can be generated automatically to have PHP CodeSniffer ignore any third-party library code. This can be generated by running: |
59 | 46 |
|
60 | | -#### PhpStorm |
61 | | - |
62 | | -1. Open PhpStorm preferences |
63 | | -2. Go to Inspections > PHP > PHP Code Sniffer Validation |
64 | | -3. In the 'coding standard' dropdown, select 'moodle' |
| 47 | +```shell |
| 48 | +npx grunt ignorefiles |
| 49 | +``` |
65 | 50 |
|
66 | | -#### Sublime Text |
| 51 | +### Using the `moodle-extra` coding style |
67 | 52 |
|
68 | | -Find documentation [here](https://docs.moodle.org/dev/Setting_up_Sublime2#Sublime_PHP_CS). |
| 53 | +The recommended way of configuring PHP CodeSniffer to use the `moodle-extra` coding style is to provide an additional configuration file. |
69 | 54 |
|
70 | | -1. Go in your Sublime Text to Preferences -> Package Control -> Package Control: Install Package |
71 | | -2. Write 'phpcs' in the search field, if you see Phpcs and SublimeLinter-phpcs, click on them to install them. |
72 | | -3. If not, check if they are already installed Preferences -> Package Control -> Package Control: Remove Package. |
73 | | -4. To set your codecheck to moodle standards go to Preferences -> Package Settings -> PHP Code Sniffer -> Settings-User and write: |
| 55 | +For Moodle 3.11 onwards you can create a file named `.phpcs.xml` with the following contents: |
74 | 56 |
|
75 | | - { "phpcs_additional_args": { |
76 | | - "--standard": "moodle", |
77 | | - "-n": " |
78 | | - }, |
79 | | - } |
| 57 | +```xml |
| 58 | +<?xml version="1.0" encoding="UTF-8"?> |
| 59 | +<ruleset name="MoodleCore"> |
| 60 | + <rule ref="./phpcs.xml"/> |
| 61 | + <rule ref="moodle-extra"/> |
| 62 | +</ruleset> |
| 63 | +``` |
80 | 64 |
|
81 | | -5. If you don’t have the auto-save plugin turned on, YOU’RE DONE! |
82 | | -6. If you have the auto-save plugin turned on, because the codecheck gets triggered on save, the quick panel will keep popping making it impossible to type. |
83 | | - To stop quick panel from showing go to Settings-User file and add: |
| 65 | +This will load the `phpcs.xml` file (generated by `npx grunt ignorefiles`), and apply the `moodle-extra` configuration on top. |
84 | 66 |
|
85 | | - "phpcs_show_quick_panel": false, |
| 67 | +### Moodle 3.10 and earlier |
86 | 68 |
|
87 | | - The line with the error will still get marked and if you’ll click on it you’ll see the error text in the status bar. |
| 69 | +The easiset way to have PHP CodeSniffer pick up your preferred style, you can create a file named `phpcs.xml` with the following contents: |
88 | 70 |
|
89 | | -#### VSCode |
| 71 | +```xml |
| 72 | +<?xml version="1.0" encoding="UTF-8"?> |
| 73 | +<ruleset name="MoodleCore"> |
| 74 | + <rule ref="moodle"/> |
| 75 | +</ruleset> |
| 76 | +``` |
90 | 77 |
|
91 | | -Find documentation [here](https://docs.moodle.org/dev/Setting_up_VSCode#PHP_CS). |
| 78 | +If you wish to use the `moodle-extra` coding style, then you can use the following content: |
92 | 79 |
|
93 | | -1. Install [PHPSniffer](https://marketplace.visualstudio.com/items?itemName=wongjn.php-sniffer). |
94 | | -2. Open VSCode settings.json and add the following setting to define standard PHP CS (if you haven't set it as default in your system): |
| 80 | +```xml |
| 81 | +<?xml version="1.0" encoding="UTF-8"?> |
| 82 | +<ruleset name="MoodleCore"> |
| 83 | + <rule ref="moodle-extra"/> |
| 84 | +</ruleset> |
| 85 | +``` |
95 | 86 |
|
96 | | - "phpSniffer.standard": "moodle", |
| 87 | +Note: Third-party library code will not be ignored with these versions of Moodle. |
0 commit comments