PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.6.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.6.1
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 4.6.1, at includes/Dependencies/Invoker/Reflection/CallableReflection.php

58 lines 1.7 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 // phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards.
3
4 namespace WPDeveloper\BetterDocs\Dependencies\Invoker\Reflection;
5
6 use Closure;
7 use WPDeveloper\BetterDocs\Dependencies\Invoker\Exception\NotCallableException;
8 use ReflectionException;
9 use ReflectionFunction;
10 use ReflectionFunctionAbstract;
11 use ReflectionMethod;
12
13 /**
14 * Create a reflection object from a callable or a callable-like.
15 *
16 * @internal
17 */
18 class CallableReflection
19 {
20 /**
21 * @param callable|array|string $callable Can be a callable or a callable-like.
22 * @throws NotCallableException|ReflectionException
23 */
24 public static function create($callable): ReflectionFunctionAbstract
25 {
26 // Closure
27 if ($callable instanceof Closure) {
28 return new ReflectionFunction($callable);
29 }
30
31 // Array callable
32 if (is_array($callable)) {
33 [$class, $method] = $callable;
34
35 if (! method_exists($class, $method)) {
36 throw NotCallableException::fromInvalidCallable($callable);
37 }
38
39 return new ReflectionMethod($class, $method);
40 }
41
42 // Callable object (i.e. implementing __invoke())
43 if (is_object($callable) && method_exists($callable, '__invoke')) {
44 return new ReflectionMethod($callable, '__invoke');
45 }
46
47 // Standard function
48 if (is_string($callable) && function_exists($callable)) {
49 return new ReflectionFunction($callable);
50 }
51
52 throw new NotCallableException(sprintf(
53 '%s is not a callable',
54 is_string($callable) ? $callable : 'Instance of ' . get_class($callable)
55 ));
56 }
57 }
58