Conditions | 11 |
Paths | 45 |
Total Lines | 49 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
47 | protected function convertDomElement(DOMElement $element) |
||
48 | { |
||
49 | $sameNames = []; |
||
50 | $result = $this->convertAttributes($element->attributes); |
||
|
|||
51 | |||
52 | // Creates an index which counts each key, starting at zero, e.g. ['foo' => 2, 'bar' => 0] |
||
53 | $childNodeNames = []; |
||
54 | foreach ($element->childNodes as $key => $node) { |
||
55 | if (array_key_exists($node->nodeName, $sameNames)) { |
||
56 | $sameNames[$node->nodeName] += 1; |
||
57 | } else { |
||
58 | $sameNames[$node->nodeName] = 0; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | foreach ($element->childNodes as $key => $node) { |
||
63 | if (is_null($result)) { |
||
64 | $result = []; |
||
65 | } |
||
66 | |||
67 | if ($node instanceof DOMCdataSection) { |
||
68 | $result['_cdata'] = $node->data; |
||
69 | |||
70 | continue; |
||
71 | } |
||
72 | if ($node instanceof DOMText) { |
||
73 | if (empty($result)) { |
||
74 | $result = $node->textContent; |
||
75 | } else { |
||
76 | $result['_value'] = $node->textContent; |
||
77 | } |
||
78 | continue; |
||
79 | } |
||
80 | if ($node instanceof DOMElement) { |
||
81 | if ($sameNames[$node->nodeName]) { // Truthy — When $sameNames['foo'] > 0 |
||
82 | if (!array_key_exists($node->nodeName, $result)) { // Setup $result['foo'] |
||
83 | $result[$node->nodeName] = []; |
||
84 | } |
||
85 | |||
86 | $result[$node->nodeName][$key] = $this->convertDomElement($node); // Store as $result['foo'][*] |
||
87 | } else { |
||
88 | $result[$node->nodeName] = $this->convertDomElement($node); // Store as $result['foo'] |
||
89 | } |
||
90 | |||
91 | continue; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | return $result; |
||
96 | } |
||
113 |