Completed
Push — master ( 126b63...733cbc )
by Randy
04:39
created

Assembler::visitXmlElement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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