| Conditions | 21 |
| Paths | 400 |
| Total Lines | 104 |
| Code Lines | 62 |
| 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 |
||
| 138 | protected function parseNode(&$var, InstanceObject &$o, $trigger) |
||
| 139 | { |
||
| 140 | // Fill the properties |
||
| 141 | // They can't be enumerated through reflection or casting, |
||
| 142 | // so we have to trust the docs and try them one at a time |
||
| 143 | $known_properties = array( |
||
| 144 | 'nodeValue', |
||
| 145 | 'childNodes', |
||
| 146 | 'attributes', |
||
| 147 | ); |
||
| 148 | |||
| 149 | if (self::$verbose) { |
||
| 150 | $known_properties = array( |
||
| 151 | 'nodeName', |
||
| 152 | 'nodeValue', |
||
| 153 | 'nodeType', |
||
| 154 | 'parentNode', |
||
| 155 | 'childNodes', |
||
| 156 | 'firstChild', |
||
| 157 | 'lastChild', |
||
| 158 | 'previousSibling', |
||
| 159 | 'nextSibling', |
||
| 160 | 'attributes', |
||
| 161 | 'ownerDocument', |
||
| 162 | 'namespaceURI', |
||
| 163 | 'prefix', |
||
| 164 | 'localName', |
||
| 165 | 'baseURI', |
||
| 166 | 'textContent', |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | $childNodes = array(); |
||
| 171 | $attributes = array(); |
||
| 172 | |||
| 173 | $rep = $o->value; |
||
| 174 | |||
| 175 | foreach ($known_properties as $prop) { |
||
| 176 | $prop_obj = $this->parseProperty($o, $prop, $var); |
||
| 177 | $rep->contents[] = $prop_obj; |
||
| 178 | |||
| 179 | if ($prop === 'childNodes') { |
||
| 180 | $childNodes = $prop_obj->getRepresentation('iterator'); |
||
| 181 | } elseif ($prop === 'attributes') { |
||
| 182 | $attributes = $prop_obj->getRepresentation('iterator'); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!self::$verbose) { |
||
| 187 | $o->removeRepresentation('methods'); |
||
| 188 | $o->removeRepresentation('properties'); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Attributes and comments and text nodes don't |
||
| 192 | // need children or attributes of their own |
||
| 193 | if (in_array($o->classname, array('DOMAttr', 'DOMText', 'DOMComment'))) { |
||
| 194 | return; |
||
| 195 | } |
||
| 196 | |||
| 197 | // Set the attributes |
||
| 198 | if ($attributes) { |
||
| 199 | $a = new Representation('Attributes'); |
||
| 200 | foreach ($attributes->contents as $attribute) { |
||
| 201 | $a->contents[] = self::textualNodeToString($attribute); |
||
| 202 | } |
||
| 203 | $o->addRepresentation($a, 0); |
||
| 204 | } |
||
| 205 | |||
| 206 | // Set the children |
||
| 207 | if ($childNodes) { |
||
| 208 | $c = new Representation('Children'); |
||
| 209 | |||
| 210 | if (count($childNodes->contents) === 1 && ($node = reset($childNodes->contents)) && in_array('depth_limit', $node->hints)) { |
||
| 211 | $node = $node->transplant(new InstanceObject()); |
||
| 212 | $node->name = 'childNodes'; |
||
| 213 | $node->classname = 'DOMNodeList'; |
||
| 214 | $c->contents = array($node); |
||
| 215 | } else { |
||
| 216 | foreach ($childNodes->contents as $index => $node) { |
||
| 217 | // Shortcircuit text nodes to plain strings |
||
| 218 | if ($node->classname === 'DOMText' || $node->classname === 'DOMComment') { |
||
| 219 | $node = self::textualNodeToString($node); |
||
| 220 | |||
| 221 | // And remove them if they're empty |
||
| 222 | if (ctype_space($node->value->contents) || $node->value->contents === '') { |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | $c->contents[] = $node; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | $o->addRepresentation($c, 0); |
||
| 232 | } |
||
| 233 | |||
| 234 | if (isset($c) && count($c->contents)) { |
||
| 235 | $o->size = count($c->contents); |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!$o->size) { |
||
| 239 | $o->size = null; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 298 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.