PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.3.4
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.3.4
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / Invoker / Reflection / CallableReflection.php

CallableReflection.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.3.4, at includes/Dependencies/Invoker/Reflection/CallableReflection.php

57 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace WPDeveloper\BetterDocs\Dependencies\Invoker\Reflection;
4
5 use Closure;
6 use WPDeveloper\BetterDocs\Dependencies\Invoker\Exception\NotCallableException;
7 use ReflectionException;
8 use ReflectionFunction;
9 use ReflectionFunctionAbstract;
10 use ReflectionMethod;
11
12 /**
13 * Create a reflection object from a callable or a callable-like.
14 *
15 * @internal
16 */
17 class CallableReflection
18 {
19 /**
20 * @param callable|array|string $callable Can be a callable or a callable-like.
21 * @throws NotCallableException|ReflectionException
22 */
23 public static function create($callable): ReflectionFunctionAbstract
24 {
25 // Closure
26 if ($callable instanceof Closure) {
27 return new ReflectionFunction($callable);
28 }
29
30 // Array callable
31 if (is_array($callable)) {
32 [$class, $method] = $callable;
33
34 if (! method_exists($class, $method)) {
35 throw NotCallableException::fromInvalidCallable($callable);
36 }
37
38 return new ReflectionMethod($class, $method);
39 }
40
41 // Callable object (i.e. implementing __invoke())
42 if (is_object($callable) && method_exists($callable, '__invoke')) {
43 return new ReflectionMethod($callable, '__invoke');
44 }
45
46 // Standard function
47 if (is_string($callable) && function_exists($callable)) {
48 return new ReflectionFunction($callable);
49 }
50
51 throw new NotCallableException(sprintf(
52 '%s is not a callable',
53 is_string($callable) ? $callable : 'Instance of ' . get_class($callable)
54 ));
55 }
56 }
57