Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined index generated by missing function in class #823

Merged
merged 9 commits into from
Jul 31, 2019
2 changes: 1 addition & 1 deletion src/Stacktrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/backtraces/anonymous_frame.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"function": "call_user_func"
}
]
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/backtraces/exception.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"function": "crashyFunction"
}
]
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/frames/missing_function_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"file": "path/to/file",
"line": 12,
"class": "TestClass"
}
21 changes: 13 additions & 8 deletions tests/StacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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
Expand Down