public function __construct($level = 1) { $data = []; $data['caller'] = CodeCaller::fromBacktrace(debug_backtrace(), $level); $msg = "unreachable code executed at " . $data['caller']['file'] . ':' . $data['caller']['line']; parent::__construct(500, $msg, $data); }
public function __construct($value, $reason = null) { $data = []; $data['value'] = $value; $data['reason'] = $reason; $data['caller'] = CodeCaller::fromBacktrace(debug_backtrace(), 2); $msg = "contract failed at " . $data['caller']['file'] . ':' . $data['caller']['line'] . "; failed value is: " . print_r($value, true); if ($reason !== null) { $msg .= '; contract is: ' . $reason; } parent::__construct(500, $msg, $data); }
/** * @covers ::fromBacktrace */ public function testRetrievesFileAndLineToo() { // ---------------------------------------------------------------- // setup your test // we don't call debug_backtrace() directly here, because the whole // point of the CodeCaller is to workout who called whatever code // provides the debug_backtrace() result $backtrace = $this->getBacktrace(); $expectedFile = __FILE__; $expectedLine = 92; // ---------------------------------------------------------------- // perform the change // this is a '0' here because of the perculiar way in which PHPUnit // calls tests methods ... it really challenges the PHP call stack // data $actualCaller = CodeCaller::fromBacktrace($backtrace, 0); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedFile, $actualCaller[2]); $this->assertEquals($expectedLine, $actualCaller[3]); }
/** * work out who is throwing the exception * * @param int $level * how deep into the backtrace we need to go * * this is relative to the caller * @return array * the calling class, and the calling method */ private function getCaller($level = 1) { // let's find out who is trying to throw this exception $backtrace = debug_backtrace(); return CodeCaller::fromBacktrace($backtrace, $level + 1); }