Skip to content

Commit 06b1231

Browse files
committed
Improve handling of anonymous stack frames
1 parent 38f3c24 commit 06b1231

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/Frame.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class Frame
1313
{
1414
public const INTERNAL_FRAME_FILENAME = '[internal]';
1515

16-
public const ANONYMOUS_CLASS_PREFIX = "class@anonymous\x00";
16+
public const ANONYMOUS_CLASS_PREFIX = "@anonymous\x00";
1717

1818
/**
1919
* @var string|null The name of the function being called

src/FrameBuilder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ public function buildFromBacktraceFrame(string $file, int $line, array $backtrac
7474
if (isset($backtraceFrame['class']) && isset($backtraceFrame['function'])) {
7575
$functionName = $backtraceFrame['class'];
7676

77-
if (mb_substr($functionName, 0, mb_strlen(Frame::ANONYMOUS_CLASS_PREFIX)) === Frame::ANONYMOUS_CLASS_PREFIX) {
78-
$functionName = Frame::ANONYMOUS_CLASS_PREFIX . $this->stripPrefixFromFilePath($this->options, substr($backtraceFrame['class'], \strlen(Frame::ANONYMOUS_CLASS_PREFIX)));
77+
// Optimization: skip doing regex if we don't have prefixes to strip
78+
if ($this->options->getPrefixes()) {
79+
$functionName = preg_replace_callback('/' . Frame::ANONYMOUS_CLASS_PREFIX . '([^:]+)(:.*)?/', function (array $matches) {
80+
return Frame::ANONYMOUS_CLASS_PREFIX . $this->stripPrefixFromFilePath($this->options, $matches[1]) . ($matches[2] ?? '');
81+
}, $functionName);
7982
}
8083

8184
$rawFunctionName = \sprintf('%s::%s', $backtraceFrame['class'], $backtraceFrame['function']);

tests/FrameBuilderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ public static function buildFromBacktraceFrameDataProvider(): \Generator
149149
],
150150
new Frame(null, 'path/not/of/app/to/file', 10, null, 'path/not/of/app/to/file'),
151151
];
152+
153+
yield [
154+
new Options([
155+
'prefixes' => ['/path/to'],
156+
]),
157+
[
158+
'file' => '/path/to/file',
159+
'line' => 10,
160+
'function' => 'test_function',
161+
'class' => "App\\ClassName@anonymous\0/path/to/file:85$29e",
162+
],
163+
new Frame("App\\ClassName@anonymous\0/file::test_function", '/file', 10, "App\\ClassName@anonymous\0/path/to/file:85$29e::test_function", '/path/to/file'),
164+
];
152165
}
153166

154167
/**

0 commit comments

Comments
 (0)