Skip to content
Permalink
Browse files

Fix #1616 - allow Psalm to output checkstyle format

  • Loading branch information...
muglug committed May 8, 2019
1 parent b7529e8 commit 111abf82daaf90f785b3a67ef847eb509b224d9b
@@ -133,6 +133,7 @@ class ProjectAnalyzer
const TYPE_JSON = 'json';
const TYPE_EMACS = 'emacs';
const TYPE_XML = 'xml';
const TYPE_CHECKSTYLE = 'checkstyle';
const TYPE_TEXT = 'text';
const SUPPORTED_OUTPUT_TYPES = [
@@ -142,6 +143,7 @@ class ProjectAnalyzer
self::TYPE_JSON,
self::TYPE_EMACS,
self::TYPE_XML,
self::TYPE_CHECKSTYLE,
self::TYPE_TEXT,
];
@@ -188,10 +190,8 @@ public function __construct(
}
if ($reports) {
/**
* @var array<string,string>
*/
$mapping = [
'checkstyle.xml' => self::TYPE_CHECKSTYLE,
'.xml' => self::TYPE_XML,
'.json' => self::TYPE_JSON,
'.txt' => self::TYPE_TEXT,
@@ -3,6 +3,7 @@
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Issue\CodeIssue;
use Psalm\Output\Checkstyle;
use Psalm\Output\Compact;
use Psalm\Output\Console;
use Psalm\Output\Emacs;
@@ -385,6 +386,10 @@ public static function getOutput($format, $use_color, $show_snippet = true, $sho
$output = new Pylint(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_CHECKSTYLE:
$output = new Checkstyle(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_XML:
$output = new Xml(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
@@ -0,0 +1,40 @@
<?php
namespace Psalm\Output;
use LSS\Array2XML;
use Psalm\Output;
class Checkstyle extends Output
{
/**
* {{@inheritdoc}}
*/
public function create(): string
{
$output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$output .= '<checkstyle>' . "\n";
foreach ($this->issues_data as $issue_data) {
$message = sprintf(
'%s: %s',
$issue_data['type'],
$issue_data['message']
);
$output .= '<file name="' . $issue_data['file_name'] . '">' . "\n";
$output .= ' ';
$output .= '<error';
$output .= ' line="' . $issue_data['line_from'] . '"';
$output .= ' column="' . $issue_data['column_from'] . '"';
$output .= ' severity="' . $issue_data['severity'] . '"';
$output .= ' message="' . $message . '"';
$output .= '/>' . "\n";
$output .= '</file>' . "\n";
}
$output .= '</checkstyle>' . "\n";
return $output;
}
}
@@ -181,7 +181,7 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
Only checks methods that have changed (and their dependents)
--output-format=console
Changes the output format. Possible values: compact, console, emacs, json, pylint, xml
Changes the output format. Possible values: compact, console, emacs, json, pylint, xml, checkstyle
--find-dead-code[=auto]
--find-unused-code[=auto]
@@ -254,6 +254,26 @@ function psalmCanVerify(int $your_code): ?string {
IssueBuffer::getOutput(ProjectAnalyzer::TYPE_COMPACT, false)
);
$this->assertSame(
'<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="somefile.php">
<error line="3" column="10" severity="error" message="UndefinedVariable: Cannot find referenced variable $as_you"/>
</file>
<file name="somefile.php">
<error line="2" column="42" severity="error" message="MixedInferredReturnType: Could not verify return type \'string|null\' for psalmCanVerify"/>
</file>
<file name="somefile.php">
<error line="7" column="6" severity="error" message="UndefinedConstant: Const CHANGE_ME is not defined"/>
</file>
<file name="somefile.php">
<error line="15" column="6" severity="error" message="PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10"/>
</file>
</checkstyle>
',
IssueBuffer::getOutput(ProjectAnalyzer::TYPE_CHECKSTYLE, false)
);
// FIXME: The XML parser only return strings, all int value are casted, so the assertSame failed
//$this->assertSame(
// ['report' => ['item' => $issue_data]],
@@ -290,6 +310,14 @@ public function testEmptyReportIfNotError()
IssueBuffer::getOutput(ProjectAnalyzer::TYPE_XML, false)
);
$this->assertSame(
'<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
</checkstyle>
',
IssueBuffer::getOutput(ProjectAnalyzer::TYPE_CHECKSTYLE, false)
);
ob_start();
IssueBuffer::finish($this->project_analyzer, true, 0);
ob_end_clean();

0 comments on commit 111abf8

Please sign in to comment.
You can’t perform that action at this time.