Convert error handler function to callable

This commit is contained in:
Sebastian Meyer 2023-12-16 21:15:47 +01:00
parent c7a615143e
commit 2314e685a3
3 changed files with 26 additions and 20 deletions

View File

@ -3,4 +3,4 @@
[![PHPStan](https://github.com/opencultureconsulting/php-basics/actions/workflows/phpstan.yml/badge.svg)](https://github.com/opencultureconsulting/php-basics/actions/workflows/phpstan.yml)
[![PHPMD](https://github.com/opencultureconsulting/php-basics/actions/workflows/phpmd.yml/badge.svg)](https://github.com/opencultureconsulting/php-basics/actions/workflows/phpmd.yml)
This is a collection of generic [Classes](https://www.php.net/manual/en/language.oop5.php), [Functions](https://www.php.net/manual/en/language.functions.php) and useful [Traits](https://www.php.net/manual/en/language.oop5.traits.php) for your PHP projects.
This is a collection of generic [Classes](https://www.php.net/manual/en/language.oop5.php) and useful [Traits](https://www.php.net/manual/en/language.oop5.traits.php) for your PHP projects.

View File

@ -1,6 +1,6 @@
{
"name": "opencultureconsulting/basics",
"description": "This is a collection of generic Classes, Functions and useful Traits for PHP projects.",
"description": "This is a collection of generic Classes and useful Traits for PHP projects.",
"type": "library",
"keywords": [
"ArrayAccess",
@ -13,7 +13,7 @@
"StrictList",
"StrictQueue",
"StrictStack",
"throwErrorException"
"ThrowErrorException"
],
"homepage": "https://github.com/opencultureconsulting/php-basics",
"readme": "README.md",
@ -37,7 +37,7 @@
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-strict-rules": "^1.5",
"friendsofphp/php-cs-fixer": "^3.38"
"friendsofphp/php-cs-fixer": "^3.41"
},
"autoload": {
"psr-4": {

View File

@ -20,31 +20,37 @@
declare(strict_types=1);
namespace OCC\Basics\Functions;
namespace OCC\Basics\ErrorHandlers;
use ErrorException;
/**
* Converts an internal PHP error into an ErrorException.
* Throw internal errors as exceptions.
*
* Usage: set_error_handler('\\OCC\\Basics\\Functions\\throwErrorException');
* Usage: set_error_handler(new ThrowErrorException());
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package opencultureconsulting/basics
*
* @param int $severity The severity of the error
* @param string $message The error message
* @param ?string $file The name of the file the error was raised in
* @param ?int $line The line number the error was raised in
*
* @return bool Always returns FALSE when not throwing an exception
*
* @throws ErrorException
*/
function throwErrorException(int $severity = E_USER_ERROR, string $message = '', ?string $file = null, ?int $line = null): bool
class ThrowErrorException
{
if ((error_reporting() & $severity) > 0) {
throw new ErrorException($message, 0, $severity, $file, $line);
/**
* Converts an internal PHP error into an ErrorException.
*
* @param int $severity The severity of the error
* @param string $message The error message
* @param ?string $file The name of the file the error was raised in
* @param ?int $line The line number the error was raised in
*
* @return bool Always returns FALSE when not throwing an exception
*
* @throws ErrorException
*/
public function __invoke(int $severity = E_USER_ERROR, string $message = '', ?string $file = null, ?int $line = null): bool
{
if ((error_reporting() & $severity) > 0) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
return false;
}
return false;
}