Skip to content

Commit

Permalink
Adjustments for 7.1, support for union types, test suite updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cdosoftei committed Aug 13, 2020
1 parent 2ed085d commit 8a8150d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 30 deletions.
30 changes: 14 additions & 16 deletions phpunit.xml.dist
@@ -1,18 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Promise Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
<exclude>
<file>./src/functions_include.php</file>
</exclude>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src/</directory>
</include>
<exclude>
<file>./src/functions_include.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Promise Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
38 changes: 27 additions & 11 deletions src/functions.php
Expand Up @@ -342,21 +342,37 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool
return true;
}

$expectedClass = null;
if (method_exists($parameters[0], 'getType')) {
$type = $parameters[0]->getType();
$type = $parameters[0]->getType();

if (!$type) {
return true;
}

$types = [$type];

if (\method_exists($type, 'getTypes')) {
$types = $type->getTypes();
}

$mismatched = false;

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

$expectedClass = new \ReflectionClass(method_exists($type, 'getName') ? $type->getName() : (string) $type);
} else {
$expectedClass = $parameters[0]->getClass();
}
try {
$expectedClass = new \ReflectionClass($type->getName());

if (!$expectedClass) {
return true;
if ($expectedClass->isInstance($reason)) {
return true;
} else {
$mismatched = true;
}
} catch (\ReflectionException $exception) {
$mismatched = true;
}
}

return $expectedClass->isInstance($reason);
return !$mismatched;
}
4 changes: 2 additions & 2 deletions tests/ErrorCollector.php
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
47 changes: 46 additions & 1 deletion tests/FunctionCheckTypehintTest.php
Expand Up @@ -42,6 +42,33 @@ public function shouldAcceptStaticClassCallbackWithTypehint()
self::assertFalse(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception()));
}

// Requires PHP 8
/*
public function shouldAcceptClosureCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint(function (RuntimeException|InvalidArgumentException $e) {}, new InvalidArgumentException()));
self::assertFalse(_checkTypehint(function (RuntimeException|InvalidArgumentException $e) {}, new Exception()));
}
public function shouldAcceptInvokableObjectCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint(new TestCallbackWithUnionTypehintClass(), new InvalidArgumentException()));
self::assertFalse(_checkTypehint(new TestCallbackWithUnionTypehintClass(), new Exception()));
}
public function shouldAcceptObjectMethodCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint([new TestCallbackWithUnionTypehintClass(), 'testCallback'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([new TestCallbackWithUnionTypehintClass(), 'testCallback'], new Exception()));
}
public function shouldAcceptStaticClassCallbackWithUnionTypehint()
{
self::assertTrue(_checkTypehint([TestCallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException()));
self::assertFalse(_checkTypehint([TestCallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new Exception()));
}
*/

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

/** @test */
Expand Down Expand Up @@ -97,6 +124,24 @@ public static function testCallbackStatic(InvalidArgumentException $e)
}
}

// Requires PHP 8
/*
class TestCallbackWithUnionTypehintClass
{
public function __invoke(RuntimeException|InvalidArgumentException $e)
{
}
public function testCallback(RuntimeException|InvalidArgumentException $e)
{
}
public static function testCallbackStatic(RuntimeException|InvalidArgumentException $e)
{
}
}
*/

class TestCallbackWithoutTypehintClass
{
public function __invoke()
Expand Down

0 comments on commit 8a8150d

Please sign in to comment.