Skip to content

Address deprecation of ReflectionType::getClass() #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,33 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool
return true;
}

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

if (!$expectedClass) {
if (!$type) {
return true;
}

return $expectedClass->isInstance($reason);
$types = [$type];

if ($type instanceof \ReflectionUnionType) {
$types = $type->getTypes();
}

$mismatched = false;

foreach ($types as $type) {
if (!$type || $type->isBuiltin()) {
continue;
}

$expectedClass = $type->getName();

if ($reason instanceof $expectedClass) {
return true;
}

$mismatched = true;
}

return !$mismatched;
}
4 changes: 2 additions & 2 deletions tests/ErrorCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public function start()
{
$errors = [];

set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$errors) {
$errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$errors) {
$errors[] = compact('errno', 'errstr', 'errfile', 'errline');
});

$this->errors = &$errors;
Expand Down
97 changes: 55 additions & 42 deletions tests/FunctionCheckTypehintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,72 @@ public function shouldAcceptClosureCallbackWithTypehint()
/** @test */
public function shouldAcceptFunctionStringCallbackWithTypehint()
{
self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException()));
self::assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new Exception()));
self::assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new InvalidArgumentException()));
self::assertFalse(_checkTypehint(new CallbackWithTypehintClass(), new Exception()));
}

/** @test */
public function shouldAcceptInvokableObjectCallbackWithTypehint()
{
self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException()));
self::assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new Exception()));
self::assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new InvalidArgumentException()));
self::assertFalse(_checkTypehint(new CallbackWithTypehintClass(), new Exception()));
}

/** @test */
public function shouldAcceptObjectMethodCallbackWithTypehint()
{
self::assertTrue(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new Exception()));
self::assertTrue(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new Exception()));
}

/** @test */
public function shouldAcceptStaticClassCallbackWithTypehint()
{
self::assertTrue(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception()));
self::assertTrue(_checkTypehint([CallbackWithTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([CallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception()));
}

/**
* @test
* @requires PHP 8
*/
public function shouldAcceptClosureCallbackWithUnionTypehint()
{
eval(
'namespace React\Promise;' .
'self::assertTrue(_checkTypehint(function (\RuntimeException|\InvalidArgumentException $e) {}, new \InvalidArgumentException()));' .
'self::assertFalse(_checkTypehint(function (\RuntimeException|\InvalidArgumentException $e) {}, new \Exception()));'
);
}

/**
* @test
* @requires PHP 8
*/
public function shouldAcceptInvokableObjectCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint(new CallbackWithUnionTypehintClass(), new InvalidArgumentException()));
self::assertFalse(_checkTypehint(new CallbackWithUnionTypehintClass(), new Exception()));
}

/**
* @test
* @requires PHP 8
*/
public function shouldAcceptObjectMethodCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint([new CallbackWithUnionTypehintClass(), 'testCallback'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([new CallbackWithUnionTypehintClass(), 'testCallback'], new Exception()));
}

/**
* @test
* @requires PHP 8
*/
public function shouldAcceptStaticClassCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint([CallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([CallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new Exception()));
}

/** @test */
Expand All @@ -52,25 +95,25 @@ public function shouldAcceptClosureCallbackWithoutTypehint()
/** @test */
public function shouldAcceptFunctionStringCallbackWithoutTypehint()
{
self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException()));
self::assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new InvalidArgumentException()));
}

/** @test */
public function shouldAcceptInvokableObjectCallbackWithoutTypehint()
{
self::assertTrue(_checkTypehint(new TestCallbackWithoutTypehintClass(), new InvalidArgumentException()));
self::assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new InvalidArgumentException()));
}

/** @test */
public function shouldAcceptObjectMethodCallbackWithoutTypehint()
{
self::assertTrue(_checkTypehint([new TestCallbackWithoutTypehintClass(), 'testCallback'], new InvalidArgumentException()));
self::assertTrue(_checkTypehint([new CallbackWithoutTypehintClass(), 'testCallback'], new InvalidArgumentException()));
}

/** @test */
public function shouldAcceptStaticClassCallbackWithoutTypehint()
{
self::assertTrue(_checkTypehint([TestCallbackWithoutTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
self::assertTrue(_checkTypehint([CallbackWithoutTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
}
}

Expand All @@ -81,33 +124,3 @@ function testCallbackWithTypehint(InvalidArgumentException $e)
function testCallbackWithoutTypehint()
{
}

class TestCallbackWithTypehintClass
{
public function __invoke(InvalidArgumentException $e)
{
}

public function testCallback(InvalidArgumentException $e)
{
}

public static function testCallbackStatic(InvalidArgumentException $e)
{
}
}

class TestCallbackWithoutTypehintClass
{
public function __invoke()
{
}

public function testCallback()
{
}

public static function testCallbackStatic()
{
}
}
20 changes: 20 additions & 0 deletions tests/fixtures/CallbackWithTypehintClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace React\Promise;

use InvalidArgumentException;

class CallbackWithTypehintClass
{
public function __invoke(InvalidArgumentException $e)
{
}

public function testCallback(InvalidArgumentException $e)
{
}

public static function testCallbackStatic(InvalidArgumentException $e)
{
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/CallbackWithUnionTypehintClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace React\Promise;

use InvalidArgumentException;
use RuntimeException;

class CallbackWithUnionTypehintClass
{
public function __invoke(RuntimeException|InvalidArgumentException $e)
{
}

public function testCallback(RuntimeException|InvalidArgumentException $e)
{
}

public static function testCallbackStatic(RuntimeException|InvalidArgumentException $e)
{
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/CallbackWithoutTypehintClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace React\Promise;

class CallbackWithoutTypehintClass
{
public function __invoke()
{
}

public function testCallback()
{
}

public static function testCallbackStatic()
{
}
}