/
PhpDocGenerator.php
269 lines (247 loc) · 7.12 KB
/
PhpDocGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace Concrete\Core\Support\Symbol;
use ReflectionClass;
/**
* Helper class to generate PHPDoc.
*/
class PhpDocGenerator
{
/**
* Are we generating PHPDoc to be placed inside a namespace?
*
* @var bool
*/
protected $insideNamespace = false;
/**
* Indentation.
*
* @var string
*/
protected $indentation = '';
/**
* Insert the variable definitions in the same PHPDoc block?
*
* @var string
*/
protected $singleDocBlock = false;
/**
* Are we generating PHPDoc to be placed inside a namespace?
*
* @return bool
*/
public function isInsideNamespace()
{
return $this->insideNamespace;
}
/**
* Are we generating PHPDoc to be placed inside a namespace?
*
* @param bool $value
*
* @return $this
*/
public function setIsInsideNamespace($value)
{
$this->insideNamespace = (bool) $value;
return $this;
}
/**
* Get the indentation.
*
* @return string
*/
public function getIndentation()
{
return $this->indentation;
}
/**
* Set the indentation.
*
* @param string $value
*
* @return $this
*/
public function setIndentation($value)
{
$this->indentation = (string) $value;
return $this;
}
/**
* Insert the variable definitions in the same PHPDoc block?
*
* @return bool
*/
public function isSingleDocBlock()
{
return $this->singleDocBlock;
}
/**
* Insert the variable definitions in the same PHPDoc block?
*
* @param bool $value
*
* @return $this
*/
public function setIsSingleDocBlock($value)
{
$this->singleDocBlock = (bool) $value;
return $this;
}
/**
* Generate the PHPDoc to describe a variable.
*
* @param string $name The variable name
* @param mixed $value The variable value
*
* @return string
*/
public function describeVar($name, $value)
{
return $this->indentation . '/** ' . $this->describeVarUncommented($name, $value) . " */\n";
}
/**
* Generate the PHPDoc to describe a list of variables.
*
* @param array $vars
* @param bool $sortByName
*
* @return string
*/
public function describeVars(array $vars, $sortByName = true)
{
$count = count($vars);
if ($count === 0) {
return '';
}
if ($sortByName) {
ksort($vars, SORT_NATURAL);
}
if ($count === 1 || $this->isSingleDocBlock() === false) {
$result = '';
foreach ($vars as $name => $value) {
$result .= $this->describeVar($name, $value);
}
return $result;
}
$result = $this->indentation . "/**\n";
foreach ($vars as $name => $value) {
$result .= $this->indentation . ' * ' . $this->describeVarUncommented($name, $value) . "\n";
}
return $result . $this->indentation . " */\n";
}
/**
* Generate the PHPDoc content to describe a variable (without opening/closing comments).
*
* @param string $name The variable name
* @param mixed $value The variable value
*
* @return string
*/
protected function describeVarUncommented($name, $value)
{
return '@var ' . $this->getVarType($value) . ' ' . ($name[0] === '$' ? '' : '$') . $name;
}
/**
* Get the PHPDoc type name of a variable.
*
* @param mixed $var
* @param int $arrayLevel
*
* @return string
*/
protected function getVarType($var, $arrayLevel = 0)
{
$phpType = gettype($var);
switch ($phpType) {
case 'boolean':
$result = 'bool';
break;
case 'integer':
$result = 'int';
break;
case 'double':
$result = 'float';
break;
case 'string':
$result = 'string';
break;
case 'array':
if ($arrayLevel > 1) {
$result = 'array';
} else {
$result = null;
$first = true;
foreach ($var as $item) {
$itemType = $this->getVarType($item, $arrayLevel + 1);
if ($first) {
$result = $itemType;
$commonObjectDescriptors = $this->getObjectDescriptors($item);
$first = false;
} else {
if ($result !== $itemType) {
$result = null;
if (empty($commonObjectDescriptors)) {
break;
}
if (!empty($commonObjectDescriptors)) {
$commonObjectDescriptors = array_intersect($commonObjectDescriptors, $this->getObjectDescriptors($item));
}
}
}
}
if ($result !== null) {
$result .= '[]';
} elseif (!empty($commonObjectDescriptors)) {
$result = array_shift($commonObjectDescriptors) . '[]';
} else {
$result = 'array';
}
}
break;
case 'object':
$result = get_class($var);
if ($result === false) {
$result = 'mixed';
} else {
if ($this->insideNamespace) {
$result = '\\' . $result;
}
}
break;
case 'resource':
$result = 'resource';
break;
case 'NULL':
$result = 'null';
break;
case 'unknown type':
default:
$result = 'mixed';
break;
}
return $result;
}
/**
* Get all the names representing an object instance (class name, parent class names, interface names).
*
* @param mixed $var
*
* @return array
*/
protected function getObjectDescriptors($var)
{
$result = [];
$className = is_object($var) ? get_class($var) : false;
if ($className !== false) {
$result[] = ($this->insideNamespace ? '\\' : '') . $className;
$class = new ReflectionClass($className);
for ($childClass = $class->getParentClass(); $childClass; $childClass = $childClass->getParentClass()) {
$result[] = ($this->insideNamespace ? '\\' : '') . $childClass->getName();
}
foreach ($class->getInterfaceNames() as $interfaceName) {
$result[] = ($this->insideNamespace ? '\\' : '') . $interfaceName;
}
}
return $result;
}
}