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