diff --git a/src/Psalm/Internal/Analyzer/DataFlowNodeData.php b/src/Psalm/Internal/Analyzer/DataFlowNodeData.php index 4a88ba34c..a33ac9696 100644 --- a/src/Psalm/Internal/Analyzer/DataFlowNodeData.php +++ b/src/Psalm/Internal/Analyzer/DataFlowNodeData.php @@ -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; diff --git a/src/Psalm/Issue/TaintedInput.php b/src/Psalm/Issue/TaintedInput.php index 9ec50c599..12f592db9 100644 --- a/src/Psalm/Issue/TaintedInput.php +++ b/src/Psalm/Issue/TaintedInput.php @@ -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], diff --git a/src/Psalm/Report/ConsoleReport.php b/src/Psalm/Report/ConsoleReport.php index f028e97b4..731b76ccd 100644 --- a/src/Psalm/Report/ConsoleReport.php +++ b/src/Psalm/Report/ConsoleReport.php @@ -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\\"; + } }