From 012c84655d931f68bfcffc916b374d706511d927 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Mon, 26 Sep 2022 12:53:44 +0200 Subject: [PATCH] LastErrorException.php and test.php --- src/LastErrorException/LastErrorException.php | 44 +++++++++++++++++++ tests/test.php | 19 ++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/LastErrorException/LastErrorException.php create mode 100644 tests/test.php diff --git a/src/LastErrorException/LastErrorException.php b/src/LastErrorException/LastErrorException.php new file mode 100644 index 0000000..03f17e7 --- /dev/null +++ b/src/LastErrorException/LastErrorException.php @@ -0,0 +1,44 @@ + 0, + 'message' => '', + 'file' => __FILE__, + 'line' => __LINE__, + ]; + } + parent::__construct($lastError['message'], $code, $lastError['type'], $lastError['file'], $lastError['line']); + } + + /** + * Create the exception if there is a last error + * + * @param bool $checkErrorReporting Check if last error type match {@see https://www.php.net/manual/function.error-reporting error_reporting()} + * + * @return static|null The exception or Null if no last error + */ + public static function createFromLastError (bool $checkErrorReporting = true): ?static { + if (($lastError = error_get_last()) === null) { + return null; + } + if ($checkErrorReporting && !($lastError['type'] & error_reporting())) { + return null; + } + return new static(); + } +} \ No newline at end of file diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..98909ff --- /dev/null +++ b/tests/test.php @@ -0,0 +1,19 @@ +getMessage() + . PHP_EOL . 'in ' . $e->getFile() . '(' . $e->getLine() . ')' + . PHP_EOL . $e->getTraceAsString() + ); +} \ No newline at end of file