Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/Integration/FrameContextifierIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ final class FrameContextifierIntegration implements IntegrationInterface
*/
private $logger;

/**
* @var array<string, array{
* pre_context: string[],
* context_line: string|null,
* post_context: string[]
* }> The excerpts read while processing the current event, keyed by "file:line"
*/
private $excerptCache = [];

/**
* Creates a new instance of this integration.
*
Expand Down Expand Up @@ -54,16 +63,20 @@ public function setupOnce(): void
return $event;
}

$stacktrace = $event->getStacktrace();
try {
$stacktrace = $event->getStacktrace();

if ($stacktrace !== null) {
$integration->addContextToStacktraceFrames($maxContextLines, $stacktrace);
}
if ($stacktrace !== null) {
$integration->addContextToStacktraceFrames($maxContextLines, $stacktrace);
}

foreach ($event->getExceptions() as $exception) {
if ($exception->getStacktrace() !== null) {
$integration->addContextToStacktraceFrames($maxContextLines, $exception->getStacktrace());
foreach ($event->getExceptions() as $exception) {
if ($exception->getStacktrace() !== null) {
$integration->addContextToStacktraceFrames($maxContextLines, $exception->getStacktrace());
}
}
} finally {
$integration->excerptCache = [];
}

return $event;
Expand Down Expand Up @@ -122,6 +135,12 @@ private function addContextToStacktraceFrame(int $maxContextLines, Frame $frame)
*/
private function getSourceCodeExcerpt(int $maxContextLines, string $filePath, int $lineNumber): array
{
$cacheKey = $filePath . ':' . $lineNumber;

if (isset($this->excerptCache[$cacheKey])) {
return $this->excerptCache[$cacheKey];
}

$frame = [
'pre_context' => [],
'context_line' => null,
Expand Down Expand Up @@ -156,6 +175,8 @@ private function getSourceCodeExcerpt(int $maxContextLines, string $filePath, in

$file->next();
}

$this->excerptCache[$cacheKey] = $frame;
} catch (\Throwable $exception) {
$this->logger->warning(
\sprintf('Failed to get the source code excerpt for the file "%s".', $filePath),
Expand Down
Loading