-
Notifications
You must be signed in to change notification settings - Fork 651
/
ConsoleReport.php
103 lines (84 loc) · 3.48 KB
/
ConsoleReport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Psalm\Report;
use Psalm\Config;
use Psalm\Report;
use Psalm\Internal\Analyzer\TaintNodeData;
use function substr;
use function basename;
use function strlen;
class ConsoleReport 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 . ')';
$file_basename = basename($issue_data->file_name);
$file_path = substr($issue_data->file_name, 0, -strlen($file_basename));
$issue_string .= ': ' . $issue_data->type
. ' - ' . $file_path . "\e[1;31m" . $file_basename . ':' . $issue_data->line_from . "\e[0m"
. ':' . $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) {
$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;
}
}