1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Encode XML output consistently

This fixes test failures when running on PHP 8.1, due to changed
`htmlspecialchars()` defaults
This commit is contained in:
Bruce Weirdan 2021-09-05 20:06:59 +03:00
parent 916b098b00
commit abf557da44
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
4 changed files with 15 additions and 11 deletions

View File

@ -4,6 +4,10 @@ namespace Psalm;
use Psalm\Internal\Analyzer\IssueData;
use function array_filter;
use function htmlspecialchars;
use const ENT_QUOTES;
use const ENT_XML1;
abstract class Report
{
@ -99,5 +103,10 @@ abstract class Report
$this->total_expression_count = $total_expression_count;
}
protected function xmlEncode(string $data): string
{
return htmlspecialchars($data, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}
abstract public function create(): string;
}

View File

@ -3,7 +3,6 @@ namespace Psalm\Report;
use Psalm\Report;
use function htmlspecialchars;
use function sprintf;
class CheckstyleReport extends Report
@ -21,13 +20,13 @@ class CheckstyleReport extends Report
$issue_data->message
);
$output .= '<file name="' . htmlspecialchars($issue_data->file_name) . '">' . "\n";
$output .= '<file name="' . $this->xmlEncode($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="' . htmlspecialchars($message) . '"';
$output .= ' message="' . $this->xmlEncode($message) . '"';
$output .= '/>' . "\n";
$output .= '</file>' . "\n";
}

View File

@ -8,12 +8,8 @@ use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;
use function count;
use function htmlspecialchars;
use function trim;
use const ENT_QUOTES;
use const ENT_XML1;
/**
* based on https://github.com/m50/psalm-json-to-junit
* Copyright (c) Marisa Clardy marisa@clardy.eu
@ -165,10 +161,10 @@ class JunitReport extends Report
private function dataToOutput(IssueData $data): string
{
$ret = 'message: ' . htmlspecialchars(trim($data->message), ENT_XML1 | ENT_QUOTES) . "\n";
$ret = 'message: ' . $this->xmlEncode(trim($data->message)) . "\n";
$ret .= 'type: ' . trim($data->type) . "\n";
$ret .= 'snippet: ' . htmlspecialchars(trim($data->snippet), ENT_XML1 | ENT_QUOTES) . "\n";
$ret .= 'selected_text: ' . htmlspecialchars(trim($data->selected_text)) . "\n";
$ret .= 'snippet: ' . $this->xmlEncode(trim($data->snippet)) . "\n";
$ret .= 'selected_text: ' . $this->xmlEncode(trim($data->selected_text)) . "\n";
$ret .= 'line: ' . $data->line_from . "\n";
$ret .= 'column_from: ' . $data->column_from . "\n";
$ret .= 'column_to: ' . $data->column_to . "\n";

View File

@ -1107,7 +1107,7 @@ INFO: PossiblyUndefinedGlobalVariable - somefile.php:17:6 - Possibly undefined g
<error line="3" column="10" severity="error" message="MixedReturnStatement: Could not infer a return type"/>
</file>
<file name="somefile.php">
<error line="2" column="42" severity="error" message="MixedInferredReturnType: Could not verify return type \'null|string\' for psalmCanVerify"/>
<error line="2" column="42" severity="error" message="MixedInferredReturnType: Could not verify return type &apos;null|string&apos; for psalmCanVerify"/>
</file>
<file name="somefile.php">
<error line="8" column="6" severity="error" message="UndefinedConstant: Const CHANGE_ME is not defined"/>