Skip to content

Commit

Permalink
fix #5: do not panic if previous error handler returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Mar 4, 2022
1 parent 13958c9 commit 053c95c
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 381 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
},
"extra": {
"branch-alias": {
Expand Down
737 changes: 359 additions & 378 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/Util/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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 call_user_func_array(self::$previous, func_get_args());
return call_user_func_array(self::$previous, func_get_args()) ?? false;
}

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

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

public static function register(): void
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/</directory>
<directory suffix=".phpt">tests/</directory>
</testsuite>
</testsuites>
</phpunit>
13 changes: 13 additions & 0 deletions tests/Util/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@ public function testShouldPassErrorsToPreviousErrorHandlerIfSilenced(): void
self::assertNotNull($error);
self::assertEquals('unlink(this_file_does_not_exist.bad_idea): No such file or directory', $error[1]);
}

public function testShouldNotCrashIfPreviousErrorHandlerReturnsNullOrVoid(): void
{
$this->expectNotToPerformAssertions();

ErrorHandler::unregister();
$previous = set_error_handler(static function () use (&$previous): void {
call_user_func_array($previous, func_get_args());
});

ErrorHandler::register();
@unlink('this_file_does_not_exist.bad_idea');
}
}
15 changes: 15 additions & 0 deletions tests/Util/error-handler-user-error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
ErrorHandler - Test User Error does throw Error
--FILE--
<?php

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';

ErrorHandler::register();
trigger_error('This is an error', E_USER_ERROR);

?>
--EXPECTF--
Fatal error: Uncaught Kcs\ClassFinder\Util\Error: This is an error in %a
15 changes: 15 additions & 0 deletions tests/Util/error-handler-user-notice.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
ErrorHandler - Test User Notice does not throw Error
--FILE--
<?php

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';

ErrorHandler::register();
trigger_error('This is a notice', E_USER_NOTICE);

?>
--EXPECTF--
Notice: This is a notice in %a
15 changes: 15 additions & 0 deletions tests/Util/error-handler-user-warning.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
ErrorHandler - Test User Warning does not throw Error
--FILE--
<?php

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';

ErrorHandler::register();
trigger_error('This is a warning', E_USER_WARNING);

?>
--EXPECTF--
Warning: This is a warning in %a
17 changes: 17 additions & 0 deletions tests/Util/error-handler-void-previous-handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
ErrorHandler - Test User Notice does not panic if previous handler returns void
--FILE--
<?php

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';

set_error_handler(static function (): void {});

ErrorHandler::register();
trigger_error('This is a notice', E_USER_NOTICE);

?>
--EXPECTF--
Notice: This is a notice in %a

0 comments on commit 053c95c

Please sign in to comment.