TagConverter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
B convert() 0 30 6
A getDescription() 0 15 4
A addTypes() 0 26 4
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Transformer\Writer\Xml;
17
18
use phpDocumentor\Descriptor\Tag\BaseTypes\TypedAbstract;
19
use phpDocumentor\Descriptor\Tag\BaseTypes\TypedVariableAbstract;
20
use phpDocumentor\Descriptor\Tag\DeprecatedDescriptor;
21
use phpDocumentor\Descriptor\Tag\LinkDescriptor;
22
use phpDocumentor\Descriptor\Tag\MethodDescriptor;
23
use phpDocumentor\Descriptor\Tag\SeeDescriptor;
24
use phpDocumentor\Descriptor\Tag\SinceDescriptor;
25
use phpDocumentor\Descriptor\Tag\UsesDescriptor;
26
use phpDocumentor\Descriptor\Tag\VersionDescriptor;
27
use phpDocumentor\Descriptor\TagDescriptor;
28
29
/**
30
 * Creates an XML Element 'tag' and appends it to the provided parent element.
31
 *
32
 * With this class we convert a TagDescriptor, or any child thereof, into an XML element that is subsequently appended
33
 * onto a provided parent element (usually an XML Element that represents a DocBlock).
34
 *
35
 * During the conversion process the generated XML Element is enriched with additional elements and attributes based on
36
 * which tags are provided (or more specifically which methods that support).
37
 */
38
class TagConverter
39
{
40
    /**
41
     * Export this tag to the given DocBlock.
42
     *
43
     * @param \DOMElement   $parent  Element to augment.
44
     * @param TagDescriptor $tag     The tag to export.
45
     *
46
     * @return \DOMElement
47
     */
48 9
    public function convert(\DOMElement $parent, TagDescriptor $tag)
49
    {
50 9
        $description = $this->getDescription($tag);
51
52 9
        $child = new \DOMElement('tag');
53 9
        $parent->appendChild($child);
54
55 9
        $child->setAttribute('name', str_replace('&', '&amp;', $tag->getName()));
56 9
        $child->setAttribute('line', $parent->getAttribute('line'));
57 9
        $child->setAttribute('description', str_replace('&', '&amp;', $description));
58 9
        $this->addTypes($tag, $child);
59
60 9
        if ($tag instanceof TypedVariableAbstract) {
61 3
            $child->setAttribute('variable', str_replace('&', '&amp;', $tag->getVariableName()));
62
        }
63
64 9
        if ($tag instanceof SeeDescriptor || $tag instanceof UsesDescriptor) {
65 1
            $child->setAttribute('link', str_replace('&', '&amp;', $tag->getReference()));
66
        }
67
68 9
        if ($tag instanceof LinkDescriptor) {
69 1
            $child->setAttribute('link', str_replace('&', '&amp;', $tag->getLink()));
70
        }
71
72 9
        if ($tag instanceof MethodDescriptor) {
73 1
            $child->setAttribute('method_name', str_replace('&', '&amp;', $tag->getMethodName()));
74
        }
75
76 9
        return $child;
77
    }
78
79
    /**
80
     * Returns the description from the Tag with the version prepended when applicable.
81
     *
82
     * @todo the version should not be prepended here but in templates; remove this.
83
     * @return string
84
     */
85 3
    protected function getDescription(TagDescriptor $tag)
86
    {
87 3
        $description = '';
88
89 3
        if ($tag instanceof VersionDescriptor ||
90 2
            $tag instanceof DeprecatedDescriptor ||
91 3
            $tag instanceof SinceDescriptor
92
        ) {
93 1
            $description .= $tag->getVersion() . ' ';
94
        }
95
96 3
        $description .= $tag->getDescription();
97
98 3
        return trim($description);
99
    }
100
101
    /**
102
     * Adds type elements and a type attribute to the tag if a method 'getTypes' is present.
103
     */
104 3
    protected function addTypes(TagDescriptor $tag, \DOMElement $child)
105
    {
106 3
        $typeString = '';
107
108 3
        if ($tag instanceof TypedAbstract) {
109 3
            $types = $tag->getType();
110
111 3
            if ($types instanceof \IteratorAggregate) {
112 1
                foreach ($types as $type) {
113 1
                    $typeString .= $type . '|';
114
115
                    /** @var \DOMElement $typeNode */
116 1
                    $typeNode = $child->appendChild(new \DOMElement('type'));
117 1
                    $typeNode->appendChild(new \DOMText((string) $type));
118
                }
119
            } else {
120 2
                $typeString .= $types . '|';
121
122
                /** @var \DOMElement $typeNode */
123 2
                $typeNode = $child->appendChild(new \DOMElement('type'));
124 2
                $typeNode->appendChild(new \DOMText((string) $types));
125
            }
126
127 3
            $child->setAttribute('type', str_replace('&', '&amp;', rtrim($typeString, '|')));
128
        }
129 3
    }
130
}
131