Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 0 additions & 12 deletions analysis-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -438,18 +438,6 @@ code = "unreachable-else-clause"
message = "Unreachable else clause"
count = 1

[[issues]]
file = "src/Integration/ModulesIntegration.php"
code = "no-value"
message = "Argument #1 passed to function `array_keys` has type `never`, meaning it cannot produce a value."
count = 1

[[issues]]
file = "src/Integration/ModulesIntegration.php"
code = "non-existent-class-like"
message = 'Class, interface, enum, or trait `PackageVersions\Versions` not found.'
count = 1

[[issues]]
file = "src/Integration/RequestIntegration.php"
code = "invalid-property-assignment-value"
Expand Down
1 change: 1 addition & 0 deletions mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ excludes = [
"tests/resources/**",
"tests/Fixtures/**",
"src/Util/ClockMock.php",
"vendor/open-telemetry/gen-otlp-protobuf/GPBMetadata/**",
]

[analyzer]
Expand Down
107 changes: 107 additions & 0 deletions src/CodeLocationResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace Sentry;

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(),
];
}
}
16 changes: 11 additions & 5 deletions src/Integration/ModulesIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Composer\InstalledVersions;
use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Sentry\Event;
use Sentry\SentrySdk;
use Sentry\State\Scope;
Expand Down Expand Up @@ -67,12 +66,19 @@ private static function getInstalledPackages(): array
return InstalledVersions::getInstalledPackages();
}

if (class_exists(Versions::class)) {
$versionsClass = 'PackageVersions\\Versions';

if (class_exists($versionsClass)) {
// BC layer for Composer 1, using a transient dependency
/** @var string[] $packages */
$packages = array_keys(Versions::VERSIONS);
/** @var mixed $versions */
$versions = \constant($versionsClass . '::VERSIONS');

if (\is_array($versions)) {
/** @var string[] $packages */
$packages = array_keys($versions);

return $packages;
return $packages;
}
}

// this should not happen
Expand Down
94 changes: 94 additions & 0 deletions tests/CodeLocationResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Sentry\Tests;

use PHPUnit\Framework\TestCase;
use Sentry\CodeLocationResolver;
use Sentry\Frame;
use Sentry\Options;
use Sentry\Serializer\RepresentationSerializer;

final class CodeLocationResolverTest extends TestCase
{
public function testFindFirstInAppFrameForBacktrace(): void
{
$expectedLine = 123;
$resolver = $this->createResolver([
'prefixes' => [],
]);

$frame = $resolver->findFirstInAppFrameForBacktrace($this->createQueryBacktrace($expectedLine));

$this->assertNotNull($frame);
$this->assertSame(__FILE__, $frame->getFile());
$this->assertSame($expectedLine, $frame->getLine());
$this->assertSame('App\\Repository\\UserRepository::findActiveUsers', $frame->getFunctionName());
}

public function testResolveFromBacktraceReturnsCodeLocationMetadata(): void
{
$expectedLine = 321;
$resolver = $this->createResolver([
'prefixes' => [\dirname(__DIR__)],
]);

$location = $resolver->resolveFromBacktrace($this->createQueryBacktrace($expectedLine));

$this->assertNotNull($location);
$this->assertSame(\DIRECTORY_SEPARATOR . 'tests' . \DIRECTORY_SEPARATOR . 'CodeLocationResolverTest.php', $location['code.filepath']);
$this->assertSame('App\\Repository\\UserRepository::findActiveUsers', $location['code.function']);
$this->assertSame($expectedLine, $location['code.lineno']);
}

public function testResolveFromBacktraceReturnsNullWithoutInAppFrame(): void
{
$resolver = $this->createResolver();

$location = $resolver->resolveFromBacktrace([
[
'file' => Frame::INTERNAL_FRAME_FILENAME,
'line' => 0,
'function' => 'internal',
],
[
'class' => 'Doctrine\\DBAL\\Connection',
'function' => 'executeQuery',
],
]);

$this->assertNull($location);
}

private function createResolver(array $options = []): CodeLocationResolver
{
$options = new Options($options);

return new CodeLocationResolver($options, new RepresentationSerializer($options));
}

/**
* @return array<int, array<string, mixed>>
*/
private function createQueryBacktrace(int $line): array
{
return [
[
'file' => Frame::INTERNAL_FRAME_FILENAME,
'line' => 0,
'function' => 'internal',
],
[
'file' => __FILE__,
'line' => $line,
'class' => 'Doctrine\\DBAL\\Connection',
'function' => 'executeQuery',
],
[
'class' => 'App\\Repository\\UserRepository',
'function' => 'findActiveUsers',
],
];
}
}
Loading