Skip to content

Commit 7eab13e

Browse files
committed
fix frames
1 parent 71c8b93 commit 7eab13e

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

src/Handlers/Console.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,39 @@ protected function handleStack(Trace $trace): void
6161
{
6262
$indent = ' ';
6363
$doubleIndent = $indent.$indent;
64+
6465
$exception = $trace->getException();
6566

6667
$text = $this->getMessage($exception);
6768
$this->writeln($text."\n");
6869
$this->writeln($indent.$exception->getFile().':'.$exception->getLine());
70+
6971
$this->writeln('');
7072

71-
foreach ($trace->getFrames()[0]->getPlaceInFile() as $num => $line) {
72-
$lineIndent = $doubleIndent;
73+
$frames = $trace->getFrames();
74+
75+
if (!\count($frames)) {
76+
return;
77+
}
78+
79+
if ($frames[0]->hasContext()) {
80+
$frame = array_shift($frames);
7381

74-
if ($num === $trace->getException()->getLine()) {
75-
$lineIndent = $indent.'--> ';
82+
foreach ($frame->getContext()->getPlaceInFile() as $num => $line) {
83+
$lineIndent = $doubleIndent;
84+
85+
if ($num === $frame->getLine()) {
86+
$lineIndent = $indent.'--> ';
87+
}
88+
89+
$text = $lineIndent.$num.' '.rtrim($line);
90+
$this->writeln($text);
7691
}
7792

78-
$text = $lineIndent.$num.' '.rtrim($line);
79-
$this->writeln($text);
93+
$this->writeln('');
8094
}
8195

82-
$this->writeln('');
83-
84-
foreach ($trace->getFrames() as $frame) {
96+
foreach ($frames as $frame) {
8597
$context = $frame->getFile() ? $frame->getFile().':'.$frame->getLine() : $frame->getCaller();
8698
$this->writeln($indent.$context);
8799
}

src/Inspection/Frame.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Frame
2424
public static function create(array $frame): self
2525
{
2626
// @link https://github.com/symfony/var-dumper/commit/7670e4790f447b3380993b8109ae2ed3d2366480
27-
if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $frame['file'] ?? '', $match) !== false) {
27+
if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $frame['file'] ?? '', $match)) {
2828
$frame['file'] = substr($frame['file'], 0, -\strlen($match[0]));
2929
$frame['line'] = (int) $match[1];
3030
}
@@ -70,6 +70,11 @@ public function getContext(): Context
7070
return new Context($this->getFile(), $this->getLine());
7171
}
7272

73+
public function hasContext(): bool
74+
{
75+
return null !== $this->getFile() && null !== $this->getLine() && is_file($this->getFile());
76+
}
77+
7378
public function getCaller(): ?string
7479
{
7580
return $this->caller;

src/Inspection/Trace.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public function getExceptionClassName(): string
2727

2828
public function getFrames(): array
2929
{
30-
return array_map(fn (array $params): Frame => Frame::create($params), $this->exception->getTrace());
30+
$frames = $this->exception->getTrace();
31+
32+
$containsException = array_reduce($frames, function (bool $carry, array $frame): bool {
33+
return isset($frame['file'], $frame['line']) && $this->exception->getFile() === $frame['file'] && $this->exception->getLine() === $frame['line'] ? true : $carry;
34+
}, false);
35+
36+
if (!$containsException) {
37+
array_unshift($frames, [
38+
'file' => $this->exception->getFile(),
39+
'line' => $this->exception->getLine(),
40+
]);
41+
}
42+
43+
return array_map(fn (array $params): Frame => Frame::create($params), $frames);
3144
}
3245
}

0 commit comments

Comments
 (0)