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

Add option to show link to source in console output (#4085)

* add option to change console output for PhpStorm suitable format, so filenames become links, closes #3271

* fix code style

* rename option to php-storm-format

* replace flag with new report format

* fix code style
This commit is contained in:
the-toster 2020-09-04 23:24:14 +03:00 committed by Daniil Gentili
parent f9d6b9d7c1
commit 3ff4d0fa3d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
7 changed files with 123 additions and 5 deletions

View File

@ -637,7 +637,7 @@ class Config
$config_path = self::locateConfigFile($path);
if (!$config_path) {
if ($output_format === \Psalm\Report::TYPE_CONSOLE) {
if (in_array($output_format, [\Psalm\Report::TYPE_CONSOLE, \Psalm\Report::TYPE_PHP_STORM])) {
echo 'Could not locate a config XML file in path ' . $path
. '. Have you run \'psalm --init\' ?' . PHP_EOL;
exit(1);

View File

@ -1231,7 +1231,10 @@ class ProjectAnalyzer
);
if ($this->stdout_report_options
&& $this->stdout_report_options->format === Report::TYPE_CONSOLE
&& in_array(
$this->stdout_report_options->format,
[\Psalm\Report::TYPE_CONSOLE, \Psalm\Report::TYPE_PHP_STORM]
)
&& $this->codebase->collect_references
) {
fwrite(

View File

@ -1,6 +1,7 @@
<?php
namespace Psalm;
use Psalm\Report\PhpStormReport;
use function array_pop;
use function array_search;
use function array_splice;
@ -37,6 +38,7 @@ use function str_replace;
use function usort;
use function array_merge;
use function array_values;
use function in_array;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
use const STDERR;
@ -455,7 +457,10 @@ class IssueBuffer
$issues_data = [];
if (self::$issues_data) {
if ($project_analyzer->stdout_report_options->format === Report::TYPE_CONSOLE) {
if (in_array(
$project_analyzer->stdout_report_options->format,
[\Psalm\Report::TYPE_CONSOLE, \Psalm\Report::TYPE_PHP_STORM]
)) {
echo "\n";
}
@ -575,7 +580,10 @@ class IssueBuffer
);
}
if ($project_analyzer->stdout_report_options->format === Report::TYPE_CONSOLE) {
if (in_array(
$project_analyzer->stdout_report_options->format,
[\Psalm\Report::TYPE_CONSOLE, \Psalm\Report::TYPE_PHP_STORM]
)) {
echo str_repeat('-', 30) . "\n";
if ($error_count) {
@ -745,6 +753,10 @@ class IssueBuffer
case Report::TYPE_GITHUB_ACTIONS:
$output = new GithubActionsReport($normalized_data, self::$fixable_issue_counts, $report_options);
break;
case Report::TYPE_PHP_STORM:
$output = new PhpStormReport($normalized_data, self::$fixable_issue_counts, $report_options);
break;
}
return $output->create();

View File

@ -18,6 +18,7 @@ abstract class Report
const TYPE_CHECKSTYLE = 'checkstyle';
const TYPE_TEXT = 'text';
const TYPE_GITHUB_ACTIONS = 'github';
const TYPE_PHP_STORM = 'phpstorm';
const SUPPORTED_OUTPUT_TYPES = [
self::TYPE_COMPACT,
@ -32,6 +33,7 @@ abstract class Report
self::TYPE_CHECKSTYLE,
self::TYPE_TEXT,
self::TYPE_GITHUB_ACTIONS,
self::TYPE_PHP_STORM,
];
/**

View File

@ -39,6 +39,7 @@ class ConsoleReport extends Report
. ' - ' . $issue_data->file_name . ':' . $issue_data->line_from . ':' . $issue_data->column_from
. ' - ' . $issue_data->message . $issue_reference . "\n";
if ($issue_data->taint_trace) {
$issue_string .= $this->getTaintSnippets($issue_data->taint_trace);
} elseif ($this->show_snippet) {

View File

@ -0,0 +1,99 @@
<?php
namespace Psalm\Report;
use Psalm\Config;
use Psalm\Report;
use Psalm\Internal\Analyzer\TaintNodeData;
use function substr;
class PhpStormReport extends Report
{
/**
* {@inheritdoc}
*/
public function create(): string
{
$output = '';
foreach ($this->issues_data as $issue_data) {
$output .= $this->format($issue_data) . "\n" . "\n";
}
return $output;
}
private function format(\Psalm\Internal\Analyzer\IssueData $issue_data): string
{
$issue_string = '';
$is_error = $issue_data->severity === Config::REPORT_ERROR;
if ($is_error) {
$issue_string .= ($this->use_color ? "\e[0;31mERROR\e[0m" : 'ERROR');
} else {
$issue_string .= 'INFO';
}
$issue_reference = ' (see ' . $issue_data->link . ')';
$issue_string .= ': ' . $issue_data->type
. "\nat " . $issue_data->file_name . ':' . $issue_data->line_from . ':' . $issue_data->column_from
. "\n" . $issue_data->message . $issue_reference . "\n";
if ($issue_data->taint_trace) {
$issue_string .= $this->getTaintSnippets($issue_data->taint_trace);
} elseif ($this->show_snippet) {
$snippet = $issue_data->snippet;
if (!$this->use_color) {
$issue_string .= $snippet;
} else {
$selection_start = $issue_data->from - $issue_data->snippet_from;
$selection_length = $issue_data->to - $issue_data->from;
$issue_string .= substr($snippet, 0, $selection_start)
. ($is_error ? "\e[97;41m" : "\e[30;47m") . substr($snippet, $selection_start, $selection_length)
. "\e[0m" . substr($snippet, $selection_length + $selection_start) . "\n";
}
}
return $issue_string;
}
/**
* @param non-empty-list<TaintNodeData|array{label: string, entry_path_type: string}> $taint_trace
*/
private function getTaintSnippets(array $taint_trace) : string
{
$snippets = '';
foreach ($taint_trace as $node_data) {
if ($node_data instanceof TaintNodeData) {
$snippets .= ' ' . $node_data->label
. ' - ' . $node_data->file_name
. ':' . $node_data->line_from
. ':' . $node_data->column_from . "\n";
if ($this->show_snippet) {
$snippet = $node_data->snippet;
if (!$this->use_color) {
$snippets .= $snippet . "\n\n";
} else {
$selection_start = $node_data->from - $node_data->snippet_from;
$selection_length = $node_data->to - $node_data->from;
$snippets .= substr($snippet, 0, $selection_start)
. "\e[30;47m" . substr($snippet, $selection_start, $selection_length)
. "\e[0m" . substr($snippet, $selection_length + $selection_start) . "\n\n";
}
}
} else {
$snippets .= ' ' . $node_data['label'] . "\n";
$snippets .= ' <no known location>' . "\n\n";
}
}
return $snippets;
}
}

View File

@ -384,7 +384,8 @@ Output:
--output-format=console
Changes the output format.
Available formats: compact, console, text, emacs, json, pylint, xml, checkstyle, junit, sonarqube, github
Available formats: compact, console, text, emacs, json, pylint, xml, checkstyle, junit, sonarqube, github,
phpstorm
--no-progress
Disable the progress indicator