Total Complexity | 129 |
Total Lines | 698 |
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 | * @var bool Holds trim level textual data |
||
38 | */ |
||
39 | public $trim; |
||
40 | |||
41 | /** |
||
42 | * Constructor for the xml parser. |
||
43 | * Example use: |
||
44 | * $xml = new xmlArray(file('data.xml')); |
||
45 | * |
||
46 | * @param string $data The xml data or an array of, unless is_clone is true. |
||
47 | * @param bool $auto_trim Used to automatically trim textual data. |
||
48 | * @param int $level The debug level. Specifies whether notices should be generated for missing elements and attributes. |
||
49 | * @param bool $is_clone default false. If is_clone is true, the xmlArray is cloned from another - used internally only. |
||
50 | */ |
||
51 | public function __construct($data, $auto_trim = false, $level = null, $is_clone = false) |
||
52 | { |
||
53 | // If we're using this try to get some more memory. |
||
54 | setMemoryLimit('32M'); |
||
55 | |||
56 | // Set the debug level. |
||
57 | $this->debug_level = $level !== null ? $level : error_reporting(); |
||
58 | $this->trim = $auto_trim; |
||
59 | |||
60 | // Is the data already parsed? |
||
61 | if ($is_clone) |
||
62 | { |
||
63 | $this->array = $data; |
||
|
|||
64 | return; |
||
65 | } |
||
66 | |||
67 | // Is the input an array? (ie. passed from file()?) |
||
68 | if (is_array($data)) |
||
69 | $data = implode('', $data); |
||
70 | |||
71 | // Remove any xml declaration or doctype, and parse out comments and CDATA. |
||
72 | $data = preg_replace('/<!--.*?-->/s', '', $this->_to_cdata(preg_replace(array('/^<\?xml.+?\?' . '>/is', '/<!DOCTYPE[^>]+?' . '>/s'), '', $data))); |
||
73 | |||
74 | // Now parse the xml! |
||
75 | $this->array = $this->_parse($data); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get the root element's name. |
||
80 | * Example use: |
||
81 | * echo $element->name(); |
||
82 | * @return string The root element's name |
||
83 | */ |
||
84 | public function name() |
||
85 | { |
||
86 | return isset($this->array['name']) ? $this->array['name'] : ''; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get a specified element's value or attribute by path. |
||
91 | * Children are parsed for text, but only textual data is returned |
||
92 | * unless get_elements is true. |
||
93 | * Example use: |
||
94 | * $data = $xml->fetch('html/head/title'); |
||
95 | * @param string $path The path to the element to fetch |
||
96 | * @param bool $get_elements Whether to include elements |
||
97 | * @return string The value or attribute of the specified element |
||
98 | */ |
||
99 | public function fetch($path, $get_elements = false) |
||
100 | { |
||
101 | // Get the element, in array form. |
||
102 | $array = $this->path($path); |
||
103 | |||
104 | if ($array === false) |
||
105 | return false; |
||
106 | |||
107 | // Getting elements into this is a bit complicated... |
||
108 | if ($get_elements && !is_string($array)) |
||
109 | { |
||
110 | $temp = ''; |
||
111 | |||
112 | // Use the _xml() function to get the xml data. |
||
113 | foreach ($array->array as $val) |
||
114 | { |
||
115 | // Skip the name and any attributes. |
||
116 | if (is_array($val)) |
||
117 | $temp .= $this->_xml($val, null); |
||
118 | } |
||
119 | |||
120 | // Just get the XML data and then take out the CDATAs. |
||
121 | return $this->_to_cdata($temp); |
||
122 | } |
||
123 | |||
124 | // Return the value - taking care to pick out all the text values. |
||
125 | return is_string($array) ? $array : $this->_fetch($array->array); |
||
126 | } |
||
127 | |||
128 | /** Get an element, returns a new xmlArray. |
||
129 | * It finds any elements that match the path specified. |
||
130 | * It will always return a set if there is more than one of the element |
||
131 | * or return_set is true. |
||
132 | * Example use: |
||
133 | * $element = $xml->path('html/body'); |
||
134 | * @param $path string The path to the element to get |
||
135 | * @param $return_full bool Whether to return the full result set |
||
136 | * @return xmlArray, a new xmlArray. |
||
137 | */ |
||
138 | public function path($path, $return_full = false) |
||
139 | { |
||
140 | // Split up the path. |
||
141 | $path = explode('/', $path); |
||
142 | |||
143 | // Start with a base array. |
||
144 | $array = $this->array; |
||
145 | |||
146 | // For each element in the path. |
||
147 | foreach ($path as $el) |
||
148 | { |
||
149 | // Deal with sets.... |
||
150 | if (strpos($el, '[') !== false) |
||
151 | { |
||
152 | $lvl = (int) substr($el, strpos($el, '[') + 1); |
||
153 | $el = substr($el, 0, strpos($el, '[')); |
||
154 | } |
||
155 | // Find an attribute. |
||
156 | elseif (substr($el, 0, 1) == '@') |
||
157 | { |
||
158 | // It simplifies things if the attribute is already there ;). |
||
159 | if (isset($array[$el])) |
||
160 | return $array[$el]; |
||
161 | else |
||
162 | { |
||
163 | $trace = debug_backtrace(); |
||
164 | $i = 0; |
||
165 | while ($i < count($trace) && isset($trace[$i]['class']) && $trace[$i]['class'] == get_class($this)) |
||
166 | $i++; |
||
167 | $debug = ' (from ' . $trace[$i - 1]['file'] . ' on line ' . $trace[$i - 1]['line'] . ')'; |
||
168 | |||
169 | // Cause an error. |
||
170 | if ($this->debug_level & E_NOTICE) |
||
171 | trigger_error('Undefined XML attribute: ' . substr($el, 1) . $debug, E_USER_NOTICE); |
||
172 | return false; |
||
173 | } |
||
174 | } |
||
175 | else |
||
176 | $lvl = null; |
||
177 | |||
178 | // Find this element. |
||
179 | $array = $this->_path($array, $el, $lvl); |
||
180 | } |
||
181 | |||
182 | // Clean up after $lvl, for $return_full. |
||
183 | if ($return_full && (!isset($array['name']) || substr($array['name'], -1) != ']')) |
||
184 | $array = array('name' => $el . '[]', $array); |
||
185 | |||
186 | // Create the right type of class... |
||
187 | $newClass = get_class($this); |
||
188 | |||
189 | // Return a new xmlArray for the result. |
||
190 | return $array === false ? false : new $newClass($array, $this->trim, $this->debug_level, true); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Check if an element exists. |
||
195 | * Example use, |
||
196 | * echo $xml->exists('html/body') ? 'y' : 'n'; |
||
197 | * |
||
198 | * @param string $path The path to the element to get. |
||
199 | * @return boolean Whether the specified path exists |
||
200 | */ |
||
201 | public function exists($path) |
||
202 | { |
||
203 | // Split up the path. |
||
204 | $path = explode('/', $path); |
||
205 | |||
206 | // Start with a base array. |
||
207 | $array = $this->array; |
||
208 | |||
209 | // For each element in the path. |
||
210 | foreach ($path as $el) |
||
211 | { |
||
212 | // Deal with sets.... |
||
213 | if (strpos($el, '[') !== false) |
||
214 | { |
||
215 | $lvl = (int) substr($el, strpos($el, '[') + 1); |
||
216 | $el = substr($el, 0, strpos($el, '[')); |
||
217 | } |
||
218 | // Find an attribute. |
||
219 | elseif (substr($el, 0, 1) == '@') |
||
220 | return isset($array[$el]); |
||
221 | else |
||
222 | $lvl = null; |
||
223 | |||
224 | // Find this element. |
||
225 | $array = $this->_path($array, $el, $lvl, true); |
||
226 | } |
||
227 | |||
228 | return $array !== false; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Count the number of occurrences of a path. |
||
233 | * Example use: |
||
234 | * echo $xml->count('html/head/meta'); |
||
235 | * @param string $path The path to search for. |
||
236 | * @return int The number of elements the path matches. |
||
237 | */ |
||
238 | public function count($path) |
||
239 | { |
||
240 | // Get the element, always returning a full set. |
||
241 | $temp = $this->path($path, true); |
||
242 | |||
243 | // Start at zero, then count up all the numeric keys. |
||
244 | $i = 0; |
||
245 | foreach ($temp->array as $item) |
||
246 | { |
||
247 | if (is_array($item)) |
||
248 | $i++; |
||
249 | } |
||
250 | |||
251 | return $i; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Get an array of xmlArray's matching the specified path. |
||
256 | * This differs from ->path(path, true) in that instead of an xmlArray |
||
257 | * of elements, an array of xmlArray's is returned for use with foreach. |
||
258 | * Example use: |
||
259 | * foreach ($xml->set('html/body/p') as $p) |
||
260 | * @param $path string The path to search for. |
||
261 | * @return xmlArray[] An array of xmlArray objects |
||
262 | */ |
||
263 | public function set($path) |
||
264 | { |
||
265 | // None as yet, just get the path. |
||
266 | $array = array(); |
||
267 | $xml = $this->path($path, true); |
||
268 | |||
269 | foreach ($xml->array as $val) |
||
270 | { |
||
271 | // Skip these, they aren't elements. |
||
272 | if (!is_array($val) || $val['name'] == '!') |
||
273 | continue; |
||
274 | |||
275 | // Create the right type of class... |
||
276 | $newClass = get_class($this); |
||
277 | |||
278 | // Create a new xmlArray and stick it in the array. |
||
279 | $array[] = new $newClass($val, $this->trim, $this->debug_level, true); |
||
280 | } |
||
281 | |||
282 | return $array; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Create an xml file from an xmlArray, the specified path if any. |
||
287 | * Example use: |
||
288 | * echo $this->create_xml(); |
||
289 | * @param string $path The path to the element. (optional) |
||
290 | * @return string Xml-formatted string. |
||
291 | */ |
||
292 | public function create_xml($path = null) |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Output the xml in an array form. |
||
315 | * Example use: |
||
316 | * print_r($xml->to_array()); |
||
317 | * |
||
318 | * @param string $path The path to output. |
||
319 | * @return array An array of XML data |
||
320 | */ |
||
321 | public function to_array($path = null) |
||
322 | { |
||
323 | // Are we doing a specific path? |
||
324 | if ($path !== null) |
||
325 | { |
||
326 | $path = $this->path($path); |
||
327 | |||
328 | // The path was not found |
||
329 | if ($path === false) |
||
330 | return false; |
||
331 | |||
332 | $path = $path->array; |
||
333 | } |
||
334 | // No, so just use the current array. |
||
335 | else |
||
336 | $path = $this->array; |
||
337 | |||
338 | return $this->_array($path); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Parse data into an array. (privately used...) |
||
343 | * |
||
344 | * @param string $data The data to parse |
||
345 | * @return array The parsed array |
||
346 | */ |
||
347 | protected function _parse($data) |
||
348 | { |
||
349 | // Start with an 'empty' array with no data. |
||
350 | $current = array( |
||
351 | ); |
||
352 | |||
353 | // Loop until we're out of data. |
||
354 | while ($data != '') |
||
355 | { |
||
356 | // Find and remove the next tag. |
||
357 | preg_match('/\A<([\w\-:]+)((?:\s+.+?)?)([\s]?\/)?' . '>/', $data, $match); |
||
358 | if (isset($match[0])) |
||
359 | $data = preg_replace('/' . preg_quote($match[0], '/') . '/s', '', $data, 1); |
||
360 | |||
361 | // Didn't find a tag? Keep looping.... |
||
362 | if (!isset($match[1]) || $match[1] == '') |
||
363 | { |
||
364 | // If there's no <, the rest is data. |
||
365 | if (strpos($data, '<') === false) |
||
366 | { |
||
367 | $text_value = $this->_from_cdata($data); |
||
368 | $data = ''; |
||
369 | |||
370 | if ($text_value != '') |
||
371 | $current[] = array( |
||
372 | 'name' => '!', |
||
373 | 'value' => $text_value |
||
374 | ); |
||
375 | } |
||
376 | // If the < isn't immediately next to the current position... more data. |
||
377 | elseif (strpos($data, '<') > 0) |
||
378 | { |
||
379 | $text_value = $this->_from_cdata(substr($data, 0, strpos($data, '<'))); |
||
380 | $data = substr($data, strpos($data, '<')); |
||
381 | |||
382 | if ($text_value != '') |
||
383 | $current[] = array( |
||
384 | 'name' => '!', |
||
385 | 'value' => $text_value |
||
386 | ); |
||
387 | } |
||
388 | // If we're looking at a </something> with no start, kill it. |
||
389 | elseif (strpos($data, '<') !== false && strpos($data, '<') == 0) |
||
390 | { |
||
391 | if (strpos($data, '<', 1) !== false) |
||
392 | { |
||
393 | $text_value = $this->_from_cdata(substr($data, 0, strpos($data, '<', 1))); |
||
394 | $data = substr($data, strpos($data, '<', 1)); |
||
395 | |||
396 | if ($text_value != '') |
||
397 | $current[] = array( |
||
398 | 'name' => '!', |
||
399 | 'value' => $text_value |
||
400 | ); |
||
401 | } |
||
402 | else |
||
403 | { |
||
404 | $text_value = $this->_from_cdata($data); |
||
405 | $data = ''; |
||
406 | |||
407 | if ($text_value != '') |
||
408 | $current[] = array( |
||
409 | 'name' => '!', |
||
410 | 'value' => $text_value |
||
411 | ); |
||
412 | } |
||
413 | } |
||
414 | |||
415 | // Wait for an actual occurance of an element. |
||
416 | continue; |
||
417 | } |
||
418 | |||
419 | // Create a new element in the array. |
||
420 | $el = &$current[]; |
||
421 | $el['name'] = $match[1]; |
||
422 | |||
423 | // If this ISN'T empty, remove the close tag and parse the inner data. |
||
424 | if ((!isset($match[3]) || trim($match[3]) != '/') && (!isset($match[2]) || trim($match[2]) != '/')) |
||
425 | { |
||
426 | // Because PHP 5.2.0+ seems to croak using regex, we'll have to do this the less fun way. |
||
427 | $last_tag_end = strpos($data, '</' . $match[1] . '>'); |
||
428 | if ($last_tag_end === false) |
||
429 | continue; |
||
430 | |||
431 | $offset = 0; |
||
432 | while (1 == 1) |
||
433 | { |
||
434 | // Where is the next start tag? |
||
435 | $next_tag_start = strpos($data, '<' . $match[1], $offset); |
||
436 | // If the next start tag is after the last end tag then we've found the right close. |
||
437 | if ($next_tag_start === false || $next_tag_start > $last_tag_end) |
||
438 | break; |
||
439 | |||
440 | // If not then find the next ending tag. |
||
441 | $next_tag_end = strpos($data, '</' . $match[1] . '>', $offset); |
||
442 | |||
443 | // Didn't find one? Then just use the last and sod it. |
||
444 | if ($next_tag_end === false) |
||
445 | break; |
||
446 | else |
||
447 | { |
||
448 | $last_tag_end = $next_tag_end; |
||
449 | $offset = $next_tag_start + 1; |
||
450 | } |
||
451 | } |
||
452 | // Parse the insides. |
||
453 | $inner_match = substr($data, 0, $last_tag_end); |
||
454 | // Data now starts from where this section ends. |
||
455 | $data = substr($data, $last_tag_end + strlen('</' . $match[1] . '>')); |
||
456 | |||
457 | if (!empty($inner_match)) |
||
458 | { |
||
459 | // Parse the inner data. |
||
460 | if (strpos($inner_match, '<') !== false) |
||
461 | $el += $this->_parse($inner_match); |
||
462 | elseif (trim($inner_match) != '') |
||
463 | { |
||
464 | $text_value = $this->_from_cdata($inner_match); |
||
465 | if ($text_value != '') |
||
466 | $el[] = array( |
||
467 | 'name' => '!', |
||
468 | 'value' => $text_value |
||
469 | ); |
||
470 | } |
||
471 | } |
||
472 | } |
||
473 | |||
474 | // If we're dealing with attributes as well, parse them out. |
||
475 | if (isset($match[2]) && $match[2] != '') |
||
476 | { |
||
477 | // Find all the attribute pairs in the string. |
||
478 | preg_match_all('/([\w:]+)="(.+?)"/', $match[2], $attr, PREG_SET_ORDER); |
||
479 | |||
480 | // Set them as @attribute-name. |
||
481 | foreach ($attr as $match_attr) |
||
482 | $el['@' . $match_attr[1]] = $match_attr[2]; |
||
483 | } |
||
484 | } |
||
485 | |||
486 | // Return the parsed array. |
||
487 | return $current; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Get a specific element's xml. (privately used...) |
||
492 | * |
||
493 | * @param array $array An array of element data |
||
494 | * @param null|int $indent How many levels to indent the elements (null = no indent) |
||
495 | * @return string The formatted XML |
||
496 | */ |
||
497 | protected function _xml($array, $indent) |
||
498 | { |
||
499 | $indentation = $indent !== null ? ' |
||
500 | ' . str_repeat(' ', $indent) : ''; |
||
501 | |||
502 | // This is a set of elements, with no name... |
||
503 | if (is_array($array) && !isset($array['name'])) |
||
504 | { |
||
505 | $temp = ''; |
||
506 | foreach ($array as $val) |
||
507 | $temp .= $this->_xml($val, $indent); |
||
508 | return $temp; |
||
509 | } |
||
510 | |||
511 | // This is just text! |
||
512 | if ($array['name'] == '!') |
||
513 | return $indentation . '<![CDATA[' . $array['value'] . ']]>'; |
||
514 | elseif (substr($array['name'], -2) == '[]') |
||
515 | $array['name'] = substr($array['name'], 0, -2); |
||
516 | |||
517 | // Start the element. |
||
518 | $output = $indentation . '<' . $array['name']; |
||
519 | |||
520 | $inside_elements = false; |
||
521 | $output_el = ''; |
||
522 | |||
523 | // Run through and recursively output all the elements or attrbutes inside this. |
||
524 | foreach ($array as $k => $v) |
||
525 | { |
||
526 | if (substr($k, 0, 1) == '@') |
||
527 | $output .= ' ' . substr($k, 1) . '="' . $v . '"'; |
||
528 | elseif (is_array($v)) |
||
529 | { |
||
530 | $output_el .= $this->_xml($v, $indent === null ? null : $indent + 1); |
||
531 | $inside_elements = true; |
||
532 | } |
||
533 | } |
||
534 | |||
535 | // Indent, if necessary.... then close the tag. |
||
536 | if ($inside_elements) |
||
537 | $output .= '>' . $output_el . $indentation . '</' . $array['name'] . '>'; |
||
538 | else |
||
539 | $output .= ' />'; |
||
540 | |||
541 | return $output; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Return an element as an array |
||
546 | * |
||
547 | * @param array $array An array of data |
||
548 | * @return string|array A string with the element's value or an array of element data |
||
549 | */ |
||
550 | protected function _array($array) |
||
551 | { |
||
552 | $return = array(); |
||
553 | $text = ''; |
||
554 | foreach ($array as $value) |
||
555 | { |
||
556 | if (!is_array($value) || !isset($value['name'])) |
||
557 | continue; |
||
558 | |||
559 | if ($value['name'] == '!') |
||
560 | $text .= $value['value']; |
||
561 | else |
||
562 | $return[$value['name']] = $this->_array($value); |
||
563 | } |
||
564 | |||
565 | if (empty($return)) |
||
566 | return $text; |
||
567 | else |
||
568 | return $return; |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Parse out CDATA tags. (htmlspecialchars them...) |
||
573 | * |
||
574 | * @param string $data The data with CDATA tags included |
||
575 | * @return string The data contained within CDATA tags |
||
576 | */ |
||
577 | function _to_cdata($data) |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Turn the CDATAs back to normal text. |
||
611 | * |
||
612 | * @param string $data The data with CDATA tags |
||
613 | * @return string The transformed data |
||
614 | */ |
||
615 | protected function _from_cdata($data) |
||
616 | { |
||
617 | // Get the HTML translation table and reverse it. |
||
618 | $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)); |
||
619 | |||
620 | // Translate all the entities out. |
||
621 | $data = strtr(preg_replace_callback('~&#(\d{1,4});~', function($m) |
||
622 | { |
||
623 | return chr("$m[1]"); |
||
624 | }, $data), $trans_tbl); |
||
625 | |||
626 | return $this->trim ? trim($data) : $data; |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * Given an array, return the text from that array. (recursive and privately used.) |
||
631 | * |
||
632 | * @param array $array An aray of data |
||
633 | * @return string The text from the array |
||
634 | */ |
||
635 | protected function _fetch($array) |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * Get a specific array by path, one level down. (privately used...) |
||
662 | * |
||
663 | * @param array $array An array of data |
||
664 | * @param string $path The path |
||
665 | * @param int $level How far deep into the array we should go |
||
666 | * @param bool $no_error Whether or not to ignore errors |
||
667 | * @return string|array The specified array (or the contents of said array if there's only one result) |
||
668 | */ |
||
669 | protected function _path($array, $path, $level, $no_error = false) |
||
721 | } |
||
722 | } |
||
723 | |||
724 | /** |
||
725 | * Class ftp_connection |
||
1205 | ?> |
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..