Skip to content

Commit

Permalink
Add the phpcs.xml dist file and some small adjustments
Browse files Browse the repository at this point in the history
Here we are introducing the phpcs.xml.dist file that will
guide our coding style within moodle-cs.

- It scans all the code but vendor and fixtures directories.
- It's the complete PSR12 standard but allowing multi line function
  declarations to have the opening curly bracket in the same line.
- With line length adjusted to 132 / 180.
- And a few more sniffs, not part of the PSR12, but good for
  readability (hopefully non-controversial):
  - && and || logical operators.
  - Short array syntax.
  - Space after commas.
  - Comma at the end of multi-line arrays.
  - PHPCompatibility standard to check for >=7.4 (with an exception
    about T_ENUM false positives).

And that's it!

Then, apart of the above, small adjustments to the composer.json
and .gitignore files.
  • Loading branch information
stronk7 committed Feb 24, 2024
1 parent e2fdd2f commit b5d09a6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
16 changes: 13 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
vendor
# Composer stuff
composer.lock
phpcs.xml
composer.phar

# PHP_CodeSniffer
.phpcs.xml
phpcs.xml
!phpcs.xml.dist

# PHPUnit
.phpunit.result.cache
phpunit.xml
.phplint-cache
clover.xml
.phpunit.cache

# PHPlin
phplint.cache

# IDEs and editors
.vscode
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
}
],
"require": {
"php": ">=7.4.0",
"ext-json": "*",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"squizlabs/php_codesniffer": "^3.9.0",
"phpcsstandards/phpcsextra": "^1.2.1",
Expand Down
46 changes: 46 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
<description>The coding rules configuration for the moodle-cs project.</description>

<!-- We don't want to analyse these -->
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/Tests/fixtures/*</exclude-pattern>
<exclude-pattern>*/Tests/*/fixtures/*</exclude-pattern>

<!-- PSR12 with a few exceptions and adjustments -->
<rule ref="PSR12">
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
</rule>

<!-- Some more chars don't hurt too much -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="132"/>
<property name="absoluteLineLimit" value="180"/>
</properties>
</rule>

<!-- We want to enforce && and || instead of and and or -->
<rule ref="Squiz.Operators.ValidLogicalOperators"/>

<!-- We want to use always the short array syntax -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>

<!-- We went better commas managing -->
<!-- Space after comma -->
<rule ref="moodle.WhiteSpace.SpaceAfterComma"/>
<!-- Comma at the end of multi-line arrays -->
<rule ref="NormalizedArrays.Arrays.CommaAfterLast">
<exclude name="NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine" />
</rule>

<!--PHPCompetibility configuration-->
<rule ref="./vendor/phpcompatibility/php-compatibility/PHPCompatibility/ruleset.xml"/>
<config name="testVersion" value="7.4-"/>

<!-- These are false positives because CodeSniffer creates them when needed.
TODO: Delete this once we raise minimum requirements to PHP 8.0. -->
<rule ref="PHPCompatibility.Constants.NewConstants">
<exclude name="PHPCompatibility.Constants.NewConstants.newConstants.t_enumFound"/>
</rule>
</ruleset>

0 comments on commit b5d09a6

Please sign in to comment.