Conditions | 10 |
Paths | 20 |
Total Lines | 54 |
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 | $result = $this->convertAttributes($element->attributes); |
||
|
|||
50 | |||
51 | $sameNamesOccurrences = []; |
||
52 | $sameNodeNameIndexes = []; |
||
53 | |||
54 | if ($element->childNodes->length > 1) { |
||
55 | $childNodeNames = []; |
||
56 | |||
57 | foreach ($element->childNodes as $node) { |
||
58 | $childNodeNames[] = $node->nodeName; |
||
59 | } |
||
60 | |||
61 | $sameNamesOccurrences = array_count_values($childNodeNames); |
||
62 | } |
||
63 | |||
64 | foreach ($element->childNodes as $node) { |
||
65 | if ($node instanceof DOMCdataSection) { |
||
66 | $result['_cdata'] = $node->data; |
||
67 | |||
68 | continue; |
||
69 | } |
||
70 | if ($node instanceof DOMText) { |
||
71 | $result = $node->textContent; |
||
72 | |||
73 | continue; |
||
74 | } |
||
75 | if ($node instanceof DOMElement) { |
||
76 | $nodeName = $node->nodeName; |
||
77 | $hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1; |
||
78 | |||
79 | if ($hasSameName === false) { |
||
80 | $result[$nodeName] = $this->convertDomElement($node); |
||
81 | continue; |
||
82 | } |
||
83 | |||
84 | // If we already have a child node with the same name, we need to increment |
||
85 | // and keep track of their index. |
||
86 | |||
87 | if (isset($sameNodeNameIndexes[$nodeName])) { |
||
88 | $key = $sameNodeNameIndexes[$nodeName] + 1; |
||
89 | } else { |
||
90 | $key = 0; |
||
91 | } |
||
92 | |||
93 | $result[$nodeName][$key] = $this->convertDomElement($node); |
||
94 | $sameNodeNameIndexes[$nodeName] = $key; |
||
95 | |||
96 | continue; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return $result; |
||
101 | } |
||
118 |