Skip to content

Commit 9ccdc9b

Browse files
authored
Merge pull request #52 from clue-labs/casts
Improve `TimeoutException` legacy API by using explicit type casts
2 parents fe0cfcd + 56936b4 commit 9ccdc9b

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

phpunit.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
66
bootstrap="vendor/autoload.php"
7+
cacheResult="false"
78
colors="true"
8-
cacheResult="false">
9+
convertDeprecationsToExceptions="true">
910
<testsuites>
1011
<testsuite name="Promise Timer Test Suite">
1112
<directory>./tests/</directory>

src/TimeoutException.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@ class TimeoutException extends RuntimeException
1111

1212
/**
1313
* @param float $timeout
14-
* @param ?string $message
15-
* @param ?int $code
14+
* @param string|null $message
15+
* @param int|null $code
1616
* @param null|\Exception|\Throwable $previous
1717
*/
18-
public function __construct($timeout, $message = null, $code = null, $previous = null)
18+
public function __construct($timeout, $message = '', $code = 0, $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-
}
27-
parent::__construct($message, $code, $previous);
20+
// Preserve compatibility with our former nullable signature, but avoid invalid arguments for the parent constructor:
21+
parent::__construct((string) $message, (int) $code, $previous);
2822

29-
$this->timeout = $timeout;
23+
$this->timeout = (float) $timeout;
3024
}
3125

3226
/**

tests/TimeoutExceptionTest.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,45 @@
22

33
namespace React\Tests\Promise\Timer;
44

5-
use ErrorException;
65
use React\Promise\Timer\TimeoutException;
76

87
class TimeoutExceptionTest extends TestCase
98
{
10-
public function testAccessTimeout()
9+
public function testCtorWithAllParameters()
1110
{
12-
$e = new TimeoutException(10);
11+
$previous = new \Exception();
12+
$e = new TimeoutException(1.0, 'Error', 42, $previous);
1313

14-
$this->assertEquals(10, $e->getTimeout());
14+
$this->assertEquals(1.0, $e->getTimeout());
15+
$this->assertEquals('Error', $e->getMessage());
16+
$this->assertEquals(42, $e->getCode());
17+
$this->assertSame($previous, $e->getPrevious());
1518
}
1619

17-
public function testEnsureNoDeprecationsAreTriggered()
20+
public function testCtorWithDefaultValues()
1821
{
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());
22+
$e = new TimeoutException(2.0);
23+
24+
$this->assertEquals(2.0, $e->getTimeout());
25+
$this->assertEquals('', $e->getMessage());
26+
$this->assertEquals(0, $e->getCode());
27+
$this->assertNull($e->getPrevious());
28+
}
29+
30+
public function testCtorWithIntTimeoutWillBeReturnedAsFloat()
31+
{
32+
$e = new TimeoutException(1);
33+
34+
$this->assertSame(1.0, $e->getTimeout());
3235
}
3336

34-
protected function setStrictErrorHandling()
37+
public function testLegacyCtorWithNullValues()
3538
{
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-
});
39+
$e = new TimeoutException(10, null, null, null);
40+
41+
$this->assertEquals(10.0, $e->getTimeout());
42+
$this->assertEquals('', $e->getMessage());
43+
$this->assertEquals(0, $e->getCode());
44+
$this->assertNull($e->getPrevious());
4745
}
4846
}

0 commit comments

Comments
 (0)