Total Complexity | 129 |
Total Lines | 722 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like xmlArray often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use xmlArray, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class xmlArray |
||
24 | { |
||
25 | /** |
||
26 | * @var array Holds parsed XML results |
||
27 | */ |
||
28 | public $array; |
||
29 | |||
30 | /** |
||
31 | * @var int The debugging level |
||
32 | */ |
||
33 | public $debug_level; |
||
34 | |||
35 | /** |
||
36 | * holds trim level textual data |
||
37 | * |
||
38 | * @var bool Holds trim level textual data |
||
39 | */ |
||
40 | public $trim; |
||
41 | |||
42 | /** |
||
43 | * Constructor for the xml parser. |
||
44 | * Example use: |
||
45 | * $xml = new xmlArray(file('data.xml')); |
||
46 | * |
||
47 | * @param string $data The xml data or an array of, unless is_clone is true. |
||
48 | * @param bool $auto_trim Used to automatically trim textual data. |
||
49 | * @param int $level The debug level. Specifies whether notices should be generated for missing elements and attributes. |
||
50 | * @param bool $is_clone default false. If is_clone is true, the xmlArray is cloned from another - used internally only. |
||
51 | */ |
||
52 | public function __construct($data, $auto_trim = false, $level = null, $is_clone = false) |
||
53 | { |
||
54 | // If we're using this try to get some more memory. |
||
55 | setMemoryLimit('32M'); |
||
56 | |||
57 | // Set the debug level. |
||
58 | $this->debug_level = $level !== null ? $level : error_reporting(); |
||
59 | $this->trim = $auto_trim; |
||
60 | |||
61 | // Is the data already parsed? |
||
62 | if ($is_clone) |
||
63 | { |
||
64 | $this->array = $data; |
||
|
|||
65 | return; |
||
66 | } |
||
67 | |||
68 | // Is the input an array? (ie. passed from file()?) |
||
69 | if (is_array($data)) |
||
70 | $data = implode('', $data); |
||
71 | |||
72 | // Remove any xml declaration or doctype, and parse out comments and CDATA. |
||
73 | $data = preg_replace('/<!--.*?-->/s', '', $this->_to_cdata(preg_replace(array('/^<\?xml.+?\?' . '>/is', '/<!DOCTYPE[^>]+?' . '>/s'), '', $data))); |
||
74 | |||
75 | // Now parse the xml! |
||
76 | $this->array = $this->_parse($data); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get the root element's name. |
||
81 | * Example use: |
||
82 | * echo $element->name(); |
||
83 | * |
||
84 | * @return string The root element's name |
||
85 | */ |
||
86 | public function name() |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get a specified element's value or attribute by path. |
||
93 | * Children are parsed for text, but only textual data is returned |
||
94 | * unless get_elements is true. |
||
95 | * Example use: |
||
96 | * $data = $xml->fetch('html/head/title'); |
||
97 | * |
||
98 | * @param string $path The path to the element to fetch |
||
99 | * @param bool $get_elements Whether to include elements |
||
100 | * @return string The value or attribute of the specified element |
||
101 | */ |
||
102 | public function fetch($path, $get_elements = false) |
||
129 | } |
||
130 | |||
131 | /** Get an element, returns a new xmlArray. |
||
132 | * It finds any elements that match the path specified. |
||
133 | * It will always return a set if there is more than one of the element |
||
134 | * or return_set is true. |
||
135 | * Example use: |
||
136 | * $element = $xml->path('html/body'); |
||
137 | * |
||
138 | * @param $path string The path to the element to get |
||
139 | * @param $return_full bool Whether to return the full result set |
||
140 | * @return xmlArray a new xmlArray. |
||
141 | */ |
||
142 | public function path($path, $return_full = false) |
||
143 | { |
||
144 | global $txt; |
||
145 | |||
146 | // Split up the path. |
||
147 | $path = explode('/', $path); |
||
148 | |||
149 | // Start with a base array. |
||
150 | $array = $this->array; |
||
151 | |||
152 | // For each element in the path. |
||
153 | foreach ($path as $el) |
||
154 | { |
||
155 | // Deal with sets.... |
||
156 | if (strpos($el, '[') !== false) |
||
157 | { |
||
158 | $lvl = (int) substr($el, strpos($el, '[') + 1); |
||
159 | $el = substr($el, 0, strpos($el, '[')); |
||
160 | } |
||
161 | // Find an attribute. |
||
162 | elseif (substr($el, 0, 1) == '@') |
||
163 | { |
||
164 | // It simplifies things if the attribute is already there ;). |
||
165 | if (isset($array[$el])) |
||
166 | return $array[$el]; |
||
167 | else |
||
168 | { |
||
169 | $trace = debug_backtrace(); |
||
170 | $i = 0; |
||
171 | while ($i < count($trace) && isset($trace[$i]['class']) && $trace[$i]['class'] == get_class($this)) |
||
172 | $i++; |
||
173 | $debug = ' (from ' . $trace[$i - 1]['file'] . ' on line ' . $trace[$i - 1]['line'] . ')'; |
||
174 | |||
175 | // Cause an error. |
||
176 | if ($this->debug_level & E_NOTICE) |
||
177 | { |
||
178 | loadLanguage('Errors'); |
||
179 | trigger_error(sprintf($txt['undefined_xml_attribute'], substr($el, 1) . $debug), E_USER_NOTICE); |
||
180 | } |
||
181 | return false; |
||
182 | } |
||
183 | } |
||
184 | else |
||
185 | $lvl = null; |
||
186 | |||
187 | // Find this element. |
||
188 | $array = $this->_path($array, $el, $lvl); |
||
189 | } |
||
190 | |||
191 | // Clean up after $lvl, for $return_full. |
||
192 | if ($return_full && (!isset($array['name']) || substr($array['name'], -1) != ']')) |
||
193 | $array = array('name' => $el . '[]', $array); |
||
194 | |||
195 | // Create the right type of class... |
||
196 | $newClass = get_class($this); |
||
197 | |||
198 | // Return a new xmlArray for the result. |
||
199 | return $array === false ? false : new $newClass($array, $this->trim, $this->debug_level, true); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Check if an element exists. |
||
204 | * Example use, |
||
205 | * echo $xml->exists('html/body') ? 'y' : 'n'; |
||
206 | * |
||
207 | * @param string $path The path to the element to get. |
||
208 | * @return boolean Whether the specified path exists |
||
209 | */ |
||
210 | public function exists($path) |
||
211 | { |
||
212 | // Split up the path. |
||
213 | $path = explode('/', $path); |
||
214 | |||
215 | // Start with a base array. |
||
216 | $array = $this->array; |
||
217 | |||
218 | // For each element in the path. |
||
219 | foreach ($path as $el) |
||
220 | { |
||
221 | // Deal with sets.... |
||
222 | if (strpos($el, '[') !== false) |
||
223 | { |
||
224 | $lvl = (int) substr($el, strpos($el, '[') + 1); |
||
225 | $el = substr($el, 0, strpos($el, '[')); |
||
226 | } |
||
227 | // Find an attribute. |
||
228 | elseif (substr($el, 0, 1) == '@') |
||
229 | return isset($array[$el]); |
||
230 | else |
||
231 | $lvl = null; |
||
232 | |||
233 | // Find this element. |
||
234 | $array = $this->_path($array, $el, $lvl, true); |
||
235 | } |
||
236 | |||
237 | return $array !== false; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Count the number of occurrences of a path. |
||
242 | * Example use: |
||
243 | * echo $xml->count('html/head/meta'); |
||
244 | * |
||
245 | * @param string $path The path to search for. |
||
246 | * @return int The number of elements the path matches. |
||
247 | */ |
||
248 | public function count($path) |
||
249 | { |
||
250 | // Get the element, always returning a full set. |
||
251 | $temp = $this->path($path, true); |
||
252 | |||
253 | // Start at zero, then count up all the numeric keys. |
||
254 | $i = 0; |
||
255 | foreach ($temp->array as $item) |
||
256 | { |
||
257 | if (is_array($item)) |
||
258 | $i++; |
||
259 | } |
||
260 | |||
261 | return $i; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get an array of xmlArray's matching the specified path. |
||
266 | * This differs from ->path(path, true) in that instead of an xmlArray |
||
267 | * of elements, an array of xmlArray's is returned for use with foreach. |
||
268 | * Example use: |
||
269 | * foreach ($xml->set('html/body/p') as $p) |
||
270 | * |
||
271 | * @param $path string The path to search for. |
||
272 | * @return xmlArray[] An array of xmlArray objects |
||
273 | */ |
||
274 | public function set($path) |
||
275 | { |
||
276 | // None as yet, just get the path. |
||
277 | $array = array(); |
||
278 | $xml = $this->path($path, true); |
||
279 | |||
280 | foreach ($xml->array as $val) |
||
281 | { |
||
282 | // Skip these, they aren't elements. |
||
283 | if (!is_array($val) || $val['name'] == '!') |
||
284 | continue; |
||
285 | |||
286 | // Create the right type of class... |
||
287 | $newClass = get_class($this); |
||
288 | |||
289 | // Create a new xmlArray and stick it in the array. |
||
290 | $array[] = new $newClass($val, $this->trim, $this->debug_level, true); |
||
291 | } |
||
292 | |||
293 | return $array; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Create an xml file from an xmlArray, the specified path if any. |
||
298 | * Example use: |
||
299 | * echo $this->create_xml(); |
||
300 | * |
||
301 | * @param string $path The path to the element. (optional) |
||
302 | * @return string Xml-formatted string. |
||
303 | */ |
||
304 | public function create_xml($path = null) |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Output the xml in an array form. |
||
327 | * Example use: |
||
328 | * print_r($xml->to_array()); |
||
329 | * |
||
330 | * @param string $path The path to output. |
||
331 | * @return array An array of XML data |
||
332 | */ |
||
333 | public function to_array($path = null) |
||
334 | { |
||
335 | // Are we doing a specific path? |
||
336 | if ($path !== null) |
||
337 | { |
||
338 | $path = $this->path($path); |
||
339 | |||
340 | // The path was not found |
||
341 | if ($path === false) |
||
342 | return false; |
||
343 | |||
344 | $path = $path->array; |
||
345 | } |
||
346 | // No, so just use the current array. |
||
347 | else |
||
348 | $path = $this->array; |
||
349 | |||
350 | return $this->_array($path); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Parse data into an array. (privately used...) |
||
355 | * |
||
356 | * @param string $data The data to parse |
||
357 | * @return array The parsed array |
||
358 | */ |
||
359 | protected function _parse($data) |
||
360 | { |
||
361 | // Start with an 'empty' array with no data. |
||
362 | $current = array( |
||
363 | ); |
||
364 | |||
365 | // Loop until we're out of data. |
||
366 | while ($data != '') |
||
367 | { |
||
368 | // Find and remove the next tag. |
||
369 | preg_match('/\A<([\w\-:]+)((?:\s+.+?)?)([\s]?\/)?' . '>/', $data, $match); |
||
370 | if (isset($match[0])) |
||
371 | $data = preg_replace('/' . preg_quote($match[0], '/') . '/s', '', $data, 1); |
||
372 | |||
373 | // Didn't find a tag? Keep looping.... |
||
374 | if (!isset($match[1]) || $match[1] == '') |
||
375 | { |
||
376 | // If there's no <, the rest is data. |
||
377 | if (strpos($data, '<') === false) |
||
378 | { |
||
379 | $text_value = $this->_from_cdata($data); |
||
380 | $data = ''; |
||
381 | |||
382 | if ($text_value != '') |
||
383 | $current[] = array( |
||
384 | 'name' => '!', |
||
385 | 'value' => $text_value |
||
386 | ); |
||
387 | } |
||
388 | // If the < isn't immediately next to the current position... more data. |
||
389 | elseif (strpos($data, '<') > 0) |
||
390 | { |
||
391 | $text_value = $this->_from_cdata(substr($data, 0, strpos($data, '<'))); |
||
392 | $data = substr($data, strpos($data, '<')); |
||
393 | |||
394 | if ($text_value != '') |
||
395 | $current[] = array( |
||
396 | 'name' => '!', |
||
397 | 'value' => $text_value |
||
398 | ); |
||
399 | } |
||
400 | // If we're looking at a </something> with no start, kill it. |
||
401 | elseif (strpos($data, '<') !== false && strpos($data, '<') == 0) |
||
402 | { |
||
403 | if (strpos($data, '<', 1) !== false) |
||
404 | { |
||
405 | $text_value = $this->_from_cdata(substr($data, 0, strpos($data, '<', 1))); |
||
406 | $data = substr($data, strpos($data, '<', 1)); |
||
407 | |||
408 | if ($text_value != '') |
||
409 | $current[] = array( |
||
410 | 'name' => '!', |
||
411 | 'value' => $text_value |
||
412 | ); |
||
413 | } |
||
414 | else |
||
415 | { |
||
416 | $text_value = $this->_from_cdata($data); |
||
417 | $data = ''; |
||
418 | |||
419 | if ($text_value != '') |
||
420 | $current[] = array( |
||
421 | 'name' => '!', |
||
422 | 'value' => $text_value |
||
423 | ); |
||
424 | } |
||
425 | } |
||
426 | |||
427 | // Wait for an actual occurance of an element. |
||
428 | continue; |
||
429 | } |
||
430 | |||
431 | // Create a new element in the array. |
||
432 | $el = &$current[]; |
||
433 | $el['name'] = $match[1]; |
||
434 | |||
435 | // If this ISN'T empty, remove the close tag and parse the inner data. |
||
436 | if ((!isset($match[3]) || trim($match[3]) != '/') && (!isset($match[2]) || trim($match[2]) != '/')) |
||
437 | { |
||
438 | // Because PHP 5.2.0+ seems to croak using regex, we'll have to do this the less fun way. |
||
439 | $last_tag_end = strpos($data, '</' . $match[1] . '>'); |
||
440 | if ($last_tag_end === false) |
||
441 | continue; |
||
442 | |||
443 | $offset = 0; |
||
444 | while (1 == 1) |
||
445 | { |
||
446 | // Where is the next start tag? |
||
447 | $next_tag_start = strpos($data, '<' . $match[1], $offset); |
||
448 | // If the next start tag is after the last end tag then we've found the right close. |
||
449 | if ($next_tag_start === false || $next_tag_start > $last_tag_end) |
||
450 | break; |
||
451 | |||
452 | // If not then find the next ending tag. |
||
453 | $next_tag_end = strpos($data, '</' . $match[1] . '>', $offset); |
||
454 | |||
455 | // Didn't find one? Then just use the last and sod it. |
||
456 | if ($next_tag_end === false) |
||
457 | break; |
||
458 | else |
||
459 | { |
||
460 | $last_tag_end = $next_tag_end; |
||
461 | $offset = $next_tag_start + 1; |
||
462 | } |
||
463 | } |
||
464 | // Parse the insides. |
||
465 | $inner_match = substr($data, 0, $last_tag_end); |
||
466 | // Data now starts from where this section ends. |
||
467 | $data = substr($data, $last_tag_end + strlen('</' . $match[1] . '>')); |
||
468 | |||
469 | if (!empty($inner_match)) |
||
470 | { |
||
471 | // Parse the inner data. |
||
472 | if (strpos($inner_match, '<') !== false) |
||
473 | $el += $this->_parse($inner_match); |
||
474 | elseif (trim($inner_match) != '') |
||
475 | { |
||
476 | $text_value = $this->_from_cdata($inner_match); |
||
477 | if ($text_value != '') |
||
478 | $el[] = array( |
||
479 | 'name' => '!', |
||
480 | 'value' => $text_value |
||
481 | ); |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | |||
486 | // If we're dealing with attributes as well, parse them out. |
||
487 | if (isset($match[2]) && $match[2] != '') |
||
488 | { |
||
489 | // Find all the attribute pairs in the string. |
||
490 | preg_match_all('/([\w:]+)="(.+?)"/', $match[2], $attr, PREG_SET_ORDER); |
||
491 | |||
492 | // Set them as @attribute-name. |
||
493 | foreach ($attr as $match_attr) |
||
494 | $el['@' . $match_attr[1]] = $match_attr[2]; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | // Return the parsed array. |
||
499 | return $current; |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Get a specific element's xml. (privately used...) |
||
504 | * |
||
505 | * @param array $array An array of element data |
||
506 | * @param null|int $indent How many levels to indent the elements (null = no indent) |
||
507 | * @return string The formatted XML |
||
508 | */ |
||
509 | protected function _xml($array, $indent) |
||
510 | { |
||
511 | $indentation = $indent !== null ? ' |
||
512 | ' . str_repeat(' ', $indent) : ''; |
||
513 | |||
514 | // This is a set of elements, with no name... |
||
515 | if (is_array($array) && !isset($array['name'])) |
||
516 | { |
||
517 | $temp = ''; |
||
518 | foreach ($array as $val) |
||
519 | $temp .= $this->_xml($val, $indent); |
||
520 | return $temp; |
||
521 | } |
||
522 | |||
523 | // This is just text! |
||
524 | if ($array['name'] == '!') |
||
525 | return $indentation . '<![CDATA[' . $array['value'] . ']]>'; |
||
526 | elseif (substr($array['name'], -2) == '[]') |
||
527 | $array['name'] = substr($array['name'], 0, -2); |
||
528 | |||
529 | // Start the element. |
||
530 | $output = $indentation . '<' . $array['name']; |
||
531 | |||
532 | $inside_elements = false; |
||
533 | $output_el = ''; |
||
534 | |||
535 | // Run through and recursively output all the elements or attrbutes inside this. |
||
536 | foreach ($array as $k => $v) |
||
537 | { |
||
538 | if (substr($k, 0, 1) == '@') |
||
539 | $output .= ' ' . substr($k, 1) . '="' . $v . '"'; |
||
540 | elseif (is_array($v)) |
||
541 | { |
||
542 | $output_el .= $this->_xml($v, $indent === null ? null : $indent + 1); |
||
543 | $inside_elements = true; |
||
544 | } |
||
545 | } |
||
546 | |||
547 | // Indent, if necessary.... then close the tag. |
||
548 | if ($inside_elements) |
||
549 | $output .= '>' . $output_el . $indentation . '</' . $array['name'] . '>'; |
||
550 | else |
||
551 | $output .= ' />'; |
||
552 | |||
553 | return $output; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Return an element as an array |
||
558 | * |
||
559 | * @param array $array An array of data |
||
560 | * @return string|array A string with the element's value or an array of element data |
||
561 | */ |
||
562 | protected function _array($array) |
||
563 | { |
||
564 | $return = array(); |
||
565 | $text = ''; |
||
566 | foreach ($array as $value) |
||
567 | { |
||
568 | if (!is_array($value) || !isset($value['name'])) |
||
569 | continue; |
||
570 | |||
571 | if ($value['name'] == '!') |
||
572 | $text .= $value['value']; |
||
573 | else |
||
574 | $return[$value['name']] = $this->_array($value); |
||
575 | } |
||
576 | |||
577 | if (empty($return)) |
||
578 | return $text; |
||
579 | else |
||
580 | return $return; |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Parse out CDATA tags. (htmlspecialchars them...) |
||
585 | * |
||
586 | * @param string $data The data with CDATA tags included |
||
587 | * @return string The data contained within CDATA tags |
||
588 | */ |
||
589 | function _to_cdata($data) |
||
619 | } |
||
620 | |||
621 | /** |
||
622 | * Turn the CDATAs back to normal text. |
||
623 | * |
||
624 | * @param string $data The data with CDATA tags |
||
625 | * @return string The transformed data |
||
626 | */ |
||
627 | protected function _from_cdata($data) |
||
628 | { |
||
629 | // Get the HTML translation table and reverse it. |
||
630 | $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)); |
||
631 | |||
632 | // Translate all the entities out. |
||
633 | $data = strtr( |
||
634 | preg_replace_callback( |
||
635 | '~&#(\d{1,4});~', |
||
636 | function($m) |
||
637 | { |
||
638 | return chr("$m[1]"); |
||
639 | }, |
||
640 | $data |
||
641 | ), |
||
642 | $trans_tbl |
||
643 | ); |
||
644 | |||
645 | return $this->trim ? trim($data) : $data; |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * Given an array, return the text from that array. (recursive and privately used.) |
||
650 | * |
||
651 | * @param array $array An aray of data |
||
652 | * @return string The text from the array |
||
653 | */ |
||
654 | protected function _fetch($array) |
||
677 | } |
||
678 | |||
679 | /** |
||
680 | * Get a specific array by path, one level down. (privately used...) |
||
681 | * |
||
682 | * @param array $array An array of data |
||
683 | * @param string $path The path |
||
684 | * @param int $level How far deep into the array we should go |
||
685 | * @param bool $no_error Whether or not to ignore errors |
||
686 | * @return string|array The specified array (or the contents of said array if there's only one result) |
||
687 | */ |
||
688 | protected function _path($array, $path, $level, $no_error = false) |
||
745 | } |
||
746 | } |
||
1229 | ?> |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..