Skip to content

Commit

Permalink
Convert phpcs tests to their own repository
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Feb 28, 2022
1 parent 73cebc6 commit 72bc372
Show file tree
Hide file tree
Showing 52 changed files with 1,475 additions and 144 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# The generic_files_linendings test is checking for crlf line endings and warns about them.
# This must be specified as a .gitattribute to prevent git updating it to be LF on commit.
/phpcs/moodle/tests/fixtures/generic_files_lineendings.php eol=crlf
49 changes: 49 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Moodle Coding Style Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
include:
- php: 8.1
- php: 8.0
- php: 7.4
steps:
- name: Check out repository code
uses: actions/checkout@v2

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ matrix.extensions }}
ini-values: pcov.directory=phpcs/moodle
coverage: pcov
tools: composer, phpcpd

- name: Install composer dependencies
run: |
composer install
- name: Run phplint
if: ${{ always() }}
run: |
./vendor/bin/phplint
- name: PHP Copy/Paste Detector
continue-on-error: true # This step will show errors but will not fail
if: ${{ always() }}
run: phpcpd --exclude phpcs/moodle/tests phpcs/moodle

- name: Run phpunit
if: ${{ always() }}
run: |
./vendor/bin/phpunit --coverage-text
- name: Test coverage
run: ./vendor/bin/phpunit-coverage-check -t 80 clover.xml
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vendor
composer.lock
phpcs.xml
.phpcs.xml
.phpunit.result.cache
phpunit.xml
.phplint-cache
clover.xml
.phpunit.cache
.vscode
4 changes: 4 additions & 0 deletions .phplint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
path: ./
exclude:
- vendor
- phpcs/moodle/tests/fixtures
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Moodle Coding Style

<div aria-hidden="true">

[![Latest Stable Version](https://poser.pugx.org/moodlehq/moodle-cs/v/stable)](https://packagist.org/packages/moodlehq/moodle-cs)
[![Release Date of the Latest Version](https://img.shields.io/github/release-date/moodlehq/moodle-cs.svg?maxAge=1800)](https://github.com/moodlehq/moodle-cs/releases)
:construction:

[![Unit Tests](https://github.com/moodlehq/moodle-cs/actions/workflows/phpcs.yml/badge.svg)](https://github.com/moodlehq/moodle-cs/actions/workflows/phpcs.yml)

[![License](https://poser.pugx.org/moodlehq/moodle-cs/license)](https://github.com/moodlehq/moodle-cs/blob/main/LICENSE)
[![Total Downloads](https://poser.pugx.org/moodlehq/moodle-cs/downloads)](https://packagist.org/packages/moodlehq/moodle-cs/stats)
[![Number of Contributors](https://img.shields.io/github/contributors/moodlehq/moodle-cs.svg?maxAge=3600)](https://github.com/moodlehq/moodle-cs/graphs/contributors)

</div>

## Information

This repository contains the Moodle Coding Style configuration.

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.


## Installation

### Using Composer

You can include these coding style rules using Composer to make them available
globally across your system.

This will install the correct version of phpcs, with the Moodle rules, and their
dependencies.

```
composer global require moodlehq/moodle-cs
```

### As a part of moodle-local_codechecker

This plugin is included as part of the [moodle-local_codechecker
plugin](https://github.com/moodlehq/moodle-local_codechecker).


## Configuration

You can set the Moodle standard as the system default:
```
phpcs --config-set default_standard moodle
```

This will inform most IDEs automatically.
Alternatively you can configuration your IDE to use phpcs with the Moodle
ruleset as required.


### IDE Integration

#### PhpStorm

1. Open PhpStorm preferences
2. Go to Inspections > PHP > PHP Code Sniffer Validation
3. In the 'coding standard' dropdown, select 'moodle'

#### Sublime Text

Find documentation [here](https://docs.moodle.org/dev/Setting_up_Sublime2#Sublime_PHP_CS).

1. Go in your Sublime Text to Preferences -> Package Control -> Package Control: Install Package
2. Write 'phpcs' in the search field, if you see Phpcs and SublimeLinter-phpcs, click on them to install them.
3. If not, check if they are already installed Preferences -> Package Control -> Package Control: Remove Package.
4. To set your codecheck to moodle standards go to Preferences -> Package Settings -> PHP Code Sniffer -> Settings-User and write:

{ "phpcs_additional_args": {
"--standard": "moodle",
"-n": "
},
}

5. If you don’t have the auto-save plugin turned on, YOU’RE DONE!
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.
To stop quick panel from showing go to Settings-User file and add:

"phpcs_show_quick_panel": false,

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.

#### VSCode

Find documentation [here](https://docs.moodle.org/dev/Setting_up_VSCode#PHP_CS).

1. Install [PHPSniffer](https://marketplace.visualstudio.com/items?itemName=wongjn.php-sniffer).
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):

"phpSniffer.standard": "moodle",
54 changes: 54 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "moodlehq/moodle-cs",
"type": "phpcodesniffer-standard",
"description": "Moodle Coding Sniffer rules",
"keywords": [
"phpcs",
"standards",
"WordPress"
],
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Andrew Lyons",
"email": "andrew@nicols.co.uk"
}
],
"require": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
"squizlabs/php_codesniffer": "^3.6",
"phpcompatibility/php-compatibility": "^9.3"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"install-codestandards": [
"Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run"
]
},
"support": {
"issues": "https://github.com/moodlehq/moodle-cs/issues",
"wiki": "https://github.com/moodlehq/moodle-cs/wiki",
"source": "https://github.com/moodlehq/moodle-cs"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"mikey179/vfsstream": "^1.6",
"overtrue/phplint": "^3.0",
"phpmd/phpmd": "^2.11",
"thor-juhasz/phpunit-coverage-check": "^0.3.0"
},
"replace": {
"moodlehq/moodle-local_codechecker": "3.1.0"
},
"autoload": {
"psr-4": {
"MoodleHQ\\MoodleCS\\moodle\\": "phpcs/moodle/"
}
}
}
4 changes: 2 additions & 2 deletions phpcs/moodle/Sniffs/Commenting/InlineCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace MoodleCodeSniffer\moodle\Sniffs\Commenting;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting;

// phpcs:disable moodle.NamingConventions

Expand Down Expand Up @@ -459,7 +459,7 @@ public function process(File $phpcsFile, $stackPtr) {
// <rule ref="moddle.Commenting.InlineComment.SpacingAfter">
// <severity>0</severity>
// </rule>
// But it doesn't work anymore with PHPCS3.x (neither with "moodle" or "MoodleCodeSniffer"),
// But it doesn't work anymore with PHPCS3.x (neither with "moodle" or "MoodleHQ\MoodleCS\moodle"),
// it seems tha there is a problem if the 2 names don't match.
// Hence, unreachable. Ugly but ok, so far. Don't add any custom code below.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\ControlStructures;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\ControlStructures;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/Files/BoilerplateCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\Files;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Files;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/Files/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\Files;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Files;

use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff as GenericLineLengthSniff;
use PHP_CodeSniffer\Sniffs\Sniff;
Expand Down
6 changes: 3 additions & 3 deletions phpcs/moodle/Sniffs/Files/MoodleInternalSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\Files;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Files;

// phpcs:disable moodle.NamingConventions

use PHP_CodeSniffer\Sniffs\Sniff;
use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use MoodleCodeSniffer\moodle\Util\MoodleUtil;

class MoodleInternalSniff implements Sniff {
/**
Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/Files/RequireLoginSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\Files;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Files;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\NamingConventions;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\NamingConventions;

// phpcs:disable moodle.NamingConventions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\NamingConventions;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\NamingConventions;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/PHP/DeprecatedFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace MoodleCodeSniffer\moodle\Sniffs\PHP;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/PHP/ForbiddenFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace MoodleCodeSniffer\moodle\Sniffs\PHP;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/PHP/ForbiddenGlobalUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\PHP;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/PHP/ForbiddenTokensSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\PHP;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/PHP/IncludingFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodleCodeSniffer\moodle\Sniffs\PHP;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down
2 changes: 1 addition & 1 deletion phpcs/moodle/Sniffs/PHP/MemberVarScopeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
*/

namespace MoodleCodeSniffer\moodle\Sniffs\PHP;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down
4 changes: 2 additions & 2 deletions phpcs/moodle/Sniffs/PHPUnit/TestCaseCoversSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace MoodleCodeSniffer\moodle\Sniffs\PHPUnit;
namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHPUnit;

// phpcs:disable moodle.NamingConventions

use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use MoodleCodeSniffer\moodle\Util\MoodleUtil;

/**
* Checks that a test file has the @coversxxx annotations properly defined.
Expand Down
Loading

0 comments on commit 72bc372

Please sign in to comment.