Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

TagConverter::addTypes()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 2
dl 0
loc 26
ccs 14
cts 14
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Plugin\Core\Transformer\Writer\Xml;
13
14
use phpDocumentor\Descriptor\Tag\MethodDescriptor;
15
use phpDocumentor\Descriptor\Tag\BaseTypes\TypedAbstract;
16
use phpDocumentor\Descriptor\Tag\BaseTypes\TypedVariableAbstract;
17
use phpDocumentor\Descriptor\Tag\DeprecatedDescriptor;
18
use phpDocumentor\Descriptor\Tag\LinkDescriptor;
19
use phpDocumentor\Descriptor\Tag\SeeDescriptor;
20
use phpDocumentor\Descriptor\Tag\SinceDescriptor;
21
use phpDocumentor\Descriptor\Tag\UsesDescriptor;
22
use phpDocumentor\Descriptor\Tag\VersionDescriptor;
23
use phpDocumentor\Descriptor\TagDescriptor;
24
25
/**
26
 * Creates an XML Element 'tag' and appends it to the provided parent element.
27
 *
28
 * With this class we convert a TagDescriptor, or any child thereof, into an XML element that is subsequently appended
29
 * onto a provided parent element (usually an XML Element that represents a DocBlock).
30
 *
31
 * During the conversion process the generated XML Element is enriched with additional elements and attributes based on
32
 * which tags are provided (or more specifically which methods that support).
33
 */
34
class TagConverter
0 ignored issues
show
Complexity introduced by
The class TagConverter has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
Loading history...
35
{
36
    /**
37
     * Export this tag to the given DocBlock.
38
     *
39
     * @param \DOMElement   $parent  Element to augment.
40
     * @param TagDescriptor $tag     The tag to export.
41
     *
42
     * @return \DOMElement
43
     */
44 9
    public function convert(\DOMElement $parent, TagDescriptor $tag)
45
    {
46 9
        $description = $this->getDescription($tag);
47
48 9
        $child = new \DOMElement('tag');
49 9
        $parent->appendChild($child);
50
51 9
        $child->setAttribute('name', str_replace('&', '&amp;', $tag->getName()));
52 9
        $child->setAttribute('line', $parent->getAttribute('line'));
53 9
        $child->setAttribute('description', str_replace('&', '&amp;', $description));
54 9
        $this->addTypes($tag, $child);
55
56
        // TODO: make the tests below configurable from the outside so that more could be added using plugins
57 9
        if ($tag instanceof TypedVariableAbstract) {
58 3
            $child->setAttribute('variable', str_replace('&', '&amp;', $tag->getVariableName()));
59
        }
60 9
        if ($tag instanceof SeeDescriptor || $tag instanceof UsesDescriptor) {
61 1
            $child->setAttribute('link', str_replace('&', '&amp;', $tag->getReference()));
62
        }
63 9
        if ($tag instanceof LinkDescriptor) {
64 1
            $child->setAttribute('link', str_replace('&', '&amp;', $tag->getLink()));
65
        }
66 9
        if ($tag instanceof MethodDescriptor) {
67 1
            $child->setAttribute('method_name', str_replace('&', '&amp;', $tag->getMethodName()));
68
        }
69
70 9
        return $child;
71
    }
72
73
    /**
74
     * Returns the description from the Tag with the version prepended when applicable.
75
     *
76
     * @param TagDescriptor $tag
77
     *
78
     * @todo the version should not be prepended here but in templates; remove this.
79
     *
80
     * @return string
81
     */
82 3
    protected function getDescription(TagDescriptor $tag)
83
    {
84 3
        $description = '';
85
86
        if (
87 3
            $tag instanceof VersionDescriptor ||
88 2
            $tag instanceof DeprecatedDescriptor ||
89 3
            $tag instanceof SinceDescriptor
90
        ) {
91 1
            $description .= $tag->getVersion() . ' ';
92
        }
93
94 3
        $description .= $tag->getDescription();
95
96 3
        return trim($description);
97
    }
98
99
    /**
100
     * Adds type elements and a type attribute to the tag if a method 'getTypes' is present.
101
     *
102
     * @param TagDescriptor $tag
103
     * @param \DOMElement   $child
104
     *
105
     * @return void
106
     */
107 3
    protected function addTypes(TagDescriptor $tag, \DOMElement $child)
108
    {
109 3
        $typeString = '';
110
111 3
        if ($tag instanceof TypedAbstract) {
112 3
            $types = $tag->getTypes();
113
114 3
            if ($types instanceof \IteratorAggregate) {
115 1
                foreach ($types as $type) {
116 1
                    $typeString .= $type . '|';
117
118
                    /** @var \DOMElement $typeNode */
119 1
                    $typeNode = $child->appendChild(new \DOMElement('type'));
120 1
                    $typeNode->appendChild(new \DOMText($type));
121
                }
122
            } else {
123 2
                $typeString .= $types . '|';
124
125
                /** @var \DOMElement $typeNode */
126 2
                $typeNode = $child->appendChild(new \DOMElement('type'));
127 2
                $typeNode->appendChild(new \DOMText($types));
128
            }
129
130 3
            $child->setAttribute('type', str_replace('&', '&amp;', rtrim($typeString, '|')));
131
        }
132 3
    }
133
}
134