Skip to content

Commit

Permalink
Convert error handler function to callable
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Dec 16, 2023
1 parent c7a6151 commit 2314e68
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -13,7 +13,7 @@
"StrictList",
"StrictQueue",
"StrictStack",
"throwErrorException"
"ThrowErrorException"
],
"homepage": "https://github.com/opencultureconsulting/php-basics",
"readme": "README.md",
Expand All @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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;
}

0 comments on commit 2314e68

Please sign in to comment.