Skip to content

Commit 3a33e5f

Browse files
authored
Merge branch 'main' into forbid-unserialize
2 parents 4353592 + 76ce969 commit 3a33e5f

File tree

50 files changed

+1792
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1792
-147
lines changed

.phplint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ path: ./
22
exclude:
33
- vendor
44
- moodle/Tests/fixtures
5+
- moodle/Tests/Sniffs/Namespaces/fixtures

CHANGELOG.md

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

88
## [Unreleased]
99

10+
## [v3.3.6] - 2023-09-15
11+
### Added
12+
- A new `moodle-extra` coding standard which moves towards a more PSR-12 compliant coding style.
13+
- Enforce the use of the short array syntax (`[]`), warning about the long alternative (`array()`): `Generic.Arrays.DisallowLongArraySyntax`. This will be raised from `warning` to `error` in 1 year.
14+
15+
## [v3.3.5] - 2023-08-28
16+
### Changed
17+
- Update composer dependencies to current versions, notably PHPCompatibility (0a17f9ed).
18+
- Enforce the use of `&&` and `||` logical operators, **now erroring** (after a grace period of 1 year) with `and` and `or` uses: `Squiz.Operators.ValidLogicalOperators`
19+
1020
## [v3.3.4] - 2023-05-28
1121
### Changed
1222
- Update composer dependencies to current versions, notably PHPCompatibility (70e4ca24).
@@ -51,7 +61,9 @@ All features are maintained and no new features have been introduced to either t
5161

5262
All the details about [previous releases] can be found in [local_codechecker](https://github.com/moodlehq/moodle-local_codechecker) own change log.
5363

54-
[Unreleased]: https://github.com/moodlehq/moodle-cs/compare/v3.3.4...main
64+
[Unreleased]: https://github.com/moodlehq/moodle-cs/compare/v3.3.6...main
65+
[v3.3.6]: https://github.com/moodlehq/moodle-cs/compare/v3.3.5...v3.3.6
66+
[v3.3.5]: https://github.com/moodlehq/moodle-cs/compare/v3.3.4...v3.3.5
5567
[v3.3.4]: https://github.com/moodlehq/moodle-cs/compare/v3.3.3...v3.3.4
5668
[v3.3.3]: https://github.com/moodlehq/moodle-cs/compare/v3.3.2...v3.3.3
5769
[v3.3.2]: https://github.com/moodlehq/moodle-cs/compare/v3.3.1...v3.3.2

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
{
1313
"name": "Andrew Lyons",
1414
"email": "andrew@nicols.co.uk"
15+
},
16+
{
17+
"name": "Eloy Lafuente",
18+
"email": "stronk7@moodle.com"
1519
}
1620
],
1721
"require": {
1822
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
1923
"squizlabs/php_codesniffer": "^3.7.2",
20-
"phpcompatibility/php-compatibility": "dev-develop#70e4ca24"
24+
"phpcsstandards/phpcsextra": "^1.1.0",
25+
"phpcompatibility/php-compatibility": "dev-develop#0a17f9ed"
2126
},
2227
"config": {
2328
"allow-plugins": {

moodle-extra/ruleset.xml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
<!-- This is an error in moodle-extra. TODO: Remove as part of #58 -->
13+
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
14+
<type>error</type>
15+
</rule>
16+
17+
<!-- Include the PSR-12 ruleset with relevant Moodle exclusions -->
18+
<rule ref="PSR12">
19+
20+
<!-- Moodle has a header manager and places its copyright on the first line after the opening tag -->
21+
<exclude name="PSR12.Files.FileHeader.SpacingAfterBlock"/>
22+
23+
<!-- Moodle already defines its own line length, so remove this from the PSR-12 standard -->
24+
<exclude name="Generic.Files.LineLength.TooLong"/>
25+
26+
<!-- Moodle has its own custom sniff for side effects -->
27+
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
28+
29+
<!-- Moodle does not support camel case at all -->
30+
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
31+
32+
<!-- Moodle contains a lot of code which pre-dates PHP 7.1 and did not support constant visibility -->
33+
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
34+
35+
<!-- Moodle does not place the opening brace on a new line -->
36+
<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/>
37+
38+
<!-- Moodle allows use of else if-->
39+
<exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed"/>
40+
41+
<!-- Moodle casing rules do not allow camel case at all -->
42+
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
43+
44+
<!-- Moodle casing currently places the brace in the same line -->
45+
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
46+
</rule>
47+
48+
<!-- Detect duplicate array keys -->
49+
<rule ref="Universal.Arrays.DuplicateArrayKey"/>
50+
51+
<!-- Disallow use of list() instead of [] -->
52+
<rule ref="Universal.Lists.DisallowLongListSyntax"/>
53+
54+
<!-- Enusre that ::class is lower-cased -->
55+
<rule ref="Universal.Constants.LowercaseClassResolutionKeyword"/>
56+
57+
<!-- Require a consistent modifier keyword order for OO constant declarations -->
58+
<rule ref="Universal.Constants.ModifierKeywordOrder"/>
59+
60+
<!-- Enforce that the names used in a class/enum "implements" statement or an interface "extends" statement are listed in alphabetic order -->
61+
<rule ref="Universal.OOStructures.AlphabeticExtendsImplements"/>
62+
63+
<!-- 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 -->
64+
<rule ref="Universal.UseStatements.KeywordSpacing"/>
65+
66+
<!-- Enforce lowercase function/const -->
67+
<rule ref="Universal.UseStatements.LowercaseFunctionConst"/>
68+
69+
<!-- Detect useless class imports (aliases) -->
70+
<rule ref="Universal.UseStatements.NoUselessAliases"/>
71+
72+
<!-- Enfore comma, spacing, like, this -->
73+
<rule ref="Universal.WhiteSpace.CommaSpacing"/>
74+
75+
<!--
76+
TODO
77+
78+
PER-2.0 support (https://github.com/squizlabs/PHP_CodeSniffer/issues/3793), including:
79+
- Trailing commas in function parameters https://github.com/squizlabs/PHP_CodeSniffer/issues/2030
80+
- Multi-line implements/extends changes
81+
82+
-->
83+
84+
<!--
85+
Detect issues with Unit Test dataProviders:
86+
- private providers
87+
- providers which do not exist
88+
- providers whose name is prefixed with _test
89+
- incorrect casing of dataProvider
90+
- dataProviders which do not return an array or Iterable
91+
- dataProviders which can be converted to a static method (PHPUnit 10 compatibility)
92+
-->
93+
<rule ref="moodle.PHPUnit.TestCaseProvider">
94+
<properties>
95+
<property name="autofixStaticProviders" value="true"/>
96+
</properties>
97+
</rule>
98+
99+
</ruleset>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Checks that each file contains the standard GPL comment.
19+
*
20+
* @package moodle-cs
21+
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Namespaces;
26+
27+
use PHP_CodeSniffer\Sniffs\Sniff;
28+
use PHP_CodeSniffer\Files\File;
29+
use PHP_CodeSniffer\Util\Tokens;
30+
31+
// phpcs:disable moodle.NamingConventions
32+
33+
class NamespaceStatementSniff implements Sniff {
34+
public function register()
35+
{
36+
return [
37+
T_NAMESPACE,
38+
];
39+
}
40+
41+
public function process(File $file, $stackPtr)
42+
{
43+
$tokens = $file->getTokens();
44+
// Format should be:
45+
// - T_NAMESPACE
46+
// - T_WHITESPACE
47+
// - T_STRING
48+
49+
$checkPtr = $stackPtr + 2;
50+
$token = $tokens[$checkPtr];
51+
if ($token['code'] === T_NS_SEPARATOR) {
52+
$fqdn = '';
53+
$stop = $file->findNext(Tokens::$emptyTokens, ($stackPtr + 2));
54+
for ($i = $stackPtr + 2; $i < $stop; $i++) {
55+
$fqdn .= $tokens[$i]['content'];
56+
}
57+
$fix = $file->addFixableError(
58+
'Namespace should not start with a slash: %s',
59+
$checkPtr,
60+
'LeadingSlash',
61+
[$fqdn]
62+
);
63+
64+
if ($fix) {
65+
$file->fixer->beginChangeset();
66+
$file->fixer->replaceToken($checkPtr, '');
67+
$file->fixer->endChangeset();
68+
}
69+
}
70+
}
71+
}

moodle/Sniffs/PHPUnit/TestCaseCoversSniff.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ public function process(File $file, $pointer) {
4949

5050
// Before starting any check, let's look for various things.
5151

52-
// Get the moodle branch being analysed.
53-
$moodleBranch = MoodleUtil::getMoodleBranch($file);
52+
// If we aren't checking Moodle 4.0dev (400) and up, nothing to check.
53+
// Make and exception for codechecker phpunit tests, so they are run always.
54+
if (!MoodleUtil::meetsMinimumMoodleVersion($file, 400) && !MoodleUtil::isUnitTestRunning()) {
55+
return; // @codeCoverageIgnore
56+
}
5457

55-
// Detect if we are running PHPUnit.
56-
$runningPHPUnit = defined('PHPUNIT_TEST') && PHPUNIT_TEST;
58+
// If the file is not a unit test file, nothing to check.
59+
if (!MoodleUtil::isUnitTest($file) && !MoodleUtil::isUnitTestRunning()) {
60+
return; // @codeCoverageIgnore
61+
}
5762

5863
// We have all we need from core, let's start processing the file.
5964

@@ -70,24 +75,6 @@ public function process(File $file, $pointer) {
7075
return; // @codeCoverageIgnore
7176
}
7277

73-
// If we aren't checking Moodle 4.0dev (400) and up, nothing to check.
74-
// Make and exception for codechecker phpunit tests, so they are run always.
75-
if (isset($moodleBranch) && $moodleBranch < 400 && !$runningPHPUnit) {
76-
return; // @codeCoverageIgnore
77-
}
78-
79-
// If the file isn't under tests directory, nothing to check.
80-
if (stripos($file->getFilename(), '/tests/') === false) {
81-
return; // @codeCoverageIgnore
82-
}
83-
84-
// If the file isn't called, _test.php, nothing to check.
85-
// Make an exception for codechecker own phpunit fixtures here, allowing any name for them.
86-
$fileName = basename($file->getFilename());
87-
if (substr($fileName, -9) !== '_test.php' && !$runningPHPUnit) {
88-
return; // @codeCoverageIgnore
89-
}
90-
9178
// Iterate over all the classes (hopefully only one, but that's not this sniff problem).
9279
$cStart = $pointer;
9380
while ($cStart = $file->findNext(T_CLASS, $cStart + 1)) {

0 commit comments

Comments
 (0)