Skip to content

Commit f68c3c2

Browse files
committed
Code refactoring
1 parent c3d3158 commit f68c3c2

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

src/XMLMapper.php

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,18 @@ public function getElement($tag, $obj = null)
131131
{
132132
$obj = $obj ?: $this->getObj();
133133

134-
if (property_exists($obj, $tag))
134+
if (property_exists($obj, $tag)) {
135135
return (new static())->loadObj($obj->{$tag});
136+
}
136137

137138
if ($obj instanceof \SimpleXMLElement) {
138139
foreach ($obj->children() as $key => $element) {
139-
140-
if ($key === $tag)
140+
if ($key === $tag) {
141141
return (new static())->loadObj($element);
142+
}
142143

143-
if ($element instanceof \SimpleXMLElement) {
144-
if ($found = $this->getElement($tag, $element)) {
144+
if ($element->count() && $found = $this->getElement($tag, $element)) {
145145
return $found;
146-
}
147146
}
148147
}
149148
}
@@ -167,7 +166,7 @@ public function getElements($tag, $obj = null)
167166
if ($var === $tag) {
168167
$list[] = (new static())->loadObj($element);
169168
} else {
170-
if ($found = $this->getElements($tag, $element)) {
169+
if ($element->count() && $found = $this->getElements($tag, $element)) {
171170
$list = array_merge($list, $found);
172171
}
173172
}
@@ -189,14 +188,14 @@ public function findValue($tag, $obj = null)
189188

190189
if ($obj instanceof \SimpleXMLElement) {
191190
foreach ($obj->children() as $var => $element) {
192-
if ($tag === $var)
191+
if ($tag === $var) {
193192
return $this->fetchValue($element);
193+
}
194194

195-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
196-
$found = $this->findValue($tag, $element);
197-
198-
if (is_string($found))
195+
if ($element->count() && $found = $this->findValue($tag, $element)) {
196+
if (is_string($found)) {
199197
return $found;
198+
}
200199
}
201200
}
202201
}
@@ -224,16 +223,18 @@ public function findAttribute($name, $tag = null, $obj = null)
224223
if ($obj instanceof \SimpleXMLElement) {
225224
foreach ($obj->children() as $var => $element) {
226225
if ($tag) {
227-
if ($tag === $var)
226+
if ($tag === $var) {
228227
return $this->fetchAttr($name, $element);
228+
}
229229
} else {
230-
if ($att = $this->fetchAttr($name, $element))
230+
if ($att = $this->fetchAttr($name, $element)) {
231231
return $att;
232+
}
232233
}
233-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
234-
$found = $this->findAttribute($name, $tag, $element);
235-
if (is_string($found))
234+
if ($element->count() && $found = $this->findAttribute($name, $tag, $element)) {
235+
if (is_string($found)) {
236236
return $found;
237+
}
237238
}
238239
}
239240
}
@@ -259,24 +260,26 @@ public function findAttributes($names, $tag = null, $obj = null)
259260
if ($tag) {
260261
if ($tag === $var) {
261262
foreach ($names as $name) {
262-
if ($val = $this->fetchAttr($name, $element))
263+
if ($val = $this->fetchAttr($name, $element)) {
263264
$return->{$name} = $val;
265+
}
264266
}
265267
}
266268
} else {
267269
foreach ($names as $name) {
268-
if ($val = $this->fetchAttr($name, $element))
270+
if ($val = $this->fetchAttr($name, $element)) {
269271
$return->{$name} = $val;
272+
}
270273
}
271274
}
272-
if (!empty(get_object_vars($return)))
275+
if (!empty(get_object_vars($return))) {
273276
return $return;
277+
}
274278

275-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
276-
$found = $this->findAttributes($names, $tag, $element);
277-
278-
if ($found instanceof \stdClass)
279+
if ($element->count() && $found = $this->findAttributes($names, $tag, $element)) {
280+
if ($found instanceof \stdClass) {
279281
return $found;
282+
}
280283
}
281284
}
282285
}
@@ -297,13 +300,14 @@ public function findAttributeWhere($name, $where, $obj = null)
297300

298301
if ($obj instanceof \SimpleXMLElement) {
299302
foreach ($obj->children() as $var => $element) {
300-
if ($this->checkCondition($where, $element))
303+
if ($this->checkCondition($where, $element)) {
301304
return $this->fetchAttr($name, $element);
305+
}
302306

303-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
304-
$found = $this->findAttributeWhere($name, $where, $element);
305-
if (is_string($found))
307+
if ($element->count() && $found = $this->findAttributeWhere($name, $where, $element)) {
308+
if (is_string($found)) {
306309
return $found;
310+
}
307311
}
308312
}
309313
}
@@ -329,16 +333,19 @@ public function findAttributesWhere($names, $where, $obj = null)
329333
if ($this->checkCondition($where, $element)) {
330334
foreach ($names as $name) {
331335
$val = $this->fetchAttr($name, $element);
332-
if ($val) $return->$name = $val;
336+
if ($val) {
337+
$return->$name = $val;
338+
}
333339
}
334340
}
335-
if (!empty(get_object_vars($return)))
341+
if (!empty(get_object_vars($return))) {
336342
return $return;
343+
}
337344

338-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
339-
$found = $this->findAttributesWhere($names, $where, $element);
340-
if ($found instanceof \stdClass)
345+
if ($element->count() && $found = $this->findAttributesWhere($names, $where, $element)) {
346+
if ($found instanceof \stdClass) {
341347
return $found;
348+
}
342349
}
343350
}
344351
}
@@ -359,17 +366,19 @@ public function findAllAttributesOf($tag, $obj = null)
359366

360367
if ($obj instanceof \SimpleXMLElement) {
361368
foreach ($obj->children() as $var => $element) {
362-
if ($var === $tag)
369+
if ($var === $tag) {
363370
$return[] = (object)current($element->attributes());
371+
}
364372

365-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
366-
$found = $this->findAllAttributesOf($tag, $element);
367-
if (is_array($found))
373+
if ($element->count() && $found = $this->findAllAttributesOf($tag, $element)) {
374+
if (is_array($found)) {
368375
return $found;
376+
}
369377
}
370378
}
371-
if (!empty($return))
379+
if (!empty($return)) {
372380
return $return;
381+
}
373382
}
374383
return null;
375384
}
@@ -390,18 +399,20 @@ public function findAllAttributesOfWhere($tag, $where, $obj = null)
390399
if ($obj instanceof \SimpleXMLElement) {
391400
foreach ($obj->children() as $var => $element) {
392401
if ($var === $tag) {
393-
if ($this->checkCondition($where, $element))
402+
if ($this->checkCondition($where, $element)) {
394403
$return[] = (object)current($element->attributes());
404+
}
395405
}
396-
if ($element instanceof \SimpleXMLElement && $element->count() > 0) {
397-
$found = $this->findAllAttributesOfWhere($tag, $where, $element);
398-
if (is_array($found))
406+
if ($element->count() && $found = $this->findAllAttributesOfWhere($tag, $where, $element)) {
407+
if (is_array($found)) {
399408
return $found;
409+
}
400410
}
401411
}
402412
}
403-
if (!empty($return))
413+
if (!empty($return)) {
404414
return $return;
415+
}
405416

406417
return null;
407418
}

0 commit comments

Comments
 (0)