-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathCodeLocationResolver.php
More file actions
110 lines (94 loc) · 3.15 KB
/
CodeLocationResolver.php
File metadata and controls
110 lines (94 loc) · 3.15 KB
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
<?php
declare(strict_types=1);
namespace Sentry\Util;
use Sentry\Frame;
use Sentry\FrameBuilder;
use Sentry\Options;
use Sentry\Serializer\RepresentationSerializerInterface;
/**
* Resolves code location metadata from backtraces.
*
* @internal
*
* @phpstan-import-type StacktraceFrame from FrameBuilder
*/
final class CodeLocationResolver
{
/**
* @var FrameBuilder An instance of the builder of {@see Frame} objects
*/
private $frameBuilder;
/**
* Constructor.
*
* @param Options $options The SDK client options
* @param RepresentationSerializerInterface $representationSerializer The representation serializer
*/
public function __construct(Options $options, RepresentationSerializerInterface $representationSerializer)
{
$this->frameBuilder = new FrameBuilder($options, $representationSerializer);
}
/**
* Resolves the first in-app frame from the current backtrace into code
* location metadata.
*
* @return array{'code.filepath': string, 'code.function': string|null, 'code.lineno': int}|null
*/
public function resolve(int $limit = 20): ?array
{
/** @var list<StacktraceFrame> $backtrace */
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $limit);
return $this->resolveFromBacktrace($backtrace);
}
/**
* Resolves the first in-app frame from a backtrace into code location metadata.
*
* @param array<int, array<string, mixed>> $backtrace The backtrace
*
* @phpstan-param list<StacktraceFrame> $backtrace
*
* @return array{'code.filepath': string, 'code.function': string|null, 'code.lineno': int}|null
*/
public function resolveFromBacktrace(array $backtrace): ?array
{
$frame = $this->findFirstInAppFrameForBacktrace($backtrace);
if ($frame === null) {
return null;
}
return $this->getCodeLocationForFrame($frame);
}
/**
* Find the first in-app frame for a given backtrace.
*
* @param array<int, array<string, mixed>> $backtrace The backtrace
*
* @phpstan-param list<StacktraceFrame> $backtrace
*/
public function findFirstInAppFrameForBacktrace(array $backtrace): ?Frame
{
$file = Frame::INTERNAL_FRAME_FILENAME;
$line = 0;
foreach ($backtrace as $backtraceFrame) {
$frame = $this->frameBuilder->buildFromBacktraceFrame($file, $line, $backtraceFrame);
if ($frame->isInApp()) {
return $frame;
}
$file = $backtraceFrame['file'] ?? Frame::INTERNAL_FRAME_FILENAME;
$line = $backtraceFrame['line'] ?? 0;
}
return null;
}
/**
* Converts a frame into code location metadata.
*
* @return array{'code.filepath': string, 'code.function': string|null, 'code.lineno': int}
*/
public function getCodeLocationForFrame(Frame $frame): array
{
return [
'code.filepath' => $frame->getFile(),
'code.function' => $frame->getFunctionName(),
'code.lineno' => $frame->getLine(),
];
}
}