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