Skip to content
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

Support union types and address deprecation of ReflectionType::getClass() (PHP 8+ / Promise v2) #198

Merged
merged 3 commits into from
Nov 6, 2021
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
strategy:
matrix:
php:
- 8.0
- 7.4
- 7.3
- 7.2
Expand Down
42 changes: 37 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,43 @@ function _checkTypehint(callable $callback, $object)
return true;
}

$expectedException = $parameters[0];
if (\PHP_VERSION_ID < 70100 || \defined('HHVM_VERSION')) {
$expectedException = $parameters[0];

if (!$expectedException->getClass()) {
return true;
}
if (!$expectedException->getClass()) {
return true;
}

return $expectedException->getClass()->isInstance($object);
} else {
$type = $parameters[0]->getType();

return $expectedException->getClass()->isInstance($object);
if (!$type) {
return true;
}

$types = [$type];

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

$mismatched = false;

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

$expectedClass = $type->getName();

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

$mismatched = true;
}

return !$mismatched;
}
}
97 changes: 52 additions & 45 deletions tests/FunctionCheckTypehintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,65 @@ public function shouldAcceptFunctionStringCallbackWithTypehint()
/** @test */
public function shouldAcceptInvokableObjectCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint(new TestCallbackWithTypehintClass(), new \Exception()));
$this->assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint(new CallbackWithTypehintClass(), new \Exception()));
}

/** @test */
public function shouldAcceptObjectMethodCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new \Exception()));
$this->assertTrue(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new \Exception()));
}

/** @test */
public function shouldAcceptStaticClassCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint(['React\Promise\TestCallbackWithTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint(['React\Promise\TestCallbackWithTypehintClass', 'testCallbackStatic'], new \Exception()));
$this->assertTrue(_checkTypehint([new CallbackWithTypehintClass(), 'testCallbackStatic'], new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint([new CallbackWithTypehintClass(), '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(['React\Promise\CallbackWithUnionTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException()));
self::assertFalse(_checkTypehint(['React\Promise\CallbackWithUnionTypehintClass', 'testCallbackStatic'], new \Exception()));
}

/** @test */
Expand All @@ -57,19 +100,19 @@ public function shouldAcceptFunctionStringCallbackWithoutTypehint()
/** @test */
public function shouldAcceptInvokableObjectCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint(new TestCallbackWithoutTypehintClass(), new \InvalidArgumentException()));
$this->assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new \InvalidArgumentException()));
}

/** @test */
public function shouldAcceptObjectMethodCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint([new TestCallbackWithoutTypehintClass(), 'testCallback'], new \InvalidArgumentException()));
$this->assertTrue(_checkTypehint([new CallbackWithoutTypehintClass(), 'testCallback'], new \InvalidArgumentException()));
}

/** @test */
public function shouldAcceptStaticClassCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint(['React\Promise\TestCallbackWithoutTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException()));
$this->assertTrue(_checkTypehint(['React\Promise\CallbackWithoutTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException()));
}
}

Expand All @@ -80,39 +123,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()
{
}
}