TraitConverter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A convert() 0 24 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\TraitDescriptor;
19
20
/**
21
 * Converter used to create an XML Element representing the Trait and its Methods, Properties and DocBlock.
22
 *
23
 * In order to convert the DocBlock to its XML representation this class requires the respective converter.
24
 */
25
class TraitConverter
26
{
27
    /** @var DocBlockConverter object used to convert DocBlocks into their XML counterpart */
28
    protected $docBlockConverter;
29
30
    /** @var MethodConverter object used to convert methods into their XML counterpart */
31
    protected $methodConverter;
32
33
    /** @var PropertyConverter object used to convert properties into their XML counterpart */
34
    protected $propertyConverter;
35
36
    /**
37
     * Initializes this converter with the DocBlock converter.
38
     */
39 1
    public function __construct(
40
        DocBlockConverter $docBlockConverter,
41
        MethodConverter $methodConverter,
42
        PropertyConverter $propertyConverter
43
    ) {
44 1
        $this->docBlockConverter = $docBlockConverter;
45 1
        $this->methodConverter = $methodConverter;
46 1
        $this->propertyConverter = $propertyConverter;
47 1
    }
48
49
    /**
50
     * Export the given reflected Trait definition to the provided parent element.
51
     *
52
     * This method creates a new child element on the given parent XML element
53
     * and takes the properties of the Reflection argument and sets the
54
     * elements and attributes on the child.
55
     *
56
     * If a child DOMElement is provided then the properties and attributes are
57
     * set on this but the child element is not appended onto the parent. This
58
     * is the responsibility of the invoker. Essentially this means that the
59
     * $parent argument is ignored in this case.
60
     *
61
     * @param \DOMElement        $parent Element to augment.
62
     * @param TraitDescriptor $trait Element to export.
63
     *
64
     * @return \DOMElement
65
     */
66 1
    public function convert(\DOMElement $parent, TraitDescriptor $trait)
67
    {
68 1
        $child = new \DOMElement('trait');
69 1
        $parent->appendChild($child);
70
71 1
        $namespace = (string) $trait->getNamespace()->getFullyQualifiedStructuralElementName();
72 1
        $child->setAttribute('namespace', ltrim($namespace, '\\'));
73 1
        $child->setAttribute('line', (string) $trait->getLine());
74
75 1
        $child->appendChild(new \DOMElement('name', $trait->getName()));
76 1
        $child->appendChild(new \DOMElement('full_name', (string) $trait->getFullyQualifiedStructuralElementName()));
77
78 1
        $this->docBlockConverter->convert($child, $trait);
79
80 1
        foreach ($trait->getProperties() as $property) {
81 1
            $this->propertyConverter->convert($child, $property);
82
        }
83
84 1
        foreach ($trait->getMethods() as $method) {
85 1
            $this->methodConverter->convert($child, $method);
86
        }
87
88 1
        return $child;
89
    }
90
}
91