psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / portal / patient / fwk / libs / verysimple / Util / ExceptionFormatter.php
blob5b1eebfcb6534a8cd744616fbe7c91421675b9b1
1 <?php
3 /** @package verysimple::Util */
5 /**
6 * Formatter for formatting Exceptions and stack traces
8 * @package verysimple::String
9 * @author Jason Hinkle
10 * @copyright 1997-2008 VerySimple, Inc.
11 * @license http://www.gnu.org/licenses/lgpl.html LGPL
12 * @version 1.0
14 class ExceptionFormatter
16 /**
17 * This is a utility function for tracing errors.
18 * It will return a string that
19 * displys the current execution stack
21 * @param string $msg
22 * a debugging message to include
23 * @param int $depth
24 * how far to go back in the stack (default = unlimited)
25 * @param string $join
26 * the delimiter between lines
27 * @param bool $show_lines
28 * true to include line numbers
30 static function GetTraceAsString($msg = "DEBUG", $depth = 0, $join = " :: ", $show_lines = true)
32 $error = new Exception($msg);
33 return self::FormatTrace($error->getTrace(), $depth, $join, $show_lines);
36 /**
37 * Formats the debug_backtrace array into a printable string.
39 * You can create a debug traceback using $exception->getTrace()
40 * or using the php debug_backtrace() function
42 * @access public
43 * @param
44 * array debug_backtrace. For example: debug_backtrace() -or- $exception->getTrace()
45 * @param int $depth
46 * how far to go back in the stack (default = unlimited)
47 * @param string $join
48 * the delimiter between lines
49 * @param bool $show_lines
50 * true to include line numbers
52 static function FormatTrace($tb, $depth = 0, $join = " :: ", $show_lines = true)
54 $msg = "";
55 $delim = "";
57 $calling_function = "";
58 $calling_line = "[?]";
59 $levels = count($tb);
61 if ($depth == 0) {
62 $depth = $levels;
65 for ($x = $levels; $x > 0; $x--) {
66 $stack = $tb [$x - 1];
67 $s_file = isset($stack ['file']) ? basename($stack ['file']) : "[?]";
68 $s_line = isset($stack ['line']) ? $stack ['line'] : "[?]";
69 $s_function = isset($stack ['function']) ? $stack ['function'] : "";
70 $s_class = isset($stack ['class']) ? $stack ['class'] : "";
71 $s_type = isset($stack ['type']) ? $stack ['type'] : "";
73 if ($depth >= $x) {
74 $msg .= $delim . "$calling_function" . ($show_lines ? " ($s_file Line $s_line)" : "");
75 $delim = $join;
78 $calling_function = $s_class . $s_type . $s_function;
81 return $msg;