1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

ConsoleReport: Add links to open file in editor

This commit is contained in:
Gregor Harlan 2021-11-07 14:38:42 +01:00
parent 143964767d
commit ab0049d0ff
No known key found for this signature in database
GPG Key ID: A43C725D9D2AE675
3 changed files with 34 additions and 5 deletions

View File

@ -27,6 +27,11 @@ class DataFlowNodeData
*/
public $file_name;
/**
* @var string
*/
public $file_path;
/**
* @var string
*/
@ -62,6 +67,7 @@ class DataFlowNodeData
int $line_from,
int $line_to,
string $file_name,
string $file_path,
string $snippet,
int $from,
int $to,
@ -73,6 +79,7 @@ class DataFlowNodeData
$this->line_from = $line_from;
$this->line_to = $line_to;
$this->file_name = $file_name;
$this->file_path = $file_path;
$this->snippet = $snippet;
$this->from = $from;
$this->to = $to;

View File

@ -66,6 +66,7 @@ abstract class TaintedInput extends CodeIssue
$location->getLineNumber(),
$location->getEndLineNumber(),
$location->file_name,
$location->file_path,
$location->getSnippet(),
$selection_bounds[0],
$selection_bounds[1],

View File

@ -3,12 +3,19 @@ namespace Psalm\Report;
use Psalm\Config;
use Psalm\Internal\Analyzer\DataFlowNodeData;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;
use function get_cfg_var;
use function ini_get;
use function strtr;
use function substr;
class ConsoleReport extends Report
{
/** @var string|null */
private $linkFormat;
public function create(): string
{
$output = '';
@ -34,7 +41,7 @@ class ConsoleReport extends Report
$issue_reference = $issue_data->link ? ' (see ' . $issue_data->link . ')' : '';
$issue_string .= ': ' . $issue_data->type
. ' - ' . $issue_data->file_name . ':' . $issue_data->line_from . ':' . $issue_data->column_from
. ' - ' . $this->getFileReference($issue_data)
. ' - ' . $issue_data->message . $issue_reference . "\n";
@ -75,10 +82,7 @@ class ConsoleReport extends Report
foreach ($taint_trace as $node_data) {
if ($node_data instanceof DataFlowNodeData) {
$snippets .= ' ' . $node_data->label
. ' - ' . $node_data->file_name
. ':' . $node_data->line_from
. ':' . $node_data->column_from . "\n";
$snippets .= ' ' . $node_data->label . ' - ' . $this->getFileReference($node_data) . "\n";
if ($this->show_snippet) {
$snippet = $node_data->snippet;
@ -102,4 +106,21 @@ class ConsoleReport extends Report
return $snippets;
}
/**
* @param IssueData|DataFlowNodeData $data
*/
private function getFileReference($data): string
{
if (null === $this->linkFormat) {
// if xdebug is not enabled, use `get_cfg_var` to get the value directly from php.ini
$this->linkFormat = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')
?: 'file://%f#L%l';
}
$link = strtr($this->linkFormat, ['%f' => $data->file_path, '%l' => $data->line_from]);
$reference = $data->file_name . ':' . $data->line_from . ':' . $data->column_from;
return "\033]8;;" . $link . "\033\\" . $reference . "\033]8;;\033\\";
}
}