From 12df5e71329db1c618fd7fd00e497755b0808013 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Wed, 11 Jan 2023 16:56:06 -0600 Subject: [PATCH] Bit of cleanup --- lib/Catcher.php | 19 ++++++++++++++----- tests/cases/TestCatcher.php | 4 ++++ tests/cases/TestHandler.php | 2 ++ tests/cases/TestThrowableController.php | 1 + 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/Catcher.php b/lib/Catcher.php index 79c9f45..9453a38 100644 --- a/lib/Catcher.php +++ b/lib/Catcher.php @@ -166,7 +166,7 @@ class Catcher { // that if the error wouldn't normally stop execution the newly-created Error // throwable is thrown in a fork instead, allowing execution to resume in the // parent process. - if (in_array($code, [ \E_ERROR, \E_PARSE, \E_CORE_ERROR, \E_COMPILE_ERROR, \E_USER_ERROR ])) { + if ($this->isErrorFatal($code)) { throw $error; } elseif ($this->forking && \PHP_SAPI === 'cli' && function_exists('pcntl_fork')) { $pid = pcntl_fork(); @@ -214,10 +214,13 @@ class Catcher { $this->isShuttingDown || $controlCode & Handler::EXIT || $throwable instanceof \Exception || - ($throwable instanceof Error && in_array($throwable->getCode(), [ \E_ERROR, \E_PARSE, \E_CORE_ERROR, \E_COMPILE_ERROR, \E_USER_ERROR ])) || + ($throwable instanceof Error && $this->isErrorFatal($throwable->getCode())) || (!$throwable instanceof Error && $throwable instanceof \Error) ) { foreach ($this->handlers as $h) { + if ($this->isShuttingDown) { + $h->setOption('outputBacktrace', false); + } $h->dispatch(); } @@ -226,7 +229,7 @@ class Catcher { // Don't want to exit here when shutting down so any shutdown functions further // down the stack still run. if (!$this->preventExit && !$this->isShuttingDown) { - $this->exit($throwable->getCode()); + $this->exit(max($throwable->getCode(), 1)); } } @@ -246,23 +249,29 @@ class Catcher { $this->throwErrors = false; $this->isShuttingDown = true; if ($error = $this->getLastError()) { - if (in_array($error['type'], [ \E_ERROR, \E_PARSE, \E_CORE_ERROR, \E_CORE_WARNING, \E_COMPILE_ERROR, \E_COMPILE_WARNING ])) { + if ($this->isErrorFatal($error['type'])) { $this->handleError($error['type'], $error['message'], $error['file'], $error['line']); } } else { foreach ($this->handlers as $h) { + $h->setOption('outputBacktrace', false); $h->dispatch(); } } } - /** Exists so the method may be replaced when mocking in tests */ + /** Exists so exits can be tracked in mocks in testing */ protected function exit(int $status): void { // This won't be shown as executed in code coverage exit($status); // @codeCoverageIgnore } + /** Checks if the error code is fatal */ + protected function isErrorFatal(int $code): bool { + return in_array($code, [ \E_ERROR, \E_PARSE, \E_CORE_ERROR, \E_COMPILE_ERROR, \E_USER_ERROR, \E_RECOVERABLE_ERROR ]); + } + /** Exists so the method may be replaced when mocking in tests */ protected function getLastError(): ?array { return error_get_last(); diff --git a/tests/cases/TestCatcher.php b/tests/cases/TestCatcher.php index 30342ba..a4c14a8 100644 --- a/tests/cases/TestCatcher.php +++ b/tests/cases/TestCatcher.php @@ -58,6 +58,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase { * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::handleError * @covers \MensBeam\Foundation\Catcher::handleThrowable + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register * @covers \MensBeam\Foundation\Catcher::unregister @@ -311,6 +312,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase { * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::getLastThrowable * @covers \MensBeam\Foundation\Catcher::handleThrowable + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register * @covers \MensBeam\Foundation\Catcher::unregister @@ -402,6 +404,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase { * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::getLastThrowable * @covers \MensBeam\Foundation\Catcher::handleError + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register * @covers \MensBeam\Foundation\Catcher::unregister @@ -445,6 +448,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase { * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::getLastError * @covers \MensBeam\Foundation\Catcher::handleError + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::handleThrowable * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register diff --git a/tests/cases/TestHandler.php b/tests/cases/TestHandler.php index c7c38ac..7e741b6 100644 --- a/tests/cases/TestHandler.php +++ b/tests/cases/TestHandler.php @@ -87,6 +87,7 @@ class TestHandler extends \PHPUnit\Framework\TestCase { * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::handleError * @covers \MensBeam\Foundation\Catcher::handleThrowable + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register * @covers \MensBeam\Foundation\Catcher::unregister @@ -141,6 +142,7 @@ class TestHandler extends \PHPUnit\Framework\TestCase { * * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::handleError + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::handleThrowable * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register diff --git a/tests/cases/TestThrowableController.php b/tests/cases/TestThrowableController.php index 7a6707f..4b289f1 100644 --- a/tests/cases/TestThrowableController.php +++ b/tests/cases/TestThrowableController.php @@ -25,6 +25,7 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase { * @covers \MensBeam\Foundation\Catcher::__construct * @covers \MensBeam\Foundation\Catcher::getLastThrowable * @covers \MensBeam\Foundation\Catcher::handleError + * @covers \MensBeam\Foundation\Catcher::isErrorFatal * @covers \MensBeam\Foundation\Catcher::handleThrowable * @covers \MensBeam\Foundation\Catcher::pushHandler * @covers \MensBeam\Foundation\Catcher::register