Skip to content

Commit a1474c6

Browse files
committed
Adapt run.php to the new phpcs3 API
1 parent c0d12b9 commit a1474c6

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

classes/runner.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function set_includewarnings($includewarnings) {
7272
}
7373

7474
/**
75-
* Set the files the runned is going to process.
75+
* Set the files the runner is going to process.
7676
*
7777
* @param string[] $files array of full paths to files or directories.
7878
*/
@@ -89,6 +89,24 @@ public function set_ignorepatterns($ignorepatterns) {
8989
$this->config->ignored = $ignorepatterns;
9090
}
9191

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+
92110
/**
93111
* Initialise the runner, invoked by run().
94112
*/
@@ -113,26 +131,27 @@ public function run() {
113131
$this->init();
114132

115133
// Create the reporter to manage all the reports from the run.
116-
$reporter = new \PHP_CodeSniffer\Reporter($this->config);
134+
$this->reporter = new \PHP_CodeSniffer\Reporter($this->config);
117135

118136
// And build the file list to iterate over.
119137
$todo = new \PHP_CodeSniffer\Files\FileList($this->config, $this->ruleset);
120138

121139
foreach ($todo as $file) {
122140
if ($file->ignored === false) {
123141
try {
124-
$file->process();
142+
$this->processFile($file);
143+
} catch (\PHP_CodeSniffer\Exceptions\DeepExitException $e) {
144+
echo $e->getMessage();
145+
return $e->getCode();
125146
} catch (\Exception $e) {
126147
$error = 'Problem during processing; checking has been aborted. The error message was: '.$e->getMessage();
127148
$file->addErrorOnLine($error, 1, 'Internal.Exception');
128149
}
129-
// Add results to the reporter and free memory.
130-
$reporter->cacheFileReport($file, $this->config);
131150
$file->cleanUp();
132151
}
133152
}
134153

135154
// Have finished, generate the final reports.
136-
$reporter->printReports();
155+
$this->reporter->printReports();
137156
}
138157
}

run.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@
2626

2727
require(dirname(__FILE__) . '/../../config.php');
2828
require_once($CFG->libdir . '/clilib.php');
29-
require_once($CFG->dirroot . '/local/codechecker/locallib.php');
3029

30+
// PHP_Codesniffer autoloading.
31+
if (is_file(__DIR__ . '/phpcs/autoload.php') === true) {
32+
include_once(__DIR__ . '/phpcs/autoload.php');
33+
} else {
34+
include_once('PHP/CodeSniffer/autoload.php');
35+
}
36+
// PHPCompatibility autoloading.
37+
require_once('PHPCSAliases.php');
38+
39+
// Own stuff (TODO: Some day all these will be moved to classes).
40+
require_once($CFG->dirroot . '/local/codechecker/locallib.php');
3141

3242
// Get the command-line options.
3343
list($options, $unrecognized) = cli_get_params(
@@ -52,14 +62,13 @@
5262

5363
raise_memory_limit(MEMORY_HUGE);
5464

55-
$standard = $CFG->dirroot . str_replace('/', DIRECTORY_SEPARATOR, '/local/codechecker/moodle');
65+
// Initialize and run the CS.
66+
$runner = new \local_codechecker\runner();
67+
$runner->set_verbosity(1);
68+
$runner->set_interactive($interactive);
69+
$runner->set_ignorepatterns(local_codesniffer_get_ignores());
70+
71+
$fullpath = local_codechecker_clean_path($CFG->dirroot . '/' . trim($path, '/'));
72+
$runner->set_files([$fullpath]);
5673

57-
$cli = new local_codechecker_codesniffer_cli();
58-
$phpcs = new PHP_CodeSniffer(1, 0, 'utf-8', $interactive);
59-
$phpcs->setAllowedFileExtensions(['php']); // We are only going to process php files ever.
60-
$phpcs->setCli($cli);
61-
$phpcs->setIgnorePatterns(local_codesniffer_get_ignores());
62-
$phpcs->process(local_codechecker_clean_path(
63-
$CFG->dirroot . '/' . trim($path, '/')),
64-
local_codechecker_clean_path($standard));
65-
$phpcs->reporting->printReport('full', false, $cli->getCommandLineValues(), null);
74+
$runner->run();

0 commit comments

Comments
 (0)