Conditions | 11 |
Paths | 21 |
Total Lines | 48 |
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 |
||
64 | function dom_to_array($root) |
||
65 | { |
||
66 | // if the node has only a single text node |
||
67 | if (!$root->hasAttributes() && $root->childNodes->length==1 |
||
68 | && $root->childNodes->item(0)->nodeType == XML_TEXT_NODE) { |
||
69 | return $root->childNodes->item(0)->nodeValue; |
||
70 | } |
||
71 | |||
72 | $result = array(); |
||
73 | |||
74 | if ($root->hasAttributes()) { |
||
75 | $attrs = $root->attributes; |
||
76 | |||
77 | foreach ($attrs as $i => $attr) { |
||
78 | $result["_" . $attr->name] = $attr->value; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $children = $root->childNodes; |
||
83 | |||
84 | $group = array(); |
||
85 | |||
86 | $text = ""; |
||
87 | |||
88 | for ($i = 0; $i < $children->length; $i++) { |
||
89 | $child = $children->item($i); |
||
90 | if ($child->nodeType == XML_TEXT_NODE) { |
||
91 | $text = $text . $child->nodeValue; |
||
92 | } else { |
||
93 | if (!isset($result[$child->nodeName])) { |
||
94 | $result[$child->nodeName] = dom_to_array($child); |
||
|
|||
95 | } else { |
||
96 | if (!isset($group[$child->nodeName])) { |
||
97 | $tmp = $result[$child->nodeName]; |
||
98 | $result[$child->nodeName] = array($tmp); |
||
99 | $group[$child->nodeName] = 1; |
||
100 | } |
||
101 | |||
102 | $result[$child->nodeName][] = dom_to_array($child); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | $trimmed = trim($text); |
||
107 | if ($trimmed != "") { |
||
108 | $result['#text'] = $text; |
||
109 | } |
||
110 | return $result; |
||
111 | } |
||
112 | /** |
||
126 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.