1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Console; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Exception\NotReadableException; |
13
|
|
|
use Railt\Io\File; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ExceptionRenderer |
17
|
|
|
*/ |
18
|
|
|
class ExceptionRenderer |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Highlighter |
22
|
|
|
*/ |
23
|
|
|
private $highlighter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ExceptionRenderer constructor. |
27
|
|
|
* @param Highlighter $highlighter |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Highlighter $highlighter) |
30
|
|
|
{ |
31
|
|
|
$this->highlighter = $highlighter; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \Throwable $e |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function render(\Throwable $e): string |
39
|
|
|
{ |
40
|
|
|
return |
41
|
|
|
$this->renderHeader($e) . |
42
|
|
|
$this->renderFileHeader($e) . |
43
|
|
|
$this->renderCode($e) . |
44
|
|
|
$this->renderTrace($e); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param \Throwable $e |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
private function renderCode(\Throwable $e): string |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
$file = File::fromPathname($e->getFile()); |
55
|
|
|
} catch (NotReadableException $error) { |
56
|
|
|
$file = File::fromSources('', $e->getFile()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->highlighter->highlight($file, $e->getLine()) . |
|
|
|
|
60
|
|
|
\PHP_EOL; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \Throwable $e |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
private function renderHeader(\Throwable $e): string |
68
|
|
|
{ |
69
|
|
|
[$name, $msg] = [$this->classname($e), $e->getMessage()]; |
|
|
|
|
70
|
|
|
|
71
|
|
|
return \sprintf('<error> %s </error> <comment>%s</comment>', $name, $msg) . |
72
|
|
|
\PHP_EOL . \PHP_EOL; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string|object $class |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function classname($class): string |
80
|
|
|
{ |
81
|
|
|
$class = \is_object($class) ? \get_class($class) : $class; |
82
|
|
|
|
83
|
|
|
return \basename(\str_replace('\\', '/', $class)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Throwable $e |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
private function renderFileHeader(\Throwable $e): string |
91
|
|
|
{ |
92
|
|
|
$message = 'in <fg=cyan;options=underscore>%s</> at line <fg=cyan>%d</>'; |
93
|
|
|
|
94
|
|
|
return \sprintf($message, $e->getFile(), $e->getLine()) . |
95
|
|
|
\PHP_EOL . \PHP_EOL; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \Throwable $e |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
private function renderTrace(\Throwable $e): string |
103
|
|
|
{ |
104
|
|
|
$result = ['<comment>Stack Trace:</comment>']; |
105
|
|
|
|
106
|
|
|
foreach ($e->getTrace() as $i => $trace) { |
107
|
|
|
$line = $this->traceInvocation($trace); |
108
|
|
|
$args = $this->traceArguments($trace); |
109
|
|
|
|
110
|
|
|
$result[] = \sprintf('<fg=blue>%3s</> <comment>%s(%s)</comment>', '#' . $i, $line, $args); |
111
|
|
|
$result[] = \sprintf(' <fg=cyan>%s</>:<fg=cyan>%d</>', $trace['file'], $trace['line']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return \implode(\PHP_EOL, $result) . |
115
|
|
|
\PHP_EOL; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $trace |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
private function traceArguments(array $trace): string |
123
|
|
|
{ |
124
|
|
|
$result = []; |
125
|
|
|
|
126
|
|
|
foreach (($trace['args'] ?? []) as $arg) { |
127
|
|
|
$result[] = \is_object($arg) ? 'Object(' . $this->classname($arg) . ')' : \gettype($arg); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return \implode(', ', $result); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $trace |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
private function traceInvocation(array $trace): string |
138
|
|
|
{ |
139
|
|
|
return isset($trace['class']) |
140
|
|
|
? $trace['class'] . $trace['type'] . $trace['function'] |
141
|
|
|
: $trace['function']; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.