Skip to content

Commit

Permalink
Merge pull request #50 from Thomas-Gelf/fix/php8.1-exception-signature
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus authored Dec 4, 2021
2 parents 099700b + d4a0b8c commit fe0cfcd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/TimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class TimeoutException extends RuntimeException
*/
public function __construct($timeout, $message = null, $code = null, $previous = null)
{
// Preserve compatibility with our former signature, but avoid invalid arguments for the parent constructor:
if ($message === null) {
$message = '';
}
if ($code === null) {
$code = 0;
}
parent::__construct($message, $code, $previous);

$this->timeout = $timeout;
Expand Down
33 changes: 33 additions & 0 deletions tests/TimeoutExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Tests\Promise\Timer;

use ErrorException;
use React\Promise\Timer\TimeoutException;

class TimeoutExceptionTest extends TestCase
Expand All @@ -12,4 +13,36 @@ public function testAccessTimeout()

$this->assertEquals(10, $e->getTimeout());
}

public function testEnsureNoDeprecationsAreTriggered()
{
$formerReporting = error_reporting();
error_reporting(E_ALL | E_STRICT);
$this->setStrictErrorHandling();

try {
$e = new TimeoutException(10);
} catch (ErrorException $e) {
error_reporting($formerReporting);
throw $e;
}

error_reporting($formerReporting);
$this->assertEquals(10, $e->getTimeout());
}

protected function setStrictErrorHandling()
{
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
if (! (error_reporting() & $errno)) {
return false;
}
switch ($errno) {
case E_DEPRECATED:
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

return false;
});
}
}

0 comments on commit fe0cfcd

Please sign in to comment.