DocBlockConverter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 114
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A convert() 0 16 2
A addSummary() 0 5 1
A addDescription() 0 5 1
A addTags() 0 12 4
A addInheritedFromTag() 0 17 3
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\DescriptorAbstract;
19
use phpDocumentor\Transformer\Router\RouterAbstract;
20
21
/**
22
 * Converter used to create an XML Element representing a DocBlock and its tags.
23
 *
24
 * In order to convert the tags to their XML representation this class requires the respective converter.
25
 */
26
class DocBlockConverter
27
{
28
    /** @var TagConverter Converter used to generate XML elements from TagDescriptors */
29
    protected $tagConverter;
30
31
    /** @var RouterAbstract */
32
    private $router;
33
34
    /**
35
     * Stores the converter for tags on this converter.
36
     */
37 1
    public function __construct(TagConverter $tagConverter, RouterAbstract $router)
38
    {
39 1
        $this->tagConverter = $tagConverter;
40 1
        $this->router = $router;
41 1
    }
42
43
    /**
44
     * Exports the given reflection object to the parent XML element.
45
     *
46
     * This method creates a new child element on the given parent XML element
47
     * and takes the properties of the Reflection argument and sets the
48
     * elements and attributes on the child.
49
     *
50
     * If a child DOMElement is provided then the properties and attributes are
51
     * set on this but the child element is not appended onto the parent. This
52
     * is the responsibility of the invoker. Essentially this means that the
53
     * $parent argument is ignored in this case.
54
     *
55
     * @param \DOMElement        $parent  The parent element to augment.
56
     * @param DescriptorAbstract $element The data source.
57
     *
58
     * @return \DOMElement
59
     */
60 4
    public function convert(\DOMElement $parent, DescriptorAbstract $element)
61
    {
62 4
        $child = new \DOMElement('docblock');
63 4
        $parent->appendChild($child);
64
65 4
        $child->setAttribute('line', (string) $element->getLine());
66 4
        $package = str_replace('&', '&amp;', ltrim((string)$element->getPackage(), '\\'));
67 4
        $parent->setAttribute('package', $package ?: 'global');
68
69 4
        $this->addSummary($child, $element);
70 4
        $this->addDescription($child, $element);
71 4
        $this->addTags($child, $element);
72 4
        $this->addInheritedFromTag($child, $element);
73
74 4
        return $child;
75
    }
76
77
    /**
78
     * Adds the short description of $docblock to the given node as description
79
     * field.
80
     */
81 1
    protected function addSummary(\DOMElement $node, DescriptorAbstract $element)
82
    {
83 1
        $node->appendChild(new \DOMElement('description'))
84 1
            ->appendChild(new \DOMText($element->getSummary()));
85 1
    }
86
87
    /**
88
     * Adds the DocBlock's long description to the $child element,
89
     */
90 1
    protected function addDescription(\DOMElement $node, DescriptorAbstract $element)
91
    {
92 1
        $node->appendChild(new \DOMElement('long-description'))
93 1
            ->appendChild(new \DOMText((string) $element->getDescription()));
94 1
    }
95
96
    /**
97
     * Adds each tag to the XML Node representing the DocBlock.
98
     *
99
     * The Descriptor contains an array of tag groups (that are tags grouped by their name), which in itself contains
100
     * an array of the individual tags.
101
     *
102
     * @param DescriptorAbstract $descriptor
103
     */
104 2
    protected function addTags(\DOMElement $docBlock, $descriptor)
105
    {
106 2
        foreach ($descriptor->getTags() as $tagGroup) {
107 2
            if (! $tagGroup) {
108 1
                continue;
109
            }
110
111 1
            foreach ($tagGroup as $tag) {
112 1
                $this->tagConverter->convert($docBlock, $tag);
113
            }
114
        }
115 2
    }
116
117
    /**
118
     * Adds the 'inherited_from' tag when a Descriptor inherits from another Descriptor.
119
     *
120
     * @param DescriptorAbstract $descriptor
121
     */
122 2
    protected function addInheritedFromTag(\DOMElement $docBlock, $descriptor)
123
    {
124 2
        $parentElement = $descriptor->getInheritedElement();
125 2
        if (! $parentElement instanceof $descriptor) {
126 1
            return;
127
        }
128
129 1
        $child = new \DOMElement('tag');
130 1
        $docBlock->appendChild($child);
131
132 1
        $rule = $this->router->match($parentElement);
133
134 1
        $child->setAttribute('name', 'inherited_from');
135 1
        $child->setAttribute('description', (string) $parentElement->getFullyQualifiedStructuralElementName());
136 1
        $child->setAttribute('refers', (string) $parentElement->getFullyQualifiedStructuralElementName());
137 1
        $child->setAttribute('link', $rule ? $rule->generate($parentElement) : '');
138 1
    }
139
}
140