| Conditions | 26 |
| Paths | 2748 |
| Total Lines | 159 |
| Code Lines | 88 |
| Lines | 23 |
| Ratio | 14.47 % |
| 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 |
||
| 78 | public function parse(&$data, $encoding) |
||
| 79 | { |
||
| 80 | // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character |
||
| 81 | if (strtoupper($encoding) === 'US-ASCII') |
||
| 82 | { |
||
| 83 | $this->encoding = 'UTF-8'; |
||
| 84 | } |
||
| 85 | else |
||
| 86 | { |
||
| 87 | $this->encoding = $encoding; |
||
| 88 | } |
||
| 89 | |||
| 90 | // Strip BOM: |
||
| 91 | // UTF-32 Big Endian BOM |
||
| 92 | if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") |
||
| 93 | { |
||
| 94 | $data = substr($data, 4); |
||
| 95 | } |
||
| 96 | // UTF-32 Little Endian BOM |
||
| 97 | elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") |
||
| 98 | { |
||
| 99 | $data = substr($data, 4); |
||
| 100 | } |
||
| 101 | // UTF-16 Big Endian BOM |
||
| 102 | elseif (substr($data, 0, 2) === "\xFE\xFF") |
||
| 103 | { |
||
| 104 | $data = substr($data, 2); |
||
| 105 | } |
||
| 106 | // UTF-16 Little Endian BOM |
||
| 107 | elseif (substr($data, 0, 2) === "\xFF\xFE") |
||
| 108 | { |
||
| 109 | $data = substr($data, 2); |
||
| 110 | } |
||
| 111 | // UTF-8 BOM |
||
| 112 | elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") |
||
| 113 | { |
||
| 114 | $data = substr($data, 3); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false) |
||
| 118 | { |
||
| 119 | $declaration = $this->registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5))); |
||
| 120 | if ($declaration->parse()) |
||
| 121 | { |
||
| 122 | $data = substr($data, $pos + 2); |
||
| 123 | $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data; |
||
| 124 | } |
||
| 125 | else |
||
| 126 | { |
||
| 127 | $this->error_string = 'SimplePie bug! Please report this!'; |
||
| 128 | return false; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $return = true; |
||
| 133 | |||
| 134 | static $xml_is_sane = null; |
||
| 135 | if ($xml_is_sane === null) |
||
| 136 | { |
||
| 137 | $parser_check = xml_parser_create(); |
||
| 138 | xml_parse_into_struct($parser_check, '<foo>&</foo>', $values); |
||
| 139 | xml_parser_free($parser_check); |
||
| 140 | $xml_is_sane = isset($values[0]['value']); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Create the parser |
||
| 144 | if ($xml_is_sane) |
||
| 145 | { |
||
| 146 | $xml = xml_parser_create_ns($this->encoding, $this->separator); |
||
| 147 | xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1); |
||
| 148 | xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0); |
||
| 149 | xml_set_object($xml, $this); |
||
| 150 | xml_set_character_data_handler($xml, 'cdata'); |
||
| 151 | xml_set_element_handler($xml, 'tag_open', 'tag_close'); |
||
| 152 | |||
| 153 | // Parse! |
||
| 154 | if (!xml_parse($xml, $data, true)) |
||
| 155 | { |
||
| 156 | $this->error_code = xml_get_error_code($xml); |
||
| 157 | $this->error_string = xml_error_string($this->error_code); |
||
| 158 | $return = false; |
||
| 159 | } |
||
| 160 | $this->current_line = xml_get_current_line_number($xml); |
||
| 161 | $this->current_column = xml_get_current_column_number($xml); |
||
| 162 | $this->current_byte = xml_get_current_byte_index($xml); |
||
| 163 | xml_parser_free($xml); |
||
| 164 | return $return; |
||
| 165 | } |
||
| 166 | else |
||
| 167 | { |
||
| 168 | libxml_clear_errors(); |
||
| 169 | $xml = new XMLReader(); |
||
| 170 | $xml->xml($data); |
||
| 171 | while (@$xml->read()) |
||
| 172 | { |
||
| 173 | switch ($xml->nodeType) |
||
| 174 | { |
||
| 175 | |||
| 176 | case constant('XMLReader::END_ELEMENT'): |
||
| 177 | if ($xml->namespaceURI !== '') |
||
| 178 | { |
||
| 179 | $tagName = $xml->namespaceURI . $this->separator . $xml->localName; |
||
| 180 | } |
||
| 181 | else |
||
| 182 | { |
||
| 183 | $tagName = $xml->localName; |
||
| 184 | } |
||
| 185 | $this->tag_close(null, $tagName); |
||
| 186 | break; |
||
| 187 | case constant('XMLReader::ELEMENT'): |
||
| 188 | $empty = $xml->isEmptyElement; |
||
| 189 | if ($xml->namespaceURI !== '') |
||
| 190 | { |
||
| 191 | $tagName = $xml->namespaceURI . $this->separator . $xml->localName; |
||
| 192 | } |
||
| 193 | else |
||
| 194 | { |
||
| 195 | $tagName = $xml->localName; |
||
| 196 | } |
||
| 197 | $attributes = array(); |
||
| 198 | while ($xml->moveToNextAttribute()) |
||
| 199 | { |
||
| 200 | if ($xml->namespaceURI !== '') |
||
| 201 | { |
||
| 202 | $attrName = $xml->namespaceURI . $this->separator . $xml->localName; |
||
| 203 | } |
||
| 204 | else |
||
| 205 | { |
||
| 206 | $attrName = $xml->localName; |
||
| 207 | } |
||
| 208 | $attributes[$attrName] = $xml->value; |
||
| 209 | } |
||
| 210 | $this->tag_open(null, $tagName, $attributes); |
||
| 211 | if ($empty) |
||
| 212 | { |
||
| 213 | $this->tag_close(null, $tagName); |
||
| 214 | } |
||
| 215 | break; |
||
| 216 | case constant('XMLReader::TEXT'): |
||
| 217 | |||
| 218 | case constant('XMLReader::CDATA'): |
||
| 219 | $this->cdata(null, $xml->value); |
||
| 220 | break; |
||
| 221 | } |
||
| 222 | } |
||
| 223 | if ($error = libxml_get_last_error()) |
||
| 224 | { |
||
| 225 | $this->error_code = $error->code; |
||
| 226 | $this->error_string = $error->message; |
||
| 227 | $this->current_line = $error->line; |
||
| 228 | $this->current_column = $error->column; |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | else |
||
| 232 | { |
||
| 233 | return true; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 408 |