-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathErrorFormatter.php
142 lines (122 loc) · 4.61 KB
/
ErrorFormatter.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace PHP_Parallel_Lint\PhpParallelLint;
use JakubOnderka\PhpConsoleColor\ConsoleColor as OldConsoleColor;
use JakubOnderka\PhpConsoleHighlighter\Highlighter as OldHighlighter;
use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;
use PHP_Parallel_Lint\PhpParallelLint\Errors\ParallelLintError;
use PHP_Parallel_Lint\PhpParallelLint\Errors\SyntaxError;
class ErrorFormatter
{
/** @var string */
private $useColors;
/** @var bool */
private $forceColors;
/** @var bool */
private $translateTokens;
public function __construct($useColors = Settings::AUTODETECT, $translateTokens = false, $forceColors = false)
{
$this->useColors = $useColors;
$this->forceColors = $forceColors;
$this->translateTokens = $translateTokens;
}
/**
* @param ParallelLintError $error
* @return string
*/
public function format(ParallelLintError $error)
{
if ($error instanceof SyntaxError) {
return $this->formatSyntaxErrorMessage($error);
} else {
if ($error->getMessage()) {
return $error->getMessage();
} else {
return "Unknown error for file '{$error->getFilePath()}'.";
}
}
}
/**
* @param SyntaxError $error
* @param bool $withCodeSnipped
* @return string
*/
public function formatSyntaxErrorMessage(SyntaxError $error, $withCodeSnipped = true)
{
$string = "Parse error: {$error->getShortFilePath()}";
if ($error->getLine()) {
$onLine = $error->getLine();
$string .= ":$onLine" . PHP_EOL;
if ($withCodeSnipped) {
if ($this->useColors !== Settings::DISABLED) {
$string .= $this->getColoredCodeSnippet($error->getFilePath(), $onLine);
} else {
$string .= $this->getCodeSnippet($error->getFilePath(), $onLine);
}
}
}
$string .= $error->getNormalizedMessage($this->translateTokens);
if ($error->getBlame()) {
$blame = $error->getBlame();
$shortCommitHash = substr($blame->commitHash, 0, 8);
$dateTime = $blame->datetime->format('c');
$string .= PHP_EOL . "Blame {$blame->name} <{$blame->email}>, commit '$shortCommitHash' from $dateTime";
}
return $string;
}
/**
* @param string $filePath
* @param int $lineNumber
* @param int $linesBefore
* @param int $linesAfter
* @return string
*/
protected function getCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $linesAfter = 2)
{
$lines = file($filePath);
$offset = $lineNumber - $linesBefore - 1;
$offset = max($offset, 0);
$length = $linesAfter + $linesBefore + 1;
$lines = array_slice($lines, $offset, $length, $preserveKeys = true);
end($lines);
$lineStrlen = strlen(key($lines) + 1);
$snippet = '';
foreach ($lines as $i => $line) {
$snippet .= ($lineNumber === $i + 1 ? ' > ' : ' ');
$snippet .= str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| ' . rtrim($line) . PHP_EOL;
}
return $snippet;
}
/**
* @param string $filePath
* @param int $lineNumber
* @param int $linesBefore
* @param int $linesAfter
* @return string
*/
protected function getColoredCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $linesAfter = 2)
{
if (
class_exists('\PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter')
&& class_exists('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')
) {
// Highlighter and ConsoleColor 1.0+.
$colors = new ConsoleColor();
$colors->setForceStyle($this->forceColors);
$highlighter = new Highlighter($colors);
} else if (
class_exists('\JakubOnderka\PhpConsoleHighlighter\Highlighter')
&& class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')
) {
// Highlighter and ConsoleColor < 1.0.
$colors = new OldConsoleColor();
$colors->setForceStyle($this->forceColors);
$highlighter = new OldHighlighter($colors);
}
if (isset($colors, $highlighter) === false) {
return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter);
}
$fileContent = file_get_contents($filePath);
return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);
}
}