Assembler::visitXmlElement()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Soap\Attribute\Attribute;
6
use Dgame\Soap\Attribute\XmlAttribute;
7
use Dgame\Soap\Element;
8
use Dgame\Soap\Visitor\AttributeVisitorInterface;
9
use Dgame\Soap\Visitor\ElementVisitorInterface;
10
use Dgame\Soap\XmlElement;
11
use Dgame\Soap\XmlNode;
12
use DOMDocument;
13
use DOMElement;
14
use DOMNode;
15
16
/**
17
 * Class Assembler
18
 * @package Dgame\Soap\Hydrator
19
 */
20
final class Assembler implements ElementVisitorInterface, AttributeVisitorInterface
21
{
22
    /**
23
     * @var DOMDocument
24
     */
25
    private $document;
26
    /**
27
     * @var DOMElement
28
     */
29
    private $node;
30
31
    /**
32
     * Assembler constructor.
33
     *
34
     * @param DOMNode|null $node
35
     */
36
    public function __construct(DOMNode $node = null)
37
    {
38
        if ($node === null) {
39
            $this->node = $this->document = new DOMDocument('1.0', 'utf-8');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->document = new \D...ocument('1.0', 'utf-8') of type object<DOMDocument> is incompatible with the declared type object<DOMElement> of property $node.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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