| Conditions | 16 |
| Paths | 265 |
| Total Lines | 100 |
| Code Lines | 53 |
| 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 |
||
| 29 | public function parse(&$var, BasicObject &$o, $trigger) |
||
| 30 | { |
||
| 31 | if (!$var instanceof SimpleXMLElement) { |
||
| 32 | return; |
||
| 33 | } |
||
| 34 | |||
| 35 | $o->hints[] = 'simplexml_element'; |
||
| 36 | |||
| 37 | if (!self::$verbose) { |
||
| 38 | $o->removeRepresentation('properties'); |
||
| 39 | $o->removeRepresentation('iterator'); |
||
| 40 | $o->removeRepresentation('methods'); |
||
| 41 | } |
||
| 42 | |||
| 43 | // Attributes |
||
| 44 | $a = new Representation('Attributes'); |
||
| 45 | |||
| 46 | $base_obj = new BasicObject(); |
||
| 47 | $base_obj->depth = $o->depth; |
||
| 48 | |||
| 49 | if ($o->access_path) { |
||
| 50 | $base_obj->access_path = '(string) '.$o->access_path; |
||
| 51 | } |
||
| 52 | |||
| 53 | if ($var->attributes()) { |
||
| 54 | $attribs = iterator_to_array($var->attributes()); |
||
| 55 | $attribs = array_map('strval', $attribs); |
||
| 56 | } else { |
||
| 57 | $attribs = array(); |
||
| 58 | } |
||
| 59 | |||
| 60 | // XML attributes are by definition strings and don't have children, |
||
| 61 | // so up the depth limit in case we're just below the limit since |
||
| 62 | // there won't be any recursive stuff anyway. |
||
| 63 | $a->contents = $this->parser->parseDeep($attribs, $base_obj)->value->contents; |
||
| 64 | |||
| 65 | $o->addRepresentation($a, 0); |
||
| 66 | |||
| 67 | // Children |
||
| 68 | // We need to check children() separately from the values we already parsed because |
||
| 69 | // text contents won't show up in children() but they will show up in properties. |
||
| 70 | // |
||
| 71 | // Why do we still need to check for attributes if we already have an attributes() |
||
| 72 | // method? Hell if I know! |
||
| 73 | $children = $var->children(); |
||
| 74 | |||
| 75 | if ($o->value) { |
||
| 76 | $c = new Representation('Children'); |
||
| 77 | |||
| 78 | foreach ($o->value->contents as $value) { |
||
| 79 | if ($value->name === '@attributes') { |
||
| 80 | continue; |
||
| 81 | } elseif (isset($children->{$value->name})) { |
||
| 82 | $i = 0; |
||
| 83 | |||
| 84 | while (isset($children->{$value->name}[$i])) { |
||
| 85 | $base_obj = new BasicObject(); |
||
| 86 | $base_obj->depth = $o->depth + 1; |
||
| 87 | $base_obj->name = $value->name; |
||
| 88 | if ($value->access_path) { |
||
| 89 | $base_obj->access_path = $value->access_path.'['.$i.']'; |
||
| 90 | } |
||
| 91 | |||
| 92 | $value = $this->parser->parse($children->{$value->name}[$i], $base_obj); |
||
| 93 | |||
| 94 | if ($value->access_path && $value->type === 'string') { |
||
| 95 | $value->access_path = '(string) '.$value->access_path; |
||
| 96 | } |
||
| 97 | |||
| 98 | $c->contents[] = $value; |
||
| 99 | |||
| 100 | ++$i; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $o->size = count($c->contents); |
||
| 106 | |||
| 107 | if (!$o->size) { |
||
| 108 | $o->size = null; |
||
| 109 | |||
| 110 | if (strlen((string) $var)) { |
||
| 111 | $base_obj = new BlobObject(); |
||
| 112 | $base_obj->depth = $o->depth + 1; |
||
| 113 | $base_obj->name = $o->name; |
||
| 114 | if ($o->access_path) { |
||
| 115 | $base_obj->access_path = '(string) '.$o->access_path; |
||
| 116 | } |
||
| 117 | |||
| 118 | $value = (string) $var; |
||
| 119 | |||
| 120 | $c = new Representation('Contents'); |
||
| 121 | $c->implicit_label = true; |
||
| 122 | $c->contents = array($this->parser->parseDeep($value, $base_obj)); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $o->addRepresentation($c, 0); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 |
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.