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
2 changes: 1 addition & 1 deletion analysis-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ count = 10
file = "src/Profiling/ExcimerLogConverter.php"
code = "mixed-assignment"
message = "Assigning `nonnull` type to a variable may lead to unexpected behavior."
count = 1
count = 3

[[issues]]
file = "src/Profiling/ExcimerLogConverter.php"
Expand Down
8 changes: 5 additions & 3 deletions src/Profiling/ExcimerLogConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class ExcimerLogConverter
public static function convert(iterable $entries, ?Options $options = null): ProfileData
{
$frames = [];
// 2D array where the first key is the file and the second is the line
// 4D array keyed by file, line, class and function, as different functions can share a line (e.g. closures)
$frameIndices = [];

$stacks = [];
Expand All @@ -65,8 +65,10 @@ public static function convert(iterable $entries, ?Options $options = null): Pro
foreach ($trace as $frame) {
$absolutePath = $frame['file'];
$lineno = $frame['line'];
$className = $frame['class'] ?? '';
$functionName = $frame['function'] ?? '';

$frameIndex = $frameIndices[$absolutePath][$lineno] ?? null;
$frameIndex = $frameIndices[$absolutePath][$lineno][$className][$functionName] ?? null;

if ($frameIndex === null) {
$file = self::stripPrefixFromFilePath($options, $absolutePath);
Expand All @@ -87,7 +89,7 @@ public static function convert(iterable $entries, ?Options $options = null): Pro
}

$frameIndex = \count($frames);
$frameIndices[$absolutePath][$lineno] = $frameIndex;
$frameIndices[$absolutePath][$lineno][$className][$functionName] = $frameIndex;
$frames[] = new ProfileFrame($absolutePath, $file, $function, $module, $lineno);
}

Expand Down
75 changes: 75 additions & 0 deletions tests/Profiling/ExcimerLogConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,80 @@ public static function convertDataProvider(): \Generator
]
),
];

yield 'Closures on the same line as their caller are not merged into the caller frame' => [
[
[
'trace' => [
[
'file' => '/var/www/html/index.php',
'line' => 10,
'function' => '{closure}',
'closure_line' => 10,
],
[
'file' => '/var/www/html/index.php',
'line' => 10,
'class' => 'App\\Controller',
'function' => 'index',
],
],
'timestamp' => 0.001,
],
],
new ProfileData(
[
new ProfileFrame('/var/www/html/index.php', '/var/www/html/index.php', '{closure}', null, 10),
new ProfileFrame('/var/www/html/index.php', '/var/www/html/index.php', 'App\\Controller::index', 'App\\Controller', 10),
],
[
[0, 1],
],
[
new ProfileSample(0, 0.001),
]
),
];

yield 'Trait methods used by different classes are not merged' => [
[
[
'trace' => [
[
'file' => '/var/www/html/ForwardsCalls.php',
'line' => 5,
'class' => 'App\\Foo',
'function' => 'forwardCall',
],
],
'timestamp' => 0.001,
],
[
'trace' => [
[
'file' => '/var/www/html/ForwardsCalls.php',
'line' => 5,
'class' => 'App\\Bar',
'function' => 'forwardCall',
],
],
'timestamp' => 0.002,
],
],
new ProfileData(
[
new ProfileFrame('/var/www/html/ForwardsCalls.php', '/var/www/html/ForwardsCalls.php', 'App\\Foo::forwardCall', 'App\\Foo', 5),
new ProfileFrame('/var/www/html/ForwardsCalls.php', '/var/www/html/ForwardsCalls.php', 'App\\Bar::forwardCall', 'App\\Bar', 5),
],
[
[0],
[1],
],
[
new ProfileSample(0, 0.001),
new ProfileSample(1, 0.002),
]
),
];
}
}
Loading