From a1474c66294f6fb1d05154d824961695f24f0132 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 23 Oct 2020 13:02:01 +0200 Subject: [PATCH] Adapt run.php to the new phpcs3 API --- classes/runner.php | 31 +++++++++++++++++++++++++------ run.php | 31 ++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/classes/runner.php b/classes/runner.php index bf9540e4..5247d6f9 100644 --- a/classes/runner.php +++ b/classes/runner.php @@ -72,7 +72,7 @@ public function set_includewarnings($includewarnings) { } /** - * Set the files the runned is going to process. + * Set the files the runner is going to process. * * @param string[] $files array of full paths to files or directories. */ @@ -89,6 +89,24 @@ public function set_ignorepatterns($ignorepatterns) { $this->config->ignored = $ignorepatterns; } + /** + * Set the verbosity for the output. + * + * @param int $verbosity How verbose the output should be. Check expected values in {@see PHP_CodeSniffer\Config}. + */ + public function set_verbosity(int $verbosity): void { + $this->config->verbosity = $verbosity; + } + + /** + * Set if the interactive checking mode should be enabled or not. + * + * @param bool $interactive If true, will stop after each file with errors and wait for user input. + */ + public function set_interactive(bool $interactive): void { + $this->config->interactive = $interactive; + } + /** * Initialise the runner, invoked by run(). */ @@ -113,7 +131,7 @@ public function run() { $this->init(); // Create the reporter to manage all the reports from the run. - $reporter = new \PHP_CodeSniffer\Reporter($this->config); + $this->reporter = new \PHP_CodeSniffer\Reporter($this->config); // And build the file list to iterate over. $todo = new \PHP_CodeSniffer\Files\FileList($this->config, $this->ruleset); @@ -121,18 +139,19 @@ public function run() { foreach ($todo as $file) { if ($file->ignored === false) { try { - $file->process(); + $this->processFile($file); + } catch (\PHP_CodeSniffer\Exceptions\DeepExitException $e) { + echo $e->getMessage(); + return $e->getCode(); } catch (\Exception $e) { $error = 'Problem during processing; checking has been aborted. The error message was: '.$e->getMessage(); $file->addErrorOnLine($error, 1, 'Internal.Exception'); } - // Add results to the reporter and free memory. - $reporter->cacheFileReport($file, $this->config); $file->cleanUp(); } } // Have finished, generate the final reports. - $reporter->printReports(); + $this->reporter->printReports(); } } diff --git a/run.php b/run.php index 93a08c36..254aca33 100644 --- a/run.php +++ b/run.php @@ -26,8 +26,18 @@ require(dirname(__FILE__) . '/../../config.php'); require_once($CFG->libdir . '/clilib.php'); -require_once($CFG->dirroot . '/local/codechecker/locallib.php'); +// PHP_Codesniffer autoloading. +if (is_file(__DIR__ . '/phpcs/autoload.php') === true) { + include_once(__DIR__ . '/phpcs/autoload.php'); +} else { + include_once('PHP/CodeSniffer/autoload.php'); +} +// PHPCompatibility autoloading. +require_once('PHPCSAliases.php'); + +// Own stuff (TODO: Some day all these will be moved to classes). +require_once($CFG->dirroot . '/local/codechecker/locallib.php'); // Get the command-line options. list($options, $unrecognized) = cli_get_params( @@ -52,14 +62,13 @@ raise_memory_limit(MEMORY_HUGE); -$standard = $CFG->dirroot . str_replace('/', DIRECTORY_SEPARATOR, '/local/codechecker/moodle'); +// Initialize and run the CS. +$runner = new \local_codechecker\runner(); +$runner->set_verbosity(1); +$runner->set_interactive($interactive); +$runner->set_ignorepatterns(local_codesniffer_get_ignores()); + +$fullpath = local_codechecker_clean_path($CFG->dirroot . '/' . trim($path, '/')); +$runner->set_files([$fullpath]); -$cli = new local_codechecker_codesniffer_cli(); -$phpcs = new PHP_CodeSniffer(1, 0, 'utf-8', $interactive); -$phpcs->setAllowedFileExtensions(['php']); // We are only going to process php files ever. -$phpcs->setCli($cli); -$phpcs->setIgnorePatterns(local_codesniffer_get_ignores()); -$phpcs->process(local_codechecker_clean_path( - $CFG->dirroot . '/' . trim($path, '/')), - local_codechecker_clean_path($standard)); -$phpcs->reporting->printReport('full', false, $cli->getCommandLineValues(), null); +$runner->run();