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_Hydrate_Procedure'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $tab; |
24
|
|
|
/** |
25
|
|
|
* @var Hydrate |
26
|
|
|
*/ |
27
|
|
|
private $hydrate; |
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
|
6 |
|
public function __construct(ClassMapper $mapper, string $tab = null) |
40
|
|
|
{ |
41
|
6 |
|
$this->mapper = $mapper; |
42
|
6 |
|
$this->tab = "\t" . $tab; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Hydrate |
47
|
|
|
*/ |
48
|
6 |
|
public function getHydrate(): Hydrate |
49
|
|
|
{ |
50
|
6 |
|
return $this->hydrate; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
6 |
|
public function isValid(): bool |
57
|
|
|
{ |
58
|
6 |
|
return $this->hydrate !== 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
|
6 |
|
public function visitXmlElement(XmlElement $element) |
75
|
|
|
{ |
76
|
6 |
|
debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlElement: ' . $element->getName()); |
77
|
|
|
|
78
|
6 |
|
$this->visit($element); |
79
|
6 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param XmlNode $node |
83
|
|
|
*/ |
84
|
6 |
|
public function visitXmlNode(XmlNode $node) |
85
|
|
|
{ |
86
|
6 |
|
debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlNode: ' . $node->getName()); |
87
|
|
|
|
88
|
6 |
|
$this->visit($node); |
89
|
6 |
|
$this->visitChildrenOf($node); |
90
|
6 |
|
} |
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
|
4 |
|
public function visitXmlAttribute(XmlAttribute $attribute) |
104
|
|
|
{ |
105
|
4 |
|
$this->assignAttribute($attribute); |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Attribute $attribute |
110
|
|
|
*/ |
111
|
4 |
|
private function assignAttribute(Attribute $attribute) |
112
|
|
|
{ |
113
|
4 |
|
if ($attribute->hasValue()) { |
114
|
4 |
|
$this->hydrate->assign($attribute); |
115
|
|
|
} |
116
|
4 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Element $element |
120
|
|
|
*/ |
121
|
6 |
|
private function visit(Element $element) |
122
|
|
|
{ |
123
|
6 |
|
$this->hydrate = $this->mapper->new($element->getName()); |
|
|
|
|
124
|
6 |
|
if ($this->isValid()) { |
125
|
6 |
|
$this->visitAttributesOf($element); |
126
|
|
|
} |
127
|
6 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Element $element |
131
|
|
|
*/ |
132
|
6 |
|
private function visitAttributesOf(Element $element) |
133
|
|
|
{ |
134
|
6 |
|
foreach ($element->getAttributes() as $attribute) { |
135
|
4 |
|
$attribute->accept($this); |
136
|
|
|
} |
137
|
|
|
|
138
|
6 |
|
if ($element->hasValue()) { |
139
|
2 |
|
$this->hydrate->assignValue('value', $element->getValue()); |
140
|
|
|
} |
141
|
6 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param XmlNode $node |
145
|
|
|
*/ |
146
|
6 |
|
private function visitChildrenOf(XmlNode $node) |
147
|
|
|
{ |
148
|
6 |
|
foreach ($node->getChildren() as $child) { |
149
|
6 |
|
$this->visitChild($child); |
150
|
|
|
} |
151
|
6 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param Element $element |
155
|
|
|
*/ |
156
|
6 |
|
private function visitChild(Element $element) |
157
|
|
|
{ |
158
|
6 |
|
$procedure = new self($this->mapper, $this->tab); |
159
|
6 |
|
$element->accept($procedure); |
160
|
|
|
|
161
|
6 |
|
if ($this->isValid()) { |
162
|
5 |
|
$this->appendOrAssign($procedure, $element); |
|
|
|
|
163
|
|
|
} else { |
164
|
3 |
|
$this->skipTo($procedure); |
|
|
|
|
165
|
|
|
} |
166
|
6 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param HydrateProcedure $procedure |
|
|
|
|
170
|
|
|
* @param Element $element |
171
|
|
|
*/ |
172
|
5 |
|
private function appendOrAssign(self $procedure, Element $element) |
173
|
|
|
{ |
174
|
5 |
|
if ($procedure->isValid()) { |
175
|
4 |
|
$this->hydrate->append($procedure->getHydrate()); |
176
|
|
|
} else { |
177
|
5 |
|
$this->hydrate->assign($element); |
178
|
|
|
} |
179
|
5 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param HydrateProcedure $procedure |
|
|
|
|
183
|
|
|
*/ |
184
|
3 |
|
private function skipTo(self $procedure) |
185
|
|
|
{ |
186
|
3 |
|
if ($procedure->isValid()) { |
187
|
3 |
|
$this->hydrate = $procedure->getHydrate(); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
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 theid
property of an instance of theAccount
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.