|
| 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 runned 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 | + * Initialise the runner, invoked by run(). |
| 94 | + */ |
| 95 | + public function init() { |
| 96 | + |
| 97 | + $this->config->standards = ['moodle']; |
| 98 | + $this->config->extensions = ['php' => 'PHP']; |
| 99 | + |
| 100 | + // Added all our customizations, finally call parent. |
| 101 | + parent::init(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Runs a codechecker execution. |
| 106 | + * |
| 107 | + * Instead of using the upstream runner, we just use this reduced version |
| 108 | + * that simplifies all the configuration, init and processing to suit |
| 109 | + * codechecker simpler needs from the UI. |
| 110 | + */ |
| 111 | + public function run() { |
| 112 | + // Setup everything. |
| 113 | + $this->init(); |
| 114 | + |
| 115 | + // Create the reporter to manage all the reports from the run. |
| 116 | + $reporter = new \PHP_CodeSniffer\Reporter($this->config); |
| 117 | + |
| 118 | + // And build the file list to iterate over. |
| 119 | + $todo = new \PHP_CodeSniffer\Files\FileList($this->config, $this->ruleset); |
| 120 | + |
| 121 | + foreach ($todo as $file) { |
| 122 | + if ($file->ignored === false) { |
| 123 | + try { |
| 124 | + $file->process(); |
| 125 | + } catch (\Exception $e) { |
| 126 | + $error = 'Problem during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
| 127 | + $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
| 128 | + } |
| 129 | + // Add results to the reporter and free memory. |
| 130 | + $reporter->cacheFileReport($file, $this->config); |
| 131 | + $file->cleanUp(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Have finished, generate the final reports. |
| 136 | + $reporter->printReports(); |
| 137 | + } |
| 138 | +} |
0 commit comments