Skip to content

Commit 72fa19c

Browse files
committed
docgen: Render proper union types
So far there is support for rendering union types separated with pipe symbols; however, the PHP documentation uses proper DocBook 5.2 union types, so we adapt. Closes GH-10.
1 parent 9e604fe commit 72fa19c

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

scripts/docgen/docgen.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,25 @@ function get_type_by_string($str) { /* {{{ */
203203
/* }}} */
204204

205205
/** @return string|null */
206-
function get_type_as_string(ReflectionType $type = null) { /* {{{ */
206+
function get_type_as_xml_string(ReflectionType $type = null) { /* {{{ */
207207
if ($type instanceof ReflectionNamedType) {
208-
$ret = $type->getName();
208+
$ret = "<type>{$type->getName()}</type>";
209209
if ($type->allowsNull()) {
210-
$ret .= '|null';
210+
$ret = "<type class=\"union\">{$ret}<type>null</type></type>";
211211
}
212212
return $ret;
213213
}
214214
if ($type instanceof ReflectionUnionType) {
215-
$types = array_map(function($type) {return $type->getName();}, $type->getTypes());
216-
return implode('|', $types);
215+
$types = array_map(function($type) {return "<type>{$type->getName()}</type>";}, $type->getTypes());
216+
$ret = implode('', $types);
217+
if (count($types) > 1) {
218+
$ret = "<type class=\"union\">{$ret}</type>";
219+
}
220+
return $ret;
217221
}
218222
if ($type instanceof ReflectionType) {
219-
return (string) $type;
223+
$ret = (string) $type;
224+
return "<type>{$ret}</type>";
220225
}
221226
return null;
222227
}
@@ -243,16 +248,16 @@ function create_markup_to_params(array $params, $ident) { /* {{{ */
243248
$markup = "";
244249
foreach ($params as $param) {
245250
/* Parameter type */
246-
$type = get_type_as_string($param->getType());
251+
$type = get_type_as_xml_string($param->getType());
247252
if ($type === null) {
248-
$type = 'mixed';
253+
$type = '<type>mixed</type>';
249254
if (!$param->getName()) {
250255
add_warning(sprintf("Parameter name not found, param%d used", $count));
251256
}
252257
add_warning(sprintf("Type hint for parameter `%s' not found, 'mixed' used", ($param->getName() ? $param->getName() : $count)));
253258
}
254259

255-
$markup .= sprintf("%s<methodparam%s><type>%s</type><parameter%s>%s</parameter></methodparam>". PHP_EOL,
260+
$markup .= sprintf("%s<methodparam%s>%s<parameter%s>%s</parameter></methodparam>". PHP_EOL,
256261
($markup ? str_repeat(' ', $ident) : ''),
257262
($param->isOptional() ? ' choice="opt"' : ''),
258263
$type,
@@ -333,12 +338,12 @@ function gen_function_markup(ReflectionFunction $function, $content) { /* {{{ */
333338
$content = preg_replace('/\{FUNCTION_NAME\}/', $function->getName(), $content);
334339

335340
/* {RETURN_TYPE} */
336-
$type = get_type_as_string($function->getReturnType());
341+
$type = get_type_as_xml_string($function->getReturnType());
337342
if ($type === null) {
338-
$type = 'mixed';
343+
$type = '<type>mixed</type>';
339344
add_warning(sprintf("Return type hint for function `%s' not found, 'mixed' used", $function->getName()));
340345
}
341-
$content = preg_replace('/\{RETURN_TYPE\}/', "<type>$type</type>", $content, 1);
346+
$content = preg_replace('/\{RETURN_TYPE\}/', $type, $content, 1);
342347

343348
/* {FUNCTION_PARAMETERS}, {PARAMETERS_DESCRIPTION} */
344349
$content = create_markup_to_parameter_section($function, $content);
@@ -368,12 +373,12 @@ function gen_method_markup(ReflectionMethod $method, $content) { /* {{{ */
368373

369374
/* {RETURN_TYPE} */
370375
if (!$method->isConstructor()) {
371-
$type = get_type_as_string($method->getReturnType());
376+
$type = get_type_as_xml_string($method->getReturnType());
372377
if ($type === null) {
373-
$type = 'mixed';
378+
$type = '<type>mixed</type>';
374379
add_warning(sprintf("Return type hint for method `%s' not found, 'mixed' used", $method->getName()));
375380
}
376-
$content = preg_replace('/\{RETURN_TYPE\}/', "<type>$type</type>", $content, 1);
381+
$content = preg_replace('/\{RETURN_TYPE\}/', $type, $content, 1);
377382
} else {
378383
$content = preg_replace('/\{RETURN_TYPE\}/', '', $content, 1);
379384
}

0 commit comments

Comments
 (0)