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) [![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) [![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", "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", "type": "library",
"keywords": [ "keywords": [
"ArrayAccess", "ArrayAccess",
@ -13,7 +13,7 @@
"StrictList", "StrictList",
"StrictQueue", "StrictQueue",
"StrictStack", "StrictStack",
"throwErrorException" "ThrowErrorException"
], ],
"homepage": "https://github.com/opencultureconsulting/php-basics", "homepage": "https://github.com/opencultureconsulting/php-basics",
"readme": "README.md", "readme": "README.md",
@ -37,7 +37,7 @@
"require-dev": { "require-dev": {
"phpstan/phpstan": "^1.10", "phpstan/phpstan": "^1.10",
"phpstan/phpstan-strict-rules": "^1.5", "phpstan/phpstan-strict-rules": "^1.5",
"friendsofphp/php-cs-fixer": "^3.38" "friendsofphp/php-cs-fixer": "^3.41"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -20,17 +20,22 @@
declare(strict_types=1); declare(strict_types=1);
namespace OCC\Basics\Functions; namespace OCC\Basics\ErrorHandlers;
use ErrorException; 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> * @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package opencultureconsulting/basics * @package opencultureconsulting/basics
*/
class ThrowErrorException
{
/**
* Converts an internal PHP error into an ErrorException.
* *
* @param int $severity The severity of the error * @param int $severity The severity of the error
* @param string $message The error message * @param string $message The error message
@ -41,10 +46,11 @@ use ErrorException;
* *
* @throws ErrorException * @throws ErrorException
*/ */
function throwErrorException(int $severity = E_USER_ERROR, string $message = '', ?string $file = null, ?int $line = null): bool public function __invoke(int $severity = E_USER_ERROR, string $message = '', ?string $file = null, ?int $line = null): bool
{ {
if ((error_reporting() & $severity) > 0) { if ((error_reporting() & $severity) > 0) {
throw new ErrorException($message, 0, $severity, $file, $line); throw new ErrorException($message, 0, $severity, $file, $line);
} }
return false; return false;
} }
}