Skip to content

Commit 05666d8

Browse files
committed
Merge branch 'phpcs3'
2 parents a361c1f + d3c7580 commit 05666d8

File tree

1,994 files changed

+121515
-86658
lines changed

Some content is hidden

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

1,994 files changed

+121515
-86658
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ script:
7676
- moodle-plugin-ci phplint
7777
- moodle-plugin-ci phpcpd
7878
- moodle-plugin-ci phpmd
79-
- moodle-plugin-ci codechecker
79+
# - moodle-plugin-ci codechecker # Heh, cannot check codechecker 3 with codechecker 2!
8080
- moodle-plugin-ci validate
8181
- moodle-plugin-ci savepoints
8282
- moodle-plugin-ci mustache
8383
- moodle-plugin-ci grunt
84+
- moodle-plugin-ci phpdoc
8485
- moodle-plugin-ci phpunit
8586
- moodle-plugin-ci behat

CHANGES.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
Changes in version 3.0.0 (20201102) - Welcome phpcs 3
2+
-----------------------------------------------------
3+
- Upgrade to PHP_CodeSniffer 3.5.8 (stronk7):
4+
- PHP_CodeSniffer move to phpcs directory (see readme_moodle.txt for complete instructions).
5+
- Move Moodle sniffs to PHP_CodeSniffer 3.
6+
- Create own runner/report.
7+
- Move as much as possible from locallib.php to classes.
8+
- Add PHPUnit 8 compatibility, keeping PHPUnit 6 working.
9+
- Min Moodle version required increases from 3.2 to 3.4.
10+
- [PR#90](https://github.com/moodlehq/moodle-local_codechecker/pull/90): Adapt custom PHP_CodeSniffer runner (classes/runner.php) to use PHP_CodeSniffer 3 API (Sara Arjona).
11+
- [PR#95](https://github.com/moodlehq/moodle-local_codechecker/pull/95): Unlock session before processing files (Víctor Déniz).
12+
113
Changes in version 2.9.8 (20201002) - Bye and thank you, phpcs 2.x
214
------------------------------------------------------------------
315
- [PR#83](https://github.com/moodlehq/moodle-local_codechecker/pull/83) and [PR#84](https://github.com/moodlehq/moodle-local_codechecker/pull/84): Allow a list of files to be checked (Sam Marshall).

PHPCSAliases.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
// phpcs:ignoreFile
3+
/**
4+
* PHPCompatibility, an external standard for PHP_CodeSniffer.
5+
*
6+
* PHPCS cross-version compatibility helper.
7+
*
8+
* @package local_codechecker
9+
* @copyright 2012-2020 PHPCompatibility Contributors
10+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
11+
* @link https://github.com/PHPCompatibility/PHPCompatibility
12+
*
13+
* @since 8.0.0
14+
*/
15+
16+
/*
17+
* Alias a number of PHPCS 3.x classes to their PHPCS 2.x equivalents.
18+
*
19+
* This file is auto-loaded by PHPCS 3.x before any sniffs are loaded
20+
* through the PHPCS 3.x `<autoload>` ruleset directive.
21+
*
22+
* {internal The PHPCS file have been reorganized in PHPCS 3.x, quite
23+
* a few "old" classes have been split and spread out over several "new"
24+
* classes. In other words, this will only work for a limited number
25+
* of classes.}
26+
*
27+
* {internal The `class_exists` wrappers are needed to play nice with other
28+
* external PHPCS standards creating cross-version compatibility in the same
29+
* manner.}
30+
*/
31+
if (defined('PHPCOMPATIBILITY_PHPCS_ALIASES_SET') === false) {
32+
if (interface_exists('\PHP_CodeSniffer_Sniff') === false) {
33+
class_alias('PHP_CodeSniffer\Sniffs\Sniff', '\PHP_CodeSniffer_Sniff');
34+
}
35+
if (class_exists('\PHP_CodeSniffer_File') === false) {
36+
class_alias('PHP_CodeSniffer\Files\File', '\PHP_CodeSniffer_File');
37+
}
38+
if (class_exists('\PHP_CodeSniffer_Tokens') === false) {
39+
class_alias('PHP_CodeSniffer\Util\Tokens', '\PHP_CodeSniffer_Tokens');
40+
}
41+
if (class_exists('\PHP_CodeSniffer_Exception') === false) {
42+
class_alias('PHP_CodeSniffer\Exceptions\RuntimeException', '\PHP_CodeSniffer_Exception');
43+
}
44+
if (class_exists('\PHP_CodeSniffer_Standards_AbstractScopeSniff') === false) {
45+
class_alias('PHP_CodeSniffer\Sniffs\AbstractScopeSniff', '\PHP_CodeSniffer_Standards_AbstractScopeSniff');
46+
}
47+
if (class_exists('\Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff') === false) {
48+
class_alias('PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff', '\Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff');
49+
}
50+
51+
define('PHPCOMPATIBILITY_PHPCS_ALIASES_SET', true);
52+
53+
/*
54+
* Register an autoloader.
55+
*
56+
* {internal When `installed_paths` is set via the ruleset, this autoloader
57+
* is needed to run the sniffs.
58+
* Upstream issue: {@link https://github.com/squizlabs/PHP_CodeSniffer/issues/1591} }
59+
*
60+
* @since 8.0.0
61+
*/
62+
spl_autoload_register(function ($class) {
63+
// Only try & load our own classes.
64+
if (stripos($class, 'PHPCompatibility') !== 0) {
65+
return;
66+
}
67+
68+
$file = realpath(__DIR__) . DIRECTORY_SEPARATOR . strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';
69+
70+
if (file_exists($file)) {
71+
include_once $file;
72+
}
73+
});
74+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ To install it using git, type this command in the root of your Moodle install:
2323

2424
Then add /local/codechecker to your git ignore.
2525

26-
Additionally, remember to only use the version of PHPCS located in ``pear/PHP/scripts/phpcs`` rather than installing PHPCS directly. Add the location of the PHPCS executable to your system path, tell PHPCS about the Moodle coding standard with ``phpcs --config-set installed_paths /path/to/moodle-local_codechecker`` and set the default coding standard to Moodle with ``phpcs --config-set default_standard moodle``. You can now test a file (or folder) with: ``phpcs /path/to/file.php``.
26+
Additionally, remember to only use the version of PHPCS located in ``phpcs/bin/phpcs`` rather than installing PHPCS directly. Add the location of the PHPCS executable to your system path, tell PHPCS about the Moodle coding standard with ``phpcs --config-set installed_paths /path/to/moodle-local_codechecker`` and set the default coding standard to Moodle with ``phpcs --config-set default_standard moodle``. You can now test a file (or folder) with: ``phpcs /path/to/file.php``.
2727

2828
Alternatively, download the zip from
2929
<https://github.com/moodlehq/moodle-local_codechecker/zipball/master>,

classes/report.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
* Custom PHP_CodeSniffer XML Report for local_codechecker.
19+
*
20+
* @package local_codechecker
21+
* @copyright 2020 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace local_codechecker;
26+
27+
// phpcs:disable moodle.NamingConventions
28+
29+
/**
30+
* Custom PHP_CodeSniffer XML Report for local_codechecker.
31+
*
32+
* This custom report is exactly the upstream XML one, but
33+
* with a few modifications when there aren't errors and warnings.
34+
*
35+
* @copyright 2020 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
36+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37+
*/
38+
class report extends \PHP_CodeSniffer\Reports\Xml {
39+
40+
/**
41+
* Generate a partial report for a single processed file.
42+
*
43+
* For files with violations delegate processing to parent class. For files
44+
* without violations, just return the plain <file> element, without any err/warn.
45+
*
46+
* @param array $report Prepared report data.
47+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being reported on.
48+
* @param boolean $showSources Show sources?
49+
* @param int $width Maximum allowed line width.
50+
*
51+
* @return boolean
52+
*/
53+
public function generateFileReport($report, \PHP_CodeSniffer\Files\File $phpcsFile, $showSources = false, $width = 80) {
54+
55+
// Report has violations, delegate to parent standard processing.
56+
if ($report['errors'] !== 0 || $report['warnings'] !== 0) {
57+
return parent::generateFileReport($report, $phpcsFile, $showSources, $width);
58+
}
59+
60+
// Here we are, with a file with 0 errors and warnings.
61+
$out = new \XMLWriter;
62+
$out->openMemory();
63+
$out->setIndent(true);
64+
65+
$out->startElement('file');
66+
$out->writeAttribute('name', $report['filename']);
67+
$out->writeAttribute('errors', $report['errors']);
68+
$out->writeAttribute('warnings', $report['warnings']);
69+
70+
$out->endElement();
71+
echo $out->flush();
72+
73+
return true;
74+
75+
}
76+
}

classes/runner.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
* Custom PHP_CodeSniffer Runner for local_codechecker.
19+
*
20+
* @package local_codechecker
21+
* @copyright 2020 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace local_codechecker;
26+
27+
/**
28+
* Custom PHP_CodeSniffer\Runner for local_codechecker.
29+
*
30+
* This custom runner just intercepts the init() method, to be able
31+
* to add all our configuration. The alternative to this is to play
32+
* with fake $_SERVER['argv' {@see PHP_CodeSniffer\Config}.
33+
*
34+
* @copyright 2020 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
35+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36+
*/
37+
class runner extends \PHP_CodeSniffer\Runner {
38+
39+
/**
40+
* Create an instance of the runner.
41+
*/
42+
public function __construct() {
43+
// Needed constant.
44+
if (defined('PHP_CODESNIFFER_CBF') === false) {
45+
define('PHP_CODESNIFFER_CBF', true);
46+
}
47+
// Pass the parallel as CLI, disabled. Note
48+
// this is to avoid some nasty argv notices.
49+
$this->config = new \PHP_CodeSniffer\Config([
50+
'--parallel=1',
51+
]);
52+
}
53+
54+
/**
55+
* Set the file where the XML report is going to be written
56+
*
57+
* @param string $reportfile file path to XML report.
58+
*/
59+
public function set_reportfile($reportfile) {
60+
$this->config->reports = [report::class => $reportfile];
61+
}
62+
63+
/**
64+
* Set if the report should include warnings or no.
65+
*
66+
* @param bool $includewarnings
67+
*/
68+
public function set_includewarnings($includewarnings) {
69+
if (empty($includewarnings)) {
70+
$this->config->warningSeverity = 0; // Disable warnings.
71+
}
72+
}
73+
74+
/**
75+
* Set the files the runner is going to process.
76+
*
77+
* @param string[] $files array of full paths to files or directories.
78+
*/
79+
public function set_files($files) {
80+
$this->config->files = $files;
81+
}
82+
83+
/**
84+
* Set all the patterns in file paths to be ignored
85+
*
86+
* @param string[] $ignorepatterns array of paths to ignore (libs, fixtures...)
87+
*/
88+
public function set_ignorepatterns($ignorepatterns) {
89+
$this->config->ignored = $ignorepatterns;
90+
}
91+
92+
/**
93+
* Set the verbosity for the output.
94+
*
95+
* @param int $verbosity How verbose the output should be. Check expected values in {@see PHP_CodeSniffer\Config}.
96+
*/
97+
public function set_verbosity(int $verbosity): void {
98+
$this->config->verbosity = $verbosity;
99+
}
100+
101+
/**
102+
* Set if the interactive checking mode should be enabled or not.
103+
*
104+
* @param bool $interactive If true, will stop after each file with errors and wait for user input.
105+
*/
106+
public function set_interactive(bool $interactive): void {
107+
$this->config->interactive = $interactive;
108+
}
109+
110+
/**
111+
* Initialise the runner, invoked by run().
112+
*/
113+
public function init() {
114+
115+
$this->config->standards = ['moodle'];
116+
$this->config->extensions = ['php' => 'PHP'];
117+
118+
// Added all our customizations, finally call parent.
119+
parent::init();
120+
}
121+
122+
/**
123+
* Runs a codechecker execution.
124+
*
125+
* Instead of using the upstream runner, we just use this reduced version
126+
* that simplifies all the configuration, init and processing to suit
127+
* codechecker simpler needs from the UI.
128+
*/
129+
public function run() {
130+
// Setup everything.
131+
$this->init();
132+
133+
// Create the reporter to manage all the reports from the run.
134+
$this->reporter = new \PHP_CodeSniffer\Reporter($this->config);
135+
136+
// And build the file list to iterate over.
137+
$todo = new \PHP_CodeSniffer\Files\FileList($this->config, $this->ruleset);
138+
139+
foreach ($todo as $file) {
140+
if ($file->ignored === false) {
141+
try {
142+
$this->processFile($file);
143+
} catch (\PHP_CodeSniffer\Exceptions\DeepExitException $e) {
144+
echo $e->getMessage();
145+
return $e->getCode();
146+
} catch (\Exception $e) {
147+
$error = 'Problem during processing; checking has been aborted. The error message was: '.$e->getMessage();
148+
$file->addErrorOnLine($error, 1, 'Internal.Exception');
149+
}
150+
$file->cleanUp();
151+
}
152+
}
153+
154+
// Have finished, generate the final reports.
155+
$this->reporter->printReports();
156+
}
157+
}

0 commit comments

Comments
 (0)