Completed
Push — master ( aa6cd0...7372cb )
by Randy
02:05
created

Assembler::visitXmlNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator\Dom;
4
5
use Dgame\Soap\Attribute\Attribute;
6
use Dgame\Soap\Attribute\XmlAttribute;
7
use Dgame\Soap\Element;
8
use Dgame\Soap\Hydrator\VisitorInterface;
9
use Dgame\Soap\XmlElement;
10
use Dgame\Soap\XmlNode;
11
use DOMDocument;
12
use DOMElement;
13
use DOMNode;
14
15
/**
16
 * Class Assembler
17
 * @package Dgame\Soap\Hydrator\Dom
18
 */
19
final class Assembler implements VisitorInterface
20
{
21
    /**
22
     * @var DOMDocument
23
     */
24
    private $document;
25
    /**
26
     * @var DOMElement
27
     */
28
    private $node;
29
30
    /**
31
     * Assembler constructor.
32
     *
33
     * @param DOMNode $node
34
     */
35
    public function __construct(DOMNode $node)
36
    {
37
        $this->node     = $node;
0 ignored issues
show
Documentation Bug introduced by
$node is of type object<DOMNode>, but the property $node was declared to be of type object<DOMElement>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
38
        $this->document = $node->ownerDocument ?? $this->node;
0 ignored issues
show
Documentation Bug introduced by
It seems like $node->ownerDocument ?? $this->node can also be of type object<DOMNode>. However, the property $document is declared as type object<DOMDocument>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
    }
40
41
    /**
42
     * @return DOMDocument
43
     */
44
    public function getDocument(): DOMDocument
45
    {
46
        return $this->document;
47
    }
48
49
    /**
50
     * @param Element $element
51
     *
52
     * @return DOMNode
53
     */
54
    private function assemble(Element $element): DOMNode
55
    {
56
        $node = $this->document->createElement($element->getName(), $element->hasValue() ? $element->getValue() : null);
57
        $this->node->appendChild($node);
58
        $this->node = $node;
59
60
        foreach ($element->getAttributes() as $attribute) {
61
            $attribute->accept($this);
62
        }
63
64
        return $node;
65
    }
66
67
    /**
68
     * @param Element $element
69
     */
70
    public function visitElement(Element $element)
71
    {
72
        $this->assemble($element);
73
    }
74
75
    /**
76
     * @param XmlElement $element
77
     */
78
    public function visitXmlElement(XmlElement $element)
79
    {
80
        $node = $this->assemble($element);
81
        if ($element->hasPrefix()) {
82
            $node->prefix = $element->getPrefix();
83
        }
84
    }
85
86
    /**
87
     * @param XmlNode $node
88
     */
89
    public function visitXmlNode(XmlNode $node)
90
    {
91
        $element = $this->assemble($node);
92
        if ($node->hasPrefix()) {
93
            $element->prefix = $node->getPrefix();
94
        }
95
96
        foreach ($node->getChildren() as $child) {
97
            $assembler = new self($this->node);
98
            $child->accept($assembler);
99
        }
100
    }
101
102
    /**
103
     * @param Attribute $attribute
104
     */
105
    public function visitAttribute(Attribute $attribute)
106
    {
107
        $this->node->setAttribute($attribute->getName(), $attribute->hasValue() ? $attribute->getValue() : null);
108
    }
109
110
    /**
111
     * @param XmlAttribute $attribute
112
     */
113
    public function visitXmlAttribute(XmlAttribute $attribute)
114
    {
115
        $attr = $this->node->setAttribute($attribute->getName(), $attribute->hasValue() ? $attribute->getValue() : null);
116
        if ($attribute->hasPrefix()) {
117
            $attr->prefix = $attribute->getPrefix();
118
        }
119
    }
120
}