Skip to content

Commit a21c54e

Browse files
committed
Don't namespaces primitive arrays
- Handle correctly array of type - Add test case (tests/data/issue234) - Better handling of multiple type (piped '|') - Update html rendering to display array in members definition Fix theseer#234
1 parent 4be0910 commit a21c54e

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

src/docblock/elements/VarElement.php

+36
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) {
1616
$node = parent::asDom($ctx);
1717
$type = $node->getAttribute('type');
1818

19+
$types = explode('|', $type);
20+
21+
if (count($types) > 1) {
22+
$node->setAttribute('type', 'mixed');
23+
24+
foreach ($types as $oneType) {
25+
$this->typeResolver($node, $oneType);
26+
}
27+
28+
return $node;
29+
}
30+
1931
if (\strpos($type, '[]')) {
2032
$type = \mb_substr($type, 0, -2);
2133
$node->setAttribute('type', 'array');
@@ -40,4 +52,28 @@ public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) {
4052

4153
return $node;
4254
}
55+
56+
protected function typeResolver(\TheSeer\fDOM\fDOMElement $node, string $type) {
57+
$isArray = false;
58+
if (substr($type, -2, 2) === '[]') {
59+
$isArray = true;
60+
$type = substr($type, 0, -2);
61+
}
62+
$nodeType = $node->appendElementNS(self::XMLNS, 'type');
63+
$nodeType->setAttribute('full', $type);
64+
$nodeType->setAttribute('array', $isArray?'true':'false');
65+
66+
if (\in_array($type, $this->types, true)) {
67+
$nodeType->setAttribute('name', $type);
68+
$nodeType->setAttribute('namespace', '');
69+
return;
70+
}
71+
72+
$parts = \explode('\\', $type);
73+
$local = \array_pop($parts);
74+
$namespace = \implode('\\', $parts);
75+
76+
$nodeType->setAttribute('namespace', $namespace);
77+
$nodeType->setAttribute('name', $local);
78+
}
4379
}

src/docblock/parser/GenericParser.php

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ protected function buildObject($classname, array $buffer) {
4747
}
4848

4949
protected function lookupType($type) {
50+
$types = explode('|', $type);
51+
52+
foreach ($types as &$oneType) {
53+
$isArray = false;
54+
if (substr($oneType, -2, 2) === '[]') {
55+
$isArray = true;
56+
$oneType = substr($oneType, 0, -2);
57+
}
58+
59+
$oneType = $this->lookupOneType($oneType);
60+
61+
if ($isArray) {
62+
$oneType .= '[]';
63+
}
64+
}
65+
66+
return implode('|', $types);
67+
}
68+
69+
protected function lookupOneType($type) {
5070
if ($type === 'self' || $type === 'static') {
5171
return $this->aliasMap['::unit'];
5272
}

templates/html/components.xsl

+26
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@
302302
<xsl:variable name="ctx" select="pdx:docblock/pdx:var/pdx:type" />
303303
<xsl:copy-of select="pdxf:link($ctx, '', $ctx/@full)" />
304304
</xsl:when>
305+
<xsl:when test="pdx:docblock/pdx:var/@type = 'array'">
306+
<xsl:variable name="ctx" select="pdx:docblock/pdx:var/pdx:type" />
307+
<xsl:choose>
308+
<xsl:when test="pdx:docblock/pdx:var/@of = 'object'">
309+
<xsl:copy-of select="pdxf:link($ctx, '', $ctx/@full)" />
310+
</xsl:when>
311+
<xsl:otherwise><xsl:value-of select="pdx:docblock/pdx:var/@of" /></xsl:otherwise>
312+
</xsl:choose>[]
313+
</xsl:when>
314+
<xsl:when test="pdx:docblock/pdx:var/@type = 'mixed' and count(pdx:docblock/pdx:var/pdx:type) > 0">
315+
<xsl:for-each select="pdx:docblock/pdx:var/pdx:type">
316+
<xsl:call-template name="mixedvartype" />
317+
</xsl:for-each>
318+
</xsl:when>
305319
<xsl:otherwise><xsl:value-of select="pdx:docblock/pdx:var/@type" /></xsl:otherwise>
306320
</xsl:choose>
307321
</xsl:if>
@@ -316,6 +330,18 @@
316330

317331
<!-- ######################################################################################################### -->
318332

333+
<xsl:template name="mixedvartype">
334+
<span class="multiple">
335+
<xsl:choose>
336+
<xsl:when test="@name = @full"><xsl:value-of select="@name" /></xsl:when>
337+
<xsl:otherwise><xsl:copy-of select="pdxf:link(., '', @full)" /></xsl:otherwise>
338+
</xsl:choose>
339+
<xsl:if test="@array = 'true'">[]</xsl:if>
340+
</span>
341+
</xsl:template>
342+
343+
<!-- ######################################################################################################### -->
344+
319345
<xsl:template name="methods">
320346
<div class="styled">
321347
<xsl:if test="$unit/pdx:method[@visibility='private']">

templates/html/static/css/style.css

+6
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ ul.styled {
252252
ul.members li {
253253
margin-bottom: 0.5em;
254254
}
255+
ul.members li .multiple:after {
256+
content: "|";
257+
}
258+
ul.members li .multiple:last-child:after {
259+
content: '';
260+
}
255261

256262
.styled h4 {
257263
padding:0;
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace MyNamespace;
4+
5+
class ArrayCases
6+
{
7+
/**
8+
* @var int[]
9+
*/
10+
public $arrayOfInt;
11+
12+
/**
13+
* @var boolean[]
14+
*/
15+
public $arrayOfBoolean;
16+
17+
/**
18+
* @var int
19+
*/
20+
public $int;
21+
22+
/**
23+
* @var boolean
24+
*/
25+
public $boolean;
26+
27+
/**
28+
* @var int[]|string[]|SomeClass[]
29+
*/
30+
public $multipleArray;
31+
32+
/**
33+
* @var int|string|SomeClass
34+
*/
35+
public $multiple;
36+
37+
/**
38+
* @var SomeClass[]
39+
*/
40+
public $arrayOfObject;
41+
42+
/**
43+
* @var SomeClass
44+
*/
45+
public $object;
46+
}

tests/data/issue234/test.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
<project name="Example" source="${basedir}/src" workdir="${basedir}/xml">
4+
<collector backend="parser" />
5+
6+
<generator output="${basedir}/xml">
7+
8+
<build engine="html" enabled="true" output="html">
9+
<template dir="${phpDox.home}/templates/html" />
10+
<file extension="html" />
11+
</build>
12+
13+
</generator>
14+
</project>
15+
</phpdox>

0 commit comments

Comments
 (0)