Skip to content

Commit

Permalink
simplify for PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
mindplay-dk committed Apr 6, 2024
1 parent 048c725 commit d0e235c
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Closure;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use TypeError;

/**
* Pseudo-namespace for some common reflection helper-functions.
Expand All @@ -25,25 +25,11 @@ abstract class Reflection
*/
public static function createFromCallable($callback): ReflectionFunctionAbstract
{
if (is_object($callback)) {
if ($callback instanceof Closure) {
return new ReflectionFunction($callback);
} elseif (method_exists($callback, '__invoke')) {
return new ReflectionMethod($callback, '__invoke');
}

throw new InvalidArgumentException("class " . get_class($callback) . " does not implement __invoke()");
} elseif (is_array($callback)) {
if (is_callable($callback)) {
return new ReflectionMethod($callback[0], $callback[1]);
}

throw new InvalidArgumentException("expected callable");
} elseif (is_callable($callback)) {
return new ReflectionFunction($callback);
try {
return new ReflectionFunction(Closure::fromCallable($callback));
} catch (TypeError $error) {
throw new InvalidArgumentException("unexpected value: " . var_export($callback, true) . " - expected callable");
}

throw new InvalidArgumentException("unexpected value: " . var_export($callback, true) . " - expected callable");
}

/**
Expand Down

0 comments on commit d0e235c

Please sign in to comment.