Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt run.php to the new phpcs3 API #90

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions classes/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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().
*/
Expand All @@ -113,26 +131,27 @@ 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);

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();
}
}
31 changes: 20 additions & 11 deletions run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
vmdef marked this conversation as resolved.
Show resolved Hide resolved
}
// PHPCompatibility autoloading.
require_once('PHPCSAliases.php');
vmdef marked this conversation as resolved.
Show resolved Hide resolved

// 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(
Expand All @@ -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();