fix var names and references
[mdref/mdref] / mdref / ExceptionHandler.php
index b641629f3c9372a63697e29ae2c8ee738ae5af1f..eaab8ea433fbfbff8e11f6253b44e26256de4f94 100644 (file)
@@ -2,7 +2,24 @@
 
 namespace mdref;
 
-use http\Env as HTTP;
+use http\Env;
+use function debug_print_backtrace;
+use function error_get_last;
+use function headers_sent;
+use function implode;
+use function ob_end_clean;
+use function ob_get_clean;
+use function ob_get_level;
+use function ob_start;
+use function register_shutdown_function;
+use function set_error_handler;
+use function set_exception_handler;
+use function sprintf;
+use const E_COMPILE_ERROR;
+use const E_CORE_ERROR;
+use const E_ERROR;
+use const E_PARSE;
+use const E_USER_ERROR;
 
 /**
  * Exception and error handler
@@ -16,15 +33,19 @@ class ExceptionHandler
 
        /**
         * Set up error/exception/shutdown handler
+        * @param Env\Response $r
         */
-       public function __construct(\http\Env\Response $r) {
+       public function __construct(Env\Response $r) {
                $this->response = $r;
                set_exception_handler($this);
                set_error_handler($this);
                register_shutdown_function($this);
        }
 
-       private static function cleanBuffers() {
+       /**
+        * Clean output buffers
+        */
+       private static function cleanBuffers() : void {
                while (ob_get_level()) {
                        if (!@ob_end_clean()) {
                                break;
@@ -34,14 +55,18 @@ class ExceptionHandler
 
        /**
         * The exception/error/shutdown handler callback
+        *
+        * @param \Throwable|string $e
+        * @param ?string $msg
+        * @throws \Exception
         */
-       public function __invoke($e = null, $msg = null) {
-               if ($e instanceof \Exception || $e instanceof \Throwable) {
+       public function __invoke($e = null, ?string $msg = null) : void {
+               if ($e instanceof \Throwable) {
                        try {
                                self::cleanBuffers();
                                echo static::htmlException($e);
                        } catch (\Exception $ignore) {
-                               headers_sent() or HTTP::setResponseCode(500);
+                               headers_sent() or Env::setResponseCode(500);
                                die("FATAL ERROR:\n$e\n$ignore");
                        }
                } elseif (isset($e, $msg)) {
@@ -64,13 +89,14 @@ class ExceptionHandler
 
        /**
         * Format an exception as HTML and send appropriate exception info as HTTP headers
-        * @param Throwable $e
+        * @param \Throwable $e
         * @param array $title_tag
         * @param array $message_tag
         * @param array $trace_tag
         * @return string
         */
-       public static function htmlException(/*\Throwable*/ $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) {
+       public static function htmlException(\Throwable $e, array $title_tag = ["h1"], array $message_tag = ["p"],
+                       array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) : string {
                if ($e instanceof Exception) {
                        $code = $e->getCode() ?: 500;
                } else {
@@ -78,7 +104,7 @@ class ExceptionHandler
                }
                
                for ($html = ""; $e; $e = $e->getPrevious()) {
-                       $html .= static::htmlError(HTTP::getResponseStatusForCode($code),
+                       $html .= static::htmlError(Env::getResponseStatusForCode($code),
                                $e->getMessage(), $code, $e->getTraceAsString(), 
                                $title_tag, $message_tag, $trace_tag);
                }
@@ -96,8 +122,9 @@ class ExceptionHandler
         * @param array $trace_tag
         * @return string
         */
-       public static function htmlError($title, $message, $code, $trace = null, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) {
-               HTTP::setResponseCode($code);
+       public static function htmlError($title, $message, $code, $trace = null, array $title_tag = ["h1"],
+                       array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) : string {
+               Env::setResponseCode($code);
                
                $html = sprintf("<%s>%s</%s>\n<%s>%s</%s>\n",
                                implode(" ", $title_tag), $title, $title_tag[0],