From 2314e685a30aa8129534a48c6d42a0ef03d56451 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Sat, 16 Dec 2023 21:15:47 +0100 Subject: [PATCH] Convert error handler function to callable --- README.md | 2 +- composer.json | 6 +-- .../ThrowErrorException.php} | 38 +++++++++++-------- 3 files changed, 26 insertions(+), 20 deletions(-) rename src/{Functions/throwErrorException.php => ErrorHandlers/ThrowErrorException.php} (55%) diff --git a/README.md b/README.md index 900ecc6..344f125 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index ac36fc4..7dfafeb 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Functions/throwErrorException.php b/src/ErrorHandlers/ThrowErrorException.php similarity index 55% rename from src/Functions/throwErrorException.php rename to src/ErrorHandlers/ThrowErrorException.php index e1321d2..15e1da6 100644 --- a/src/Functions/throwErrorException.php +++ b/src/ErrorHandlers/ThrowErrorException.php @@ -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 * @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; }