diff --git a/src/Stacktrace.php b/src/Stacktrace.php index cf0e68e46..370bfe9ae 100644 --- a/src/Stacktrace.php +++ b/src/Stacktrace.php @@ -115,7 +115,7 @@ public function addFrame(string $file, int $line, array $backtraceFrame): void $line = (int) $matches[2]; } - if (isset($backtraceFrame['class'])) { + if (isset($backtraceFrame['class']) && isset($backtraceFrame['function'])) { $functionName = sprintf('%s::%s', $backtraceFrame['class'], $backtraceFrame['function']); } elseif (isset($backtraceFrame['function'])) { $functionName = $backtraceFrame['function']; diff --git a/tests/Fixtures/backtraces/anonymous_frame.json b/tests/Fixtures/backtraces/anonymous_frame.json index 84eaa9898..2f450a5db 100644 --- a/tests/Fixtures/backtraces/anonymous_frame.json +++ b/tests/Fixtures/backtraces/anonymous_frame.json @@ -12,4 +12,4 @@ "function": "call_user_func" } ] -} \ No newline at end of file +} diff --git a/tests/Fixtures/backtraces/exception.json b/tests/Fixtures/backtraces/exception.json index 543157330..f371c73e5 100644 --- a/tests/Fixtures/backtraces/exception.json +++ b/tests/Fixtures/backtraces/exception.json @@ -15,4 +15,4 @@ "function": "crashyFunction" } ] -} \ No newline at end of file +} diff --git a/tests/Fixtures/frames/missing_function_key.json b/tests/Fixtures/frames/missing_function_key.json new file mode 100644 index 000000000..898b752e7 --- /dev/null +++ b/tests/Fixtures/frames/missing_function_key.json @@ -0,0 +1,5 @@ +{ + "file": "path/to/file", + "line": 12, + "class": "TestClass" +} diff --git a/tests/StacktraceTest.php b/tests/StacktraceTest.php index 7c5bd2849..08222be4a 100644 --- a/tests/StacktraceTest.php +++ b/tests/StacktraceTest.php @@ -39,15 +39,17 @@ public function testGetFramesAndToArray(): void { $stacktrace = new Stacktrace($this->options, $this->serializer, $this->representationSerializer); - $stacktrace->addFrame('path/to/file', 1, ['file' => 'path/to/file', 'line' => 1, 'function' => 'test_function']); - $stacktrace->addFrame('path/to/file', 2, ['file' => 'path/to/file', 'line' => 2, 'function' => 'test_function', 'class' => 'TestClass']); + $stacktrace->addFrame('path/to/file', 1, ['file' => 'path/to/file', 'line' => 1, 'class' => 'TestClass']); + $stacktrace->addFrame('path/to/file', 2, ['file' => 'path/to/file', 'line' => 2, 'function' => 'test_function']); + $stacktrace->addFrame('path/to/file', 3, ['file' => 'path/to/file', 'line' => 3, 'function' => 'test_function', 'class' => 'TestClass']); $frames = $stacktrace->getFrames(); - $this->assertCount(2, $frames); + $this->assertCount(3, $frames); $this->assertEquals($frames, $stacktrace->toArray()); - $this->assertFrameEquals($frames[0], 'TestClass::test_function', 'path/to/file', 2); - $this->assertFrameEquals($frames[1], 'test_function', 'path/to/file', 1); + $this->assertFrameEquals($frames[0], 'TestClass::test_function', 'path/to/file', 3); + $this->assertFrameEquals($frames[1], 'test_function', 'path/to/file', 2); + $this->assertFrameEquals($frames[2], null, 'path/to/file', 1); } public function testStacktraceJsonSerialization(): void @@ -56,6 +58,7 @@ public function testStacktraceJsonSerialization(): void $stacktrace->addFrame('path/to/file', 1, ['file' => 'path/to/file', 'line' => 1, 'function' => 'test_function']); $stacktrace->addFrame('path/to/file', 2, ['file' => 'path/to/file', 'line' => 2, 'function' => 'test_function', 'class' => 'TestClass']); + $stacktrace->addFrame('path/to/file', 3, ['file' => 'path/to/file', 'line' => 3, 'class' => 'TestClass']); $frames = json_encode($stacktrace->getFrames()); $serializedStacktrace = json_encode($stacktrace); @@ -72,6 +75,7 @@ public function testAddFrame(): void $this->getJsonFixture('frames/eval.json'), $this->getJsonFixture('frames/runtime_created.json'), $this->getJsonFixture('frames/function.json'), + $this->getJsonFixture('frames/missing_function_key.json'), ]; foreach ($frames as $frame) { @@ -80,10 +84,11 @@ public function testAddFrame(): void $frames = $stacktrace->getFrames(); - $this->assertCount(3, $frames); - $this->assertFrameEquals($frames[0], 'TestClass::test_function', 'path/to/file', 12); - $this->assertFrameEquals($frames[1], 'test_function', 'path/to/file', 12); + $this->assertCount(4, $frames); + $this->assertFrameEquals($frames[0], null, 'path/to/file', 12); + $this->assertFrameEquals($frames[1], 'TestClass::test_function', 'path/to/file', 12); $this->assertFrameEquals($frames[2], 'test_function', 'path/to/file', 12); + $this->assertFrameEquals($frames[3], 'test_function', 'path/to/file', 12); } public function testAddFrameSerializesMethodArguments(): void