Skip to content

Commit c65c2d9

Browse files
committed
php8: address ReflectionParameter::getClass deprecation
Adjustments for 7.1, support for union types, test suite updates Reinstated previous PHPUnit XML config (dupe of reactphp#177) Code tweaks and test adjustments Test tweaks _checkType tests cleanup
1 parent d3d4ec3 commit c65c2d9

File tree

6 files changed

+141
-47
lines changed

6 files changed

+141
-47
lines changed

src/functions.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,33 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool
342342
return true;
343343
}
344344

345-
$expectedClass = $parameters[0]->getClass();
345+
$type = $parameters[0]->getType();
346346

347-
if (!$expectedClass) {
347+
if (!$type) {
348348
return true;
349349
}
350350

351-
return $expectedClass->isInstance($reason);
351+
$types = [$type];
352+
353+
if ($type instanceof \ReflectionUnionType) {
354+
$types = $type->getTypes();
355+
}
356+
357+
$mismatched = false;
358+
359+
foreach ($types as $type) {
360+
if (!$type || $type->isBuiltin()) {
361+
continue;
362+
}
363+
364+
$expectedClass = $type->getName();
365+
366+
if ($reason instanceof $expectedClass) {
367+
return true;
368+
}
369+
370+
$mismatched = true;
371+
}
372+
373+
return !$mismatched;
352374
}

tests/ErrorCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public function start()
1010
{
1111
$errors = [];
1212

13-
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$errors) {
14-
$errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
13+
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$errors) {
14+
$errors[] = compact('errno', 'errstr', 'errfile', 'errline');
1515
});
1616

1717
$this->errors = &$errors;

tests/FunctionCheckTypehintTest.php

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,72 @@ public function shouldAcceptClosureCallbackWithTypehint()
1717
/** @test */
1818
public function shouldAcceptFunctionStringCallbackWithTypehint()
1919
{
20-
self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException()));
21-
self::assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new Exception()));
20+
self::assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new InvalidArgumentException()));
21+
self::assertFalse(_checkTypehint(new CallbackWithTypehintClass(), new Exception()));
2222
}
2323

2424
/** @test */
2525
public function shouldAcceptInvokableObjectCallbackWithTypehint()
2626
{
27-
self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException()));
28-
self::assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new Exception()));
27+
self::assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new InvalidArgumentException()));
28+
self::assertFalse(_checkTypehint(new CallbackWithTypehintClass(), new Exception()));
2929
}
3030

3131
/** @test */
3232
public function shouldAcceptObjectMethodCallbackWithTypehint()
3333
{
34-
self::assertTrue(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new InvalidArgumentException()));
35-
self::assertFalse(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new Exception()));
34+
self::assertTrue(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new InvalidArgumentException()));
35+
self::assertFalse(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new Exception()));
3636
}
3737

3838
/** @test */
3939
public function shouldAcceptStaticClassCallbackWithTypehint()
4040
{
41-
self::assertTrue(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
42-
self::assertFalse(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception()));
41+
self::assertTrue(_checkTypehint([CallbackWithTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
42+
self::assertFalse(_checkTypehint([CallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception()));
43+
}
44+
45+
/**
46+
* @test
47+
* @requires PHP 8
48+
*/
49+
public function shouldAcceptClosureCallbackWithUnionTypehint()
50+
{
51+
eval(
52+
'namespace React\Promise;' .
53+
'self::assertTrue(_checkTypehint(function (\RuntimeException|\InvalidArgumentException $e) {}, new \InvalidArgumentException()));' .
54+
'self::assertFalse(_checkTypehint(function (\RuntimeException|\InvalidArgumentException $e) {}, new \Exception()));'
55+
);
56+
}
57+
58+
/**
59+
* @test
60+
* @requires PHP 8
61+
*/
62+
public function shouldAcceptInvokableObjectCallbackWithUnionTypehint()
63+
{
64+
self::assertTrue(_checkTypehint(new CallbackWithUnionTypehintClass(), new InvalidArgumentException()));
65+
self::assertFalse(_checkTypehint(new CallbackWithUnionTypehintClass(), new Exception()));
66+
}
67+
68+
/**
69+
* @test
70+
* @requires PHP 8
71+
*/
72+
public function shouldAcceptObjectMethodCallbackWithUnionTypehint()
73+
{
74+
self::assertTrue(_checkTypehint([new CallbackWithUnionTypehintClass(), 'testCallback'], new InvalidArgumentException()));
75+
self::assertFalse(_checkTypehint([new CallbackWithUnionTypehintClass(), 'testCallback'], new Exception()));
76+
}
77+
78+
/**
79+
* @test
80+
* @requires PHP 8
81+
*/
82+
public function shouldAcceptStaticClassCallbackWithUnionTypehint()
83+
{
84+
self::assertTrue(_checkTypehint([CallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
85+
self::assertFalse(_checkTypehint([CallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new Exception()));
4386
}
4487

4588
/** @test */
@@ -52,25 +95,25 @@ public function shouldAcceptClosureCallbackWithoutTypehint()
5295
/** @test */
5396
public function shouldAcceptFunctionStringCallbackWithoutTypehint()
5497
{
55-
self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException()));
98+
self::assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new InvalidArgumentException()));
5699
}
57100

58101
/** @test */
59102
public function shouldAcceptInvokableObjectCallbackWithoutTypehint()
60103
{
61-
self::assertTrue(_checkTypehint(new TestCallbackWithoutTypehintClass(), new InvalidArgumentException()));
104+
self::assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new InvalidArgumentException()));
62105
}
63106

64107
/** @test */
65108
public function shouldAcceptObjectMethodCallbackWithoutTypehint()
66109
{
67-
self::assertTrue(_checkTypehint([new TestCallbackWithoutTypehintClass(), 'testCallback'], new InvalidArgumentException()));
110+
self::assertTrue(_checkTypehint([new CallbackWithoutTypehintClass(), 'testCallback'], new InvalidArgumentException()));
68111
}
69112

70113
/** @test */
71114
public function shouldAcceptStaticClassCallbackWithoutTypehint()
72115
{
73-
self::assertTrue(_checkTypehint([TestCallbackWithoutTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
116+
self::assertTrue(_checkTypehint([CallbackWithoutTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
74117
}
75118
}
76119

@@ -81,33 +124,3 @@ function testCallbackWithTypehint(InvalidArgumentException $e)
81124
function testCallbackWithoutTypehint()
82125
{
83126
}
84-
85-
class TestCallbackWithTypehintClass
86-
{
87-
public function __invoke(InvalidArgumentException $e)
88-
{
89-
}
90-
91-
public function testCallback(InvalidArgumentException $e)
92-
{
93-
}
94-
95-
public static function testCallbackStatic(InvalidArgumentException $e)
96-
{
97-
}
98-
}
99-
100-
class TestCallbackWithoutTypehintClass
101-
{
102-
public function __invoke()
103-
{
104-
}
105-
106-
public function testCallback()
107-
{
108-
}
109-
110-
public static function testCallbackStatic()
111-
{
112-
}
113-
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
use InvalidArgumentException;
6+
7+
class CallbackWithTypehintClass
8+
{
9+
public function __invoke(InvalidArgumentException $e)
10+
{
11+
}
12+
13+
public function testCallback(InvalidArgumentException $e)
14+
{
15+
}
16+
17+
public static function testCallbackStatic(InvalidArgumentException $e)
18+
{
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
use InvalidArgumentException;
6+
use RuntimeException;
7+
8+
class CallbackWithUnionTypehintClass
9+
{
10+
public function __invoke(RuntimeException|InvalidArgumentException $e)
11+
{
12+
}
13+
14+
public function testCallback(RuntimeException|InvalidArgumentException $e)
15+
{
16+
}
17+
18+
public static function testCallbackStatic(RuntimeException|InvalidArgumentException $e)
19+
{
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
class CallbackWithoutTypehintClass
6+
{
7+
public function __invoke()
8+
{
9+
}
10+
11+
public function testCallback()
12+
{
13+
}
14+
15+
public static function testCallbackStatic()
16+
{
17+
}
18+
}

0 commit comments

Comments
 (0)