Skip to content

Commit

Permalink
pass errors to previous error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Feb 20, 2021
1 parent 66d001d commit 54c93db
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
11 changes: 11 additions & 0 deletions lib/Util/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Kcs\ClassFinder\Util;

use Error as PhpError;

class Error extends PhpError
{
}
21 changes: 9 additions & 12 deletions lib/Util/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Kcs\ClassFinder\Util;

use Error;

use function call_user_func_array;
use function error_reporting;
use function func_get_args;
use function restore_error_handler;
use function set_error_handler;

Expand All @@ -25,11 +25,14 @@ final class ErrorHandler
{
private static bool $registered = false;

/** @var callable */
private static $previous;

public static function handleError(int $errorNumber, string $errorString): bool
{
// Do not raise an exception when the error suppression operator (@) was used.
if (! ($errorNumber & error_reporting())) {
return false;
return call_user_func_array(self::$previous, func_get_args());
}

switch ($errorNumber) {
Expand All @@ -46,7 +49,7 @@ public static function handleError(int $errorNumber, string $errorString): bool
throw new Error($errorString, $errorNumber);
}

return false;
return call_user_func_array(self::$previous, func_get_args());
}

public static function register(): void
Expand All @@ -55,14 +58,7 @@ public static function register(): void
return;
}

$oldErrorHandler = set_error_handler([self::class, 'handleError']);

if ($oldErrorHandler !== null) {
restore_error_handler();

return;
}

self::$previous = set_error_handler([self::class, 'handleError']) ?? static fn () => false;
self::$registered = true;
}

Expand All @@ -73,5 +69,6 @@ public static function unregister(): void
}

restore_error_handler();
self::$registered = false;
}
}
32 changes: 32 additions & 0 deletions tests/Util/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace Kcs\ClassFinder\Tests\Util;

use Kcs\ClassFinder\Util\Error;
use Kcs\ClassFinder\Util\ErrorHandler;
use PHPUnit\Framework\TestCase;

class ErrorHandlerTest extends TestCase
{
protected function setUp(): void
{
ErrorHandler::register();
}

protected function tearDown(): void
{
ErrorHandler::unregister();
}

public function testShouldPassNonErrorsToPreviousErrorHandler(): void
{
$this->expectWarning();
trigger_error('This is a warning', E_USER_WARNING);
}

public function testShouldThrowErrorOnErrorOrUserError(): void
{
$this->expectException(Error::class);
trigger_error('This is an error', E_USER_ERROR);
}
}

0 comments on commit 54c93db

Please sign in to comment.