Skip to content

Commit ef04fab

Browse files
andrewnicolsstronk7
authored andcommitted
Add a new moodle-extra coding style
1 parent 57bd4db commit ef04fab

File tree

4 files changed

+134
-52
lines changed

4 files changed

+134
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- A new moodle-extra coding standard which moves towards a more PSR-12 compliant coding style
13+
1014
## [v3.3.5] - 2023-08-28
1115
### Changed
1216
- Update composer dependencies to current versions, notably PHPCompatibility (0a17f9ed).

README.md

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,72 @@
1616

1717
## Information
1818

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.
2020

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:
2422

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.
2528

2629
## Installation
2730

28-
### Using Composer
31+
### Using Composer (recommended)
2932

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.
3234

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.
3536

36-
```
37+
```shell
3738
composer global require moodlehq/moodle-cs
3839
```
3940

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-
4641
## Configuration
4742

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.
5744

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:
5946

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+
```
6550

66-
#### Sublime Text
51+
### Using the `moodle-extra` coding style
6752

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.
6954

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:
7456

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+
```
8064

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.
8466

85-
"phpcs_show_quick_panel": false,
67+
### Moodle 3.10 and earlier
8668

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:
8870

89-
#### VSCode
71+
```xml
72+
<?xml version="1.0" encoding="UTF-8"?>
73+
<ruleset name="MoodleCore">
74+
<rule ref="moodle"/>
75+
</ruleset>
76+
```
9077

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:
9279

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+
```
9586

96-
"phpSniffer.standard": "moodle",
87+
Note: Third-party library code will not be ignored with these versions of Moodle.

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
{
1313
"name": "Andrew Lyons",
1414
"email": "[email protected]"
15+
},
16+
{
17+
"name": "Eloy Lafuente",
18+
"email": "[email protected]"
1519
}
1620
],
1721
"require": {
1822
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
1923
"squizlabs/php_codesniffer": "^3.7.2",
24+
"phpcsstandards/phpcsextra": "^1.1.0",
2025
"phpcompatibility/php-compatibility": "dev-develop#0a17f9ed"
2126
},
2227
"config": {

moodle-extra/ruleset.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd"
4+
name="moodle-strict"
5+
namespace="MoodleHQ\MoodleCS\MoodleExtra">
6+
7+
<description>Best Practices for Moodle development beyond the core Coding Standards</description>
8+
9+
<!-- Extend the standard Moodle coding style -->
10+
<rule ref="moodle"/>
11+
12+
<!-- Include the PSR-12 ruleset with relevant Moodle exclusions -->
13+
<rule ref="PSR12">
14+
15+
<!-- Moodle has a header manager and places its copyright on the first line after the opening tag -->
16+
<exclude name="PSR12.Files.FileHeader.SpacingAfterBlock"/>
17+
18+
<!-- Moodle already defines its own line length, so remove this from the PSR-12 standard -->
19+
<exclude name="Generic.Files.LineLength.TooLong"/>
20+
21+
<!-- Moodle has its own custom sniff for side effects -->
22+
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
23+
24+
<!-- Moodle does not support camel case at all -->
25+
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
26+
27+
<!-- Moodle contains a lot of code which pre-dates PHP 7.1 and did not support constant visibility -->
28+
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
29+
30+
<!-- Moodle does not place the opening brace on a new line -->
31+
<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/>
32+
33+
<!-- Moodle allows use of else if-->
34+
<exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed"/>
35+
36+
<!-- Moodle casing rules do not allow camel case at all -->
37+
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
38+
39+
<!-- Moodle casing currently places the brace in the same line -->
40+
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
41+
</rule>
42+
43+
<!-- Detect duplicate array keys -->
44+
<rule ref="Universal.Arrays.DuplicateArrayKey"/>
45+
46+
<!-- Require a comma after the last element in a multi-line array, but prevent in a single-line array definition -->
47+
<rule ref="NormalizedArrays.Arrays.CommaAfterLast"/>
48+
49+
<!-- Disallow use of list() instead of [] -->
50+
<rule ref="Universal.Lists.DisallowLongListSyntax"/>
51+
52+
<!-- Enusre that ::class is lower-cased -->
53+
<rule ref="Universal.Constants.LowercaseClassResolutionKeyword"/>
54+
55+
<!-- Require a consistent modifier keyword order for OO constant declarations -->
56+
<rule ref="Universal.Constants.ModifierKeywordOrder"/>
57+
58+
<!-- Enforce that the names used in a class/enum "implements" statement or an interface "extends" statement are listed in alphabetic order -->
59+
<rule ref="Universal.OOStructures.AlphabeticExtendsImplements"/>
60+
61+
<!-- Enforce the use of a single space after the use, function, const keywords and both before and after the as keyword in import use statements -->
62+
<rule ref="Universal.UseStatements.KeywordSpacing"/>
63+
64+
<!-- Enforce lowercase function/const -->
65+
<rule ref="Universal.UseStatements.LowercaseFunctionConst"/>
66+
67+
<!-- Detect useless class imports (aliases) -->
68+
<rule ref="Universal.UseStatements.NoUselessAliases"/>
69+
70+
<!-- Enfore comma, spacing, like, this -->
71+
<rule ref="Universal.WhiteSpace.CommaSpacing"/>
72+
73+
<!--
74+
TODO
75+
76+
PER-2.0 support (https://github.com/squizlabs/PHP_CodeSniffer/issues/3793), including:
77+
- Trailing commas in function parameters https://github.com/squizlabs/PHP_CodeSniffer/issues/2030
78+
- Multi-line implements/extends changes
79+
80+
-->
81+
82+
</ruleset>

0 commit comments

Comments
 (0)