PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.3
4.9.3 4.9.2 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 All 201 releases
betterdocs / includes / Dependencies / DI / Definition / Dumper / ObjectDefinitionDumper.php

ObjectDefinitionDumper.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.3, at includes/Dependencies/DI/Definition/Dumper/ObjectDefinitionDumper.php

145 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards.
3
4 declare(strict_types=1);
5
6 namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Dumper;
7
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
9 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition;
10 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition\MethodInjection;
11 use ReflectionException;
12
13 /**
14 * Dumps object definitions to string for debugging purposes.
15 *
16 * @since 4.1
17 * @author Matthieu Napoli <[email protected]>
18 */
19 class ObjectDefinitionDumper
20 {
21 /**
22 * Returns the definition as string representation.
23 */
24 public function dump(ObjectDefinition $definition) : string
25 {
26 $className = $definition->getClassName();
27 $classExist = class_exists($className) || interface_exists($className);
28
29 // Class
30 if (! $classExist) {
31 $warning = '#UNKNOWN# ';
32 } else {
33 $class = new \ReflectionClass($className);
34 $warning = $class->isInstantiable() ? '' : '#NOT INSTANTIABLE# ';
35 }
36 $str = sprintf(' class = %s%s', $warning, $className);
37
38 // Lazy
39 $str .= PHP_EOL . ' lazy = ' . var_export($definition->isLazy(), true);
40
41 if ($classExist) {
42 // Constructor
43 $str .= $this->dumpConstructor($className, $definition);
44
45 // Properties
46 $str .= $this->dumpProperties($definition);
47
48 // Methods
49 $str .= $this->dumpMethods($className, $definition);
50 }
51
52 return sprintf('Object (' . PHP_EOL . '%s' . PHP_EOL . ')', $str);
53 }
54
55 private function dumpConstructor(string $className, ObjectDefinition $definition) : string
56 {
57 $str = '';
58
59 $constructorInjection = $definition->getConstructorInjection();
60
61 if ($constructorInjection !== null) {
62 $parameters = $this->dumpMethodParameters($className, $constructorInjection);
63
64 $str .= sprintf(PHP_EOL . ' __construct(' . PHP_EOL . ' %s' . PHP_EOL . ' )', $parameters);
65 }
66
67 return $str;
68 }
69
70 private function dumpProperties(ObjectDefinition $definition) : string
71 {
72 $str = '';
73
74 foreach ($definition->getPropertyInjections() as $propertyInjection) {
75 $value = $propertyInjection->getValue();
76 if ($value instanceof Definition) {
77 $valueStr = (string) $value;
78 } else {
79 $valueStr = var_export($value, true);
80 }
81
82 $str .= sprintf(PHP_EOL . ' $%s = %s', $propertyInjection->getPropertyName(), $valueStr);
83 }
84
85 return $str;
86 }
87
88 private function dumpMethods(string $className, ObjectDefinition $definition) : string
89 {
90 $str = '';
91
92 foreach ($definition->getMethodInjections() as $methodInjection) {
93 $parameters = $this->dumpMethodParameters($className, $methodInjection);
94
95 $str .= sprintf(PHP_EOL . ' %s(' . PHP_EOL . ' %s' . PHP_EOL . ' )', $methodInjection->getMethodName(), $parameters);
96 }
97
98 return $str;
99 }
100
101 private function dumpMethodParameters(string $className, MethodInjection $methodInjection) : string
102 {
103 $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName());
104
105 $args = [];
106
107 $definitionParameters = $methodInjection->getParameters();
108
109 foreach ($methodReflection->getParameters() as $index => $parameter) {
110 if (array_key_exists($index, $definitionParameters)) {
111 $value = $definitionParameters[$index];
112
113 if ($value instanceof Definition) {
114 $valueStr = (string) $value;
115 } else {
116 $valueStr = var_export($value, true);
117 }
118 $args[] = sprintf('$%s = %s', $parameter->getName(), $valueStr);
119
120 continue;
121 }
122
123 // If the parameter is optional and wasn't specified, we take its default value
124 if ($parameter->isOptional()) {
125 try {
126 $value = $parameter->getDefaultValue();
127
128 $args[] = sprintf(
129 '$%s = (default value) %s',
130 $parameter->getName(),
131 var_export($value, true)
132 );
133 continue;
134 } catch (ReflectionException $e) {
135 // The default value can't be read through Reflection because it is a PHP internal class
136 }
137 }
138
139 $args[] = sprintf('$%s = #UNDEFINED#', $parameter->getName());
140 }
141
142 return implode(PHP_EOL . ' ', $args);
143 }
144 }
145