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

HydrateProcedure::visitXmlElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
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\XmlElement;
9
use Dgame\Soap\XmlNode;
10
use function Dgame\Conditional\debug;
11
12
/**
13
 * Class HydrateProcedure
14
 * @package Dgame\Soap\Hydrator
15
 */
16
final class HydrateProcedure implements VisitorInterface
17
{
18
    const DEBUG_LABEL = 'Debug_Soap_Hydrator';
19
20
    /**
21
     * @var string
22
     */
23
    private $tab;
24
    /**
25
     * @var HydratableInterface
26
     */
27
    private $hydratable;
28
    /**
29
     * @var ClassMapper
30
     */
31
    private $mapper;
32
33
    /**
34
     * Hydrat constructor.
35
     *
36
     * @param ClassMapper $mapper
37
     * @param string|null $tab
38
     */
39
    public function __construct(ClassMapper $mapper, string $tab = null)
40
    {
41
        $this->mapper = $mapper;
42
        $this->tab    = "\t" . $tab;
43
    }
44
45
    /**
46
     * @return HydratableInterface
47
     */
48
    public function getHydratable(): HydratableInterface
49
    {
50
        return $this->hydratable;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isValid(): bool
57
    {
58
        return $this->hydratable !== null;
59
    }
60
61
    /**
62
     * @param Element $element
63
     */
64
    public function visitElement(Element $element)
65
    {
66
        debug(self::DEBUG_LABEL)->output($this->tab . ' - Element: ' . $element->getName());
67
68
        $this->visit($element);
69
    }
70
71
    /**
72
     * @param XmlElement $element
73
     */
74
    public function visitXmlElement(XmlElement $element)
75
    {
76
        debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlElement: ' . $element->getName());
77
78
        $this->visit($element);
79
    }
80
81
    /**
82
     * @param XmlNode $node
83
     */
84
    public function visitXmlNode(XmlNode $node)
85
    {
86
        debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlNode: ' . $node->getName());
87
88
        $this->visit($node);
89
        $this->visitChildrenOf($node);
90
    }
91
92
    /**
93
     * @param Attribute $attribute
94
     */
95
    public function visitAttribute(Attribute $attribute)
96
    {
97
        $this->assignAttribute($attribute);
98
    }
99
100
    /**
101
     * @param XmlAttribute $attribute
102
     */
103
    public function visitXmlAttribute(XmlAttribute $attribute)
104
    {
105
        $this->assignAttribute($attribute);
106
    }
107
108
    /**
109
     * @param Attribute $attribute
110
     */
111
    private function assignAttribute(Attribute $attribute)
112
    {
113
        if ($attribute->hasValue()) {
114
            $this->hydratable->assign($attribute);
115
        }
116
    }
117
118
    /**
119
     * @param Element $element
120
     */
121
    private function visit(Element $element)
122
    {
123
        $this->hydratable = $this->mapper->getInstanceOf($element->getName());
124
        if ($this->isValid()) {
125
            $this->visitAttributesOf($element);
126
        }
127
    }
128
129
    /**
130
     * @param Element $element
131
     */
132
    private function visitAttributesOf(Element $element)
133
    {
134
        foreach ($element->getAttributes() as $attribute) {
135
            $attribute->accept($this);
136
        }
137
138
        if ($element->hasValue()) {
139
            $this->hydratable->assignValue('value', $element->getValue());
140
        }
141
    }
142
143
    /**
144
     * @param XmlNode $node
145
     */
146
    private function visitChildrenOf(XmlNode $node)
147
    {
148
        foreach ($node->getChildren() as $child) {
149
            $this->visitChild($child);
150
        }
151
    }
152
153
    /**
154
     * @param Element $element
155
     */
156
    private function visitChild(Element $element)
157
    {
158
        $hydrator = new self($this->mapper, $this->tab);
159
        $element->accept($hydrator);
160
161
        if ($this->isValid() && $hydrator->isValid()) {
162
            $this->hydratable->append($hydrator->getHydratable());
0 ignored issues
show
Documentation introduced by
$hydrator->getHydratable() is of type object<Dgame\Soap\Hydrator\HydratableInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
        } else if ($this->isValid()) {
164
            $this->hydratable->assign($element);
165
        } else if ($hydrator->isValid()) {
166
            $this->hydratable = $hydrator->getHydratable();
167
        }
168
    }
169
}