Skip to content

Commit 6341d84

Browse files
authored
Improve handling of prefix stripping for anonymous frames (#1820)
1 parent 38f3c24 commit 6341d84

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/Frame.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ final class Frame
1313
{
1414
public const INTERNAL_FRAME_FILENAME = '[internal]';
1515

16+
/**
17+
* @deprecated This constant is deprecated and will be removed in 5.x.
18+
*/
1619
public const ANONYMOUS_CLASS_PREFIX = "class@anonymous\x00";
1720

1821
/**

src/FrameBuilder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ 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+
$prefixStrippedFunctionName = preg_replace_callback('/@anonymous\\x00([^:]+)(:.*)?/', function (array $matches) {
80+
return "@anonymous\x00" . $this->stripPrefixFromFilePath($this->options, $matches[1]) . ($matches[2] ?? '');
81+
}, $functionName);
82+
83+
if ($prefixStrippedFunctionName) {
84+
$functionName = $prefixStrippedFunctionName;
85+
}
7986
}
8087

8188
$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)