From 3ad4620d0c68c6f12f44b09579d4903092f21c81 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Fri, 13 Jan 2023 08:54:50 -0600 Subject: [PATCH] Prevent Catcher from changing reporting if changed after Catcher init, fixes #6 --- lib/Catcher.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/Catcher.php b/lib/Catcher.php index a2d6ab9..0a8e942 100644 --- a/lib/Catcher.php +++ b/lib/Catcher.php @@ -23,6 +23,11 @@ class Catcher { /** When set to true Catcher will throw errors as throwables */ public bool $throwErrors = true; + /** + * Stores the error reporting level set by Catcher to compare against when + * unregistering + */ + protected ?int $errorReporting = null; /** * Array of handlers the exceptions are passed to * @@ -92,7 +97,14 @@ class Catcher { return false; } - error_reporting(error_reporting() & ~\E_ERROR); + // If the current error reporting level has E_ERROR then remove it and store for + // comparison when unregistering + $errorReporting = error_reporting(); + if ($errorReporting & \E_ERROR) { + $this->errorReporting = $errorReporting & ~\E_ERROR; + error_reporting($this->errorReporting); + } + set_error_handler([ $this, 'handleError' ]); set_exception_handler([ $this, 'handleThrowable' ]); register_shutdown_function([ $this, 'handleShutdown' ]); @@ -120,7 +132,15 @@ class Catcher { restore_error_handler(); restore_exception_handler(); - error_reporting(error_reporting() | \E_ERROR); + + // If error reporting has been set when registering and the error reporting level + // is the same as it was when it was set then add E_ERROR back to the error + $errorReporting = error_reporting(); + if ($this->errorReporting !== null && $this->errorReporting === $errorReporting) { + error_reporting($errorReporting | \E_ERROR); + } + $this->errorReporting = null; + $this->registered = false; return true; }