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
4 changes: 3 additions & 1 deletion src/Stacktrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ 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['class'])) {
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
$functionName = $backtraceFrame['class'];
} elseif (isset($backtraceFrame['function'])) {
$functionName = $backtraceFrame['function'];
} else {
Expand Down
5 changes: 4 additions & 1 deletion tests/Fixtures/backtraces/anonymous_frame.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"function": "triggerError",
"class": "TestClass"
},
{
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
"class": "CrashyClass"
},
{
"file": "path/to/file",
"line": 7,
"function": "call_user_func"
}
]
}
}
7 changes: 6 additions & 1 deletion tests/Fixtures/backtraces/exception.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"file": "path/to/file",
"line": 12,
"backtrace": [
{
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
"file": "path/to/file",
"line": 30,
"class": "CrashyClass"
},
{
"file": "path/to/file",
"function": "triggerError",
Expand All @@ -15,4 +20,4 @@
"function": "crashyFunction"
}
]
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/frames/class.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"file": "path/to/file",
"line": 12,
"class": "CrashyClass"
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 17 additions & 10 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' => 2, 'class' => 'CrashyClass']);
adnedelcu marked this conversation as resolved.
Show resolved Hide resolved
$stacktrace->addFrame('path/to/file', 2, ['file' => 'path/to/file', 'line' => 1, 'function' => 'test_function']);
adnedelcu marked this conversation as resolved.
Show resolved Hide resolved
$stacktrace->addFrame('path/to/file', 3, ['file' => 'path/to/file', 'line' => 2, 'function' => 'test_function', 'class' => 'TestClass']);
adnedelcu marked this conversation as resolved.
Show resolved Hide resolved

$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], 'CrashyClass', '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' => 2, 'class' => 'CrashyClass']);
ste93cry marked this conversation as resolved.
Show resolved Hide resolved

$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/class.json'),
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
];

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], 'CrashyClass', 'path/to/file', 12);
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
$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 Expand Up @@ -227,7 +232,8 @@ public function testFromBacktrace(): void

$this->assertFrameEquals($frames[0], null, 'path/to/file', 16);
$this->assertFrameEquals($frames[1], 'TestClass::crashyFunction', 'path/to/file', 7);
$this->assertFrameEquals($frames[2], 'TestClass::triggerError', 'path/to/file', 12);
$this->assertFrameEquals($frames[2], 'TestClass::triggerError', 'path/to/file', 30);
$this->assertFrameEquals($frames[3], 'CrashyClass', 'path/to/file', 12);
}

public function testFromBacktraceWithAnonymousFrame(): void
Expand All @@ -237,7 +243,8 @@ public function testFromBacktraceWithAnonymousFrame(): void

$this->assertFrameEquals($frames[0], null, 'path/to/file', 7);
$this->assertFrameEquals($frames[1], 'call_user_func', '[internal]', 0);
$this->assertFrameEquals($frames[2], 'TestClass::triggerError', 'path/to/file', 12);
$this->assertFrameEquals($frames[2], 'CrashyClass', '[internal]', 0);
$this->assertFrameEquals($frames[3], 'TestClass::triggerError', 'path/to/file', 12);
}

public function testInAppWithEmptyFrame(): void
Expand Down