Skip to content

Commit fe0cfcd

Browse files
authored
Merge pull request #50 from Thomas-Gelf/fix/php8.1-exception-signature
2 parents 099700b + d4a0b8c commit fe0cfcd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/TimeoutException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class TimeoutException extends RuntimeException
1717
*/
1818
public function __construct($timeout, $message = null, $code = null, $previous = null)
1919
{
20+
// Preserve compatibility with our former signature, but avoid invalid arguments for the parent constructor:
21+
if ($message === null) {
22+
$message = '';
23+
}
24+
if ($code === null) {
25+
$code = 0;
26+
}
2027
parent::__construct($message, $code, $previous);
2128

2229
$this->timeout = $timeout;

tests/TimeoutExceptionTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Tests\Promise\Timer;
44

5+
use ErrorException;
56
use React\Promise\Timer\TimeoutException;
67

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

1314
$this->assertEquals(10, $e->getTimeout());
1415
}
16+
17+
public function testEnsureNoDeprecationsAreTriggered()
18+
{
19+
$formerReporting = error_reporting();
20+
error_reporting(E_ALL | E_STRICT);
21+
$this->setStrictErrorHandling();
22+
23+
try {
24+
$e = new TimeoutException(10);
25+
} catch (ErrorException $e) {
26+
error_reporting($formerReporting);
27+
throw $e;
28+
}
29+
30+
error_reporting($formerReporting);
31+
$this->assertEquals(10, $e->getTimeout());
32+
}
33+
34+
protected function setStrictErrorHandling()
35+
{
36+
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
37+
if (! (error_reporting() & $errno)) {
38+
return false;
39+
}
40+
switch ($errno) {
41+
case E_DEPRECATED:
42+
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
43+
}
44+
45+
return false;
46+
});
47+
}
1548
}

0 commit comments

Comments
 (0)