Skip to content

Commit d5f69a5

Browse files
committed
Add checkstyle support
1 parent 42a18d0 commit d5f69a5

File tree

7 files changed

+125
-20
lines changed

7 files changed

+125
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ For colored output install the suggested package `jakub-onderka/php-console-high
3030
- `--colors` Force enable colors in console output.
3131
- `--no-colors` Disable colors in console output.
3232
- `--no-progress` Disable progress in console output.
33+
- `--checkstyle` Output results as Checkstyle XML.
3334
- `--json` Output results as JSON string (require PHP 5.4).
3435
- `--blame` Try to show git blame for row with error.
3536
- `--git <git>` Path to Git executable to show blame message (default: 'git').

parallel-lint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function showOptions()
3232
--no-colors Disable colors in console output.
3333
--no-progress Disable progress in console output.
3434
--json Output results as JSON string.
35+
--checkstyle Output results as Checkstyle XML.
3536
--blame Try to show git blame for row with error.
3637
--git <git> Path to Git executable to show blame message (default: 'git').
3738
--stdin Load files and folder to test from standard input.
@@ -115,7 +116,7 @@ function showUsage()
115116
die(FAILED);
116117

117118
} catch (PhpParallelLint\Exception $e) {
118-
if (isset($settings) && $settings->json) {
119+
if (isset($settings) && $settings->format === PhpParallelLint\Settings::FORMAT_JSON) {
119120
echo json_encode($e);
120121
} else {
121122
echo $e->getMessage(), PHP_EOL;

src/Manager.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,22 @@ public function setOutput(Output $output)
104104
protected function getDefaultOutput(Settings $settings)
105105
{
106106
$writer = new ConsoleWriter;
107-
if ($settings->json) {
108-
return new JsonOutput($writer);
107+
switch ($settings->format) {
108+
case Settings::FORMAT_JSON:
109+
return new JsonOutput($writer);
110+
case Settings::FORMAT_CHECKSTYLE:
111+
return new CheckstyleOutput($writer);
112+
}
113+
114+
if ($settings->colors === Settings::DISABLED) {
115+
$output = new TextOutput($writer);
109116
} else {
110-
if ($settings->colors === Settings::DISABLED) {
111-
$output = new TextOutput($writer);
112-
} else {
113-
$output = new TextOutputColored($writer, $settings->colors);
114-
}
117+
$output = new TextOutputColored($writer, $settings->colors);
118+
}
115119

116-
$output->showProgress = $settings->showProgress;
120+
$output->showProgress = $settings->showProgress;
117121

118-
return $output;
119-
}
122+
return $output;
120123
}
121124

122125
/**

src/Output.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,82 @@ protected function phpVersionIdToString($phpVersionId)
332332
}
333333
}
334334

335+
class CheckstyleOutput implements Output
336+
{
337+
private $writer;
338+
339+
public function __construct(IWriter $writer)
340+
{
341+
$this->writer = $writer;
342+
}
343+
344+
public function ok()
345+
{
346+
}
347+
348+
public function skip()
349+
{
350+
}
351+
352+
public function error()
353+
{
354+
}
355+
356+
public function fail()
357+
{
358+
}
359+
360+
public function setTotalFileCount($count)
361+
{
362+
}
363+
364+
public function writeHeader($phpVersion, $parallelJobs, $hhvmVersion = null)
365+
{
366+
$this->writer->write('<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL);
367+
}
368+
369+
public function writeResult(Result $result, ErrorFormatter $errorFormatter, $ignoreFails)
370+
{
371+
$this->writer->write('<checkstyle>' . PHP_EOL);
372+
$errors = array();
373+
374+
foreach ($result->getErrors() as $error) {
375+
$message = $error->getMessage();
376+
if ($error instanceof SyntaxError) {
377+
$line = $error->getLine();
378+
$source = "Syntax Error";
379+
} else {
380+
$line = 1;
381+
$source = "Linter Error";
382+
}
383+
384+
$errors[$error->getShortFilePath()][] = array(
385+
'message' => $message,
386+
'line' => $line,
387+
'source' => $source
388+
);
389+
}
390+
391+
foreach ($errors as $file => $fileErrors) {
392+
$this->writer->write(sprintf(' <file name="%s">', $file) . PHP_EOL);
393+
foreach ($fileErrors as $fileError) {
394+
$this->writer->write(
395+
sprintf(
396+
' <error line="%d" severity="ERROR" message="%s" source="%s" />',
397+
$fileError['line'],
398+
$fileError['message'],
399+
$fileError['source']
400+
) .
401+
PHP_EOL
402+
);
403+
}
404+
$this->writer->write(' </file>' . PHP_EOL);
405+
}
406+
407+
$this->writer->write('</checkstyle>' . PHP_EOL);
408+
}
409+
}
410+
335411
class TextOutputColored extends TextOutput
336412
{
337413
/** @var \JakubOnderka\PhpConsoleColor\ConsoleColor */

src/Result.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(array $errors, array $checkedFiles, array $skippedFi
5959
}
6060

6161
/**
62-
* @return array
62+
* @return Error[]
6363
*/
6464
public function getErrors()
6565
{

src/Settings.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class Settings
4040
const DISABLED = 'DISABLED';
4141
const AUTODETECT = 'AUTODETECT';
4242

43+
const FORMAT_TEXT = 'text';
44+
const FORMAT_JSON = 'json';
45+
const FORMAT_CHECKSTYLE = 'checkstyle';
46+
4347
/**
4448
* Path to PHP executable
4549
* @var string
@@ -95,10 +99,10 @@ class Settings
9599
public $showProgress = true;
96100

97101
/**
98-
* Output results as JSON string
99-
* @var bool
102+
* Output format (see FORMAT_* constants)
103+
* @var string
100104
*/
101-
public $json = false;
105+
public $format = self::FORMAT_TEXT;
102106

103107
/**
104108
* Read files and folder to tests from standard input (blocking)
@@ -185,7 +189,11 @@ public static function parseArguments(array $arguments)
185189
break;
186190

187191
case '--json':
188-
$settings->json = true;
192+
$settings->format = self::FORMAT_JSON;
193+
break;
194+
195+
case '--checkstyle':
196+
$settings->format = self::FORMAT_CHECKSTYLE;
189197
break;
190198

191199
case '--git':

tests/Settings.parseArguments.phpt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
2727
$expectedSettings->excluded = array();
2828
$expectedSettings->colors = Settings::AUTODETECT;
2929
$expectedSettings->showProgress = true;
30-
$expectedSettings->json = false;
30+
$expectedSettings->format = Settings::FORMAT_TEXT;
3131

3232
Assert::equal($expectedSettings->phpExecutable, $settings->phpExecutable);
3333
Assert::equal($expectedSettings->shortTag, $settings->shortTag);
@@ -38,7 +38,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
3838
Assert::equal($expectedSettings->excluded, $settings->excluded);
3939
Assert::equal($expectedSettings->colors, $settings->colors);
4040
Assert::equal($expectedSettings->showProgress, $settings->showProgress);
41-
Assert::equal($expectedSettings->json, $settings->json);
41+
Assert::equal($expectedSettings->format, $settings->format);
4242
}
4343

4444
public function testMoreArguments()
@@ -57,7 +57,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
5757
$expectedSettings->excluded = array('vendor');
5858
$expectedSettings->colors = Settings::DISABLED;
5959
$expectedSettings->showProgress = true;
60-
$expectedSettings->json = false;
60+
$expectedSettings->format = Settings::FORMAT_TEXT;
6161

6262
Assert::equal($expectedSettings->phpExecutable, $settings->phpExecutable);
6363
Assert::equal($expectedSettings->shortTag, $settings->shortTag);
@@ -68,7 +68,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
6868
Assert::equal($expectedSettings->excluded, $settings->excluded);
6969
Assert::equal($expectedSettings->colors, $settings->colors);
7070
Assert::equal($expectedSettings->showProgress, $settings->showProgress);
71-
Assert::equal($expectedSettings->json, $settings->json);
71+
Assert::equal($expectedSettings->format, $settings->format);
7272
}
7373

7474
public function testColorsForced()
@@ -94,6 +94,22 @@ class SettingsParseArgumentsTest extends Tester\TestCase
9494

9595
Assert::equal($expectedSettings->colors, $settings->colors);
9696
}
97+
98+
public function testJsonOutput()
99+
{
100+
$commandLine = './parallel-lint --json .';
101+
$argv = explode(" ", $commandLine);
102+
$settings = Settings::parseArguments($argv);
103+
Assert::equal(Settings::FORMAT_JSON, $settings->format);
104+
}
105+
106+
public function testCheckstyleOutput()
107+
{
108+
$commandLine = './parallel-lint --checkstyle .';
109+
$argv = explode(" ", $commandLine);
110+
$settings = Settings::parseArguments($argv);
111+
Assert::equal(Settings::FORMAT_CHECKSTYLE, $settings->format);
112+
}
97113
}
98114

99115
$testCase = new SettingsParseArgumentsTest;

0 commit comments

Comments
 (0)