| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Util; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sentry\Frame; |
| 7 |
use WCPOS\Vendor\Sentry\FrameBuilder; |
| 8 |
use WCPOS\Vendor\Sentry\Options; |
| 9 |
use WCPOS\Vendor\Sentry\Serializer\RepresentationSerializerInterface; |
| 10 |
/** |
| 11 |
* Resolves code location metadata from backtraces. |
| 12 |
* |
| 13 |
* @internal |
| 14 |
* |
| 15 |
* @phpstan-import-type StacktraceFrame from FrameBuilder |
| 16 |
*/ |
| 17 |
final class CodeLocationResolver |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var FrameBuilder An instance of the builder of {@see Frame} objects |
| 21 |
*/ |
| 22 |
private $frameBuilder; |
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @param Options $options The SDK client options |
| 27 |
* @param RepresentationSerializerInterface $representationSerializer The representation serializer |
| 28 |
*/ |
| 29 |
public function __construct(Options $options, RepresentationSerializerInterface $representationSerializer) |
| 30 |
{ |
| 31 |
$this->frameBuilder = new FrameBuilder($options, $representationSerializer); |
| 32 |
} |
| 33 |
/** |
| 34 |
* Resolves the first in-app frame from the current backtrace into code |
| 35 |
* location metadata. |
| 36 |
* |
| 37 |
* @return array{'code.filepath': string, 'code.function': string|null, 'code.lineno': int}|null |
| 38 |
*/ |
| 39 |
public function resolve(int $limit = 20) : ?array |
| 40 |
{ |
| 41 |
/** @var list<StacktraceFrame> $backtrace */ |
| 42 |
$backtrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $limit); |
| 43 |
return $this->resolveFromBacktrace($backtrace); |
| 44 |
} |
| 45 |
/** |
| 46 |
* Resolves the first in-app frame from a backtrace into code location metadata. |
| 47 |
* |
| 48 |
* @param array<int, array<string, mixed>> $backtrace The backtrace |
| 49 |
* |
| 50 |
* @phpstan-param list<StacktraceFrame> $backtrace |
| 51 |
* |
| 52 |
* @return array{'code.filepath': string, 'code.function': string|null, 'code.lineno': int}|null |
| 53 |
*/ |
| 54 |
public function resolveFromBacktrace(array $backtrace) : ?array |
| 55 |
{ |
| 56 |
$frame = $this->findFirstInAppFrameForBacktrace($backtrace); |
| 57 |
if ($frame === null) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
return $this->getCodeLocationForFrame($frame); |
| 61 |
} |
| 62 |
/** |
| 63 |
* Find the first in-app frame for a given backtrace. |
| 64 |
* |
| 65 |
* @param array<int, array<string, mixed>> $backtrace The backtrace |
| 66 |
* |
| 67 |
* @phpstan-param list<StacktraceFrame> $backtrace |
| 68 |
*/ |
| 69 |
public function findFirstInAppFrameForBacktrace(array $backtrace) : ?Frame |
| 70 |
{ |
| 71 |
$file = Frame::INTERNAL_FRAME_FILENAME; |
| 72 |
$line = 0; |
| 73 |
foreach ($backtrace as $backtraceFrame) { |
| 74 |
$frame = $this->frameBuilder->buildFromBacktraceFrame($file, $line, $backtraceFrame); |
| 75 |
if ($frame->isInApp()) { |
| 76 |
return $frame; |
| 77 |
} |
| 78 |
$file = $backtraceFrame['file'] ?? Frame::INTERNAL_FRAME_FILENAME; |
| 79 |
$line = $backtraceFrame['line'] ?? 0; |
| 80 |
} |
| 81 |
return null; |
| 82 |
} |
| 83 |
/** |
| 84 |
* Converts a frame into code location metadata. |
| 85 |
* |
| 86 |
* @return array{'code.filepath': string, 'code.function': string|null, 'code.lineno': int} |
| 87 |
*/ |
| 88 |
public function getCodeLocationForFrame(Frame $frame) : array |
| 89 |
{ |
| 90 |
return ['code.filepath' => $frame->getFile(), 'code.function' => $frame->getFunctionName(), 'code.lineno' => $frame->getLine()]; |
| 91 |
} |
| 92 |
} |
| 93 |
|