Conditions | 19 |
Paths | 34 |
Total Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | public static function nodeToArray( |
||
28 | PHP_DOMElement $node, |
||
29 | $forceArrayKeys = ['hostAttr', 'hostObj', 'street', 'hostAddr'], |
||
30 | $ignoreAttributeKeys = ['hostAddr'] |
||
31 | ) { |
||
32 | $tmp = []; |
||
33 | foreach ($node->childNodes as $each) { |
||
34 | if ($each->nodeType !== XML_ELEMENT_NODE) { |
||
35 | continue; |
||
36 | } |
||
37 | |||
38 | // if node only has a type attribute lets distinguish them directly |
||
39 | // and then ignore the attribtue |
||
40 | if ($each->hasAttribute('type')) { |
||
41 | $key = $each->localName . '@' . $each->getAttribute('type'); |
||
42 | $ignore_attributes = true; |
||
43 | } else { |
||
44 | $key = $each->localName; |
||
45 | $ignore_attributes = false; |
||
46 | } |
||
47 | |||
48 | // in case of special keys, always create array |
||
49 | if (in_array($key, $forceArrayKeys)) { |
||
50 | $current = &$tmp[$key][]; |
||
51 | end($tmp[$key]); |
||
52 | $insert_key = key($tmp[$key]); |
||
53 | } |
||
54 | // if key already exists, dynamically create an array |
||
55 | elseif (isset($tmp[$key])) { |
||
56 | if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) { |
||
57 | $tmp[$key] = [$tmp[$key]]; |
||
58 | } |
||
59 | $current = &$tmp[$key][]; |
||
60 | end($tmp[$key]); |
||
61 | $insert_key = key($tmp[$key]); |
||
62 | } |
||
63 | // key was not yet set, so lets start off with a string |
||
64 | else { |
||
65 | $current = &$tmp[$key]; |
||
66 | $insert_key = null; |
||
67 | } |
||
68 | |||
69 | if ($each->hasChildNodes()) { |
||
70 | $current = static::nodeToArray($each, $forceArrayKeys, $ignoreAttributeKeys); |
||
71 | } else { |
||
72 | $current = $each->nodeValue; |
||
73 | |||
74 | if (!$ignore_attributes && !in_array($each->localName, $ignoreAttributeKeys) && $each->hasAttributes()) { |
||
75 | foreach ($each->attributes as $attr) { |
||
76 | |||
77 | // single attribute with empty node, use the attr-value directly |
||
78 | if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) { |
||
79 | $current = $attr->nodeValue; |
||
80 | break; |
||
81 | } |
||
82 | |||
83 | if ($insert_key) { |
||
84 | if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) { |
||
85 | $tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]]; |
||
86 | } |
||
87 | $tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue; |
||
88 | } else { |
||
89 | $tmp['@' . $key][$attr->nodeName] = $attr->nodeValue; |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return $tmp; |
||
97 | } |
||
98 | } |
||
99 |