Complex classes like PodsMigrate 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PodsMigrate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class PodsMigrate { |
||
7 | |||
8 | /** |
||
9 | * @var null|string |
||
10 | */ |
||
11 | public $type = 'php'; |
||
12 | |||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | public $types = array( 'php', 'json', 'sv', 'xml' ); |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | public $mimes = array( |
||
22 | 'json' => 'application/json', |
||
23 | 'csv' => 'text/csv', |
||
24 | 'tsv' => 'text/tsv', |
||
25 | 'xml' => 'text/xml', |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * @var null|string |
||
30 | */ |
||
31 | public $delimiter = ','; |
||
32 | |||
33 | /** |
||
34 | * @var null |
||
35 | */ |
||
36 | public $data = array( |
||
37 | 'items' => array(), |
||
38 | 'columns' => array(), |
||
39 | 'fields' => array(), |
||
40 | 'single' => false, |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * @var null |
||
45 | */ |
||
46 | public $input; |
||
47 | |||
48 | /** |
||
49 | * @var |
||
50 | */ |
||
51 | public $parsed; |
||
52 | |||
53 | /** |
||
54 | * @var |
||
55 | */ |
||
56 | public $built; |
||
57 | |||
58 | /** |
||
59 | * Migrate Data to and from Pods |
||
60 | * |
||
61 | * @param string $type Export Type (php, json, sv, xml) |
||
|
|||
62 | * @param string $delimiter Delimiter for export type 'sv' |
||
63 | * @param array $data Array of data settings |
||
64 | * |
||
65 | * @return \PodsMigrate |
||
66 | * |
||
67 | * @license http://www.gnu.org/licenses/gpl-2.0.html |
||
68 | * @since 2.0 |
||
69 | */ |
||
70 | public function __construct( $type = null, $delimiter = null, $data = null ) { |
||
100 | |||
101 | /** |
||
102 | * @param $data |
||
103 | */ |
||
104 | public function set_data( $data ) { |
||
114 | |||
115 | /** |
||
116 | * Importing / Parsing / Validating Code |
||
117 | * |
||
118 | * @param array $data Array of data |
||
119 | * @param string $type Export Type (php, json, sv, xml) |
||
120 | * @param string $delimiter Delimiter for export type 'sv' |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function import( $data = null, $type = null, $delimiter = null ) { |
||
131 | |||
132 | /** |
||
133 | * @param array $data Array of data |
||
134 | * @param string $type Export Type (php, json, sv, xml) |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function import_pod_items( $data = null, $type = null ) { |
||
150 | |||
151 | /** |
||
152 | * @param array $data Array of data |
||
153 | * @param string $type Parse Type (php, json, sv, xml) |
||
154 | * @param string $delimiter Delimiter for export type 'sv' |
||
155 | * |
||
156 | * @return null |
||
157 | */ |
||
158 | public function parse( $data = null, $type = null, $delimiter = null ) { |
||
159 | |||
160 | if ( ! empty( $data ) ) { |
||
161 | $this->input = $data; |
||
162 | } |
||
163 | |||
164 | if ( ! empty( $type ) && in_array( $type, $this->types, true ) ) { |
||
165 | $this->type = $type; |
||
166 | } |
||
167 | |||
168 | if ( !empty( $delimiter ) ) |
||
169 | $this->delimiter = $delimiter; |
||
170 | |||
171 | if ( method_exists( $this, "parse_{$this->type}" ) ) { |
||
172 | return call_user_func( array( $this, 'parse_' . $this->type ) ); |
||
173 | } |
||
174 | |||
175 | return $this->parsed; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param array $data Array of data |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function parse_json( $data = null ) { |
||
184 | |||
185 | if ( ! empty( $data ) ) { |
||
186 | $this->input = $data; |
||
187 | } |
||
188 | |||
189 | $items = @json_decode( $this->input, true ); |
||
190 | |||
191 | if ( ! is_array( $items ) ) { |
||
192 | return false; |
||
193 | } |
||
194 | |||
195 | // Only export to a basic object if building for a single item. |
||
196 | if ( ! empty( $this->data['single'] ) ) { |
||
197 | $data = $items; |
||
198 | } else { |
||
199 | $data = array( |
||
200 | 'columns' => array(), |
||
201 | 'items' => array(), |
||
202 | 'fields' => array(), |
||
203 | ); |
||
204 | |||
205 | foreach ( $items as $key => $item ) { |
||
206 | if ( ! is_array( $item ) ) { |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | foreach ( $item as $column => $value ) { |
||
211 | if ( ! in_array( $column, $data['columns'], true ) ) { |
||
212 | $data['columns'][] = $column; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | $data['items'][ $key ] = $item; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | $this->parsed = $data; |
||
221 | |||
222 | return $this->parsed; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param array $data Array of data |
||
227 | * @param string $delimiter Delimiter for export type 'sv' |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function parse_sv( $data = null, $delimiter = null ) { |
||
274 | |||
275 | /** |
||
276 | * Handle str_getcsv for cases where it's not set |
||
277 | * |
||
278 | * @param $line |
||
279 | * @param string $delimiter |
||
280 | * @param string $enclosure |
||
281 | * @param string $escape |
||
282 | * |
||
283 | * @return array|mixed |
||
284 | */ |
||
285 | public function str_getcsv( $line, $delimiter = ',', $enclosure = '"', $escape = '\\' ) { |
||
307 | |||
308 | /** |
||
309 | * @param array $data Array of data |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function parse_xml( $data = null ) { |
||
314 | |||
315 | if ( ! empty( $data ) ) { |
||
316 | $this->input = $data; |
||
317 | } |
||
318 | |||
319 | $xml = new SimpleXMLElement( $this->input ); |
||
320 | |||
321 | if ( ! isset( $xml->items ) ) { |
||
322 | return false; |
||
323 | } |
||
324 | |||
325 | $data = array( |
||
326 | 'columns' => array(), |
||
327 | 'items' => array(), |
||
328 | ); |
||
329 | |||
330 | /** |
||
331 | * @var $child SimpleXMLElement |
||
332 | * @var $item_child SimpleXMLElement |
||
333 | * @var $data_child SimpleXMLElement |
||
334 | */ |
||
335 | |||
336 | if ( isset( $xml->columns ) ) { |
||
337 | foreach ( $xml->columns->children() as $child ) { |
||
338 | $sub = $child->getName(); |
||
339 | |||
340 | if ( empty( $sub ) || 'column' !== $sub ) { |
||
341 | continue; |
||
342 | } |
||
343 | |||
344 | if ( isset( $child->name ) ) { |
||
345 | if ( is_array( $child->name ) ) { |
||
346 | $column = $child->name[0]; |
||
347 | } else { |
||
348 | $column = $child->name; |
||
349 | } |
||
350 | |||
351 | $data['columns'][] = $column; |
||
352 | } |
||
353 | } |
||
354 | } |
||
355 | |||
356 | foreach ( $xml->items->children() as $child ) { |
||
357 | $sub = $child->getName(); |
||
358 | |||
359 | if ( empty( $sub ) || 'item' !== $sub ) { |
||
360 | continue; |
||
361 | } |
||
362 | |||
363 | $item = array(); |
||
364 | |||
365 | $attributes = $child->attributes(); |
||
366 | |||
367 | if ( ! empty( $attributes ) ) { |
||
368 | foreach ( $attributes as $column => $value ) { |
||
369 | if ( ! in_array( $column, $data['columns'], true ) ) { |
||
370 | $data['columns'][] = $column; |
||
371 | } |
||
372 | |||
373 | $item[ $column ] = $value; |
||
374 | } |
||
375 | } |
||
376 | |||
377 | $item_child = $child->children(); |
||
378 | |||
379 | if ( ! empty( $item_child ) ) { |
||
380 | foreach ( $item_child->children() as $data_child ) { |
||
381 | $column = $data_child->getName(); |
||
382 | |||
383 | if ( ! in_array( $column, $data['columns'], true ) ) { |
||
384 | $data['columns'][] = $column; |
||
385 | } |
||
386 | |||
387 | $item[ $column ] = $item_child->$column; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | if ( ! empty( $item ) ) { |
||
392 | $data['items'][] = $item; |
||
393 | } |
||
394 | }//end foreach |
||
395 | |||
396 | $this->parsed = $data; |
||
397 | |||
398 | return $this->parsed; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @param array $data Array of data |
||
403 | * |
||
404 | * @return mixed |
||
405 | * |
||
406 | * @todo For much much later |
||
407 | */ |
||
408 | public function parse_sql( $data = null ) { |
||
418 | |||
419 | /** |
||
420 | * Exporting / Building Code |
||
421 | * |
||
422 | * @param array $data Array of data |
||
423 | * @param string $type Export Type (php, json, sv, xml) |
||
424 | * @param string $delimiter Delimiter for export type 'sv' |
||
425 | * |
||
426 | * @return mixed |
||
427 | */ |
||
428 | public function export( $data = null, $type = null, $delimiter = null ) { |
||
429 | |||
430 | if ( ! empty( $data ) ) { |
||
431 | $this->set_data( $data ); |
||
432 | } |
||
433 | |||
434 | if ( ! empty( $type ) && in_array( $type, $this->types, true ) ) { |
||
435 | $this->type = $type; |
||
436 | } |
||
437 | |||
438 | if ( ! empty( $delimiter ) ) { |
||
439 | $this->delimiter = $delimiter; |
||
440 | } |
||
441 | |||
442 | if ( method_exists( $this, "build_{$this->type}" ) ) { |
||
443 | call_user_func( array( $this, 'build_' . $this->type ) ); |
||
444 | } |
||
445 | |||
446 | return $this->built; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @param array $data Array of data |
||
451 | */ |
||
452 | public function export_pod_items( $data = null ) { |
||
458 | |||
459 | /** |
||
460 | * @param array $data Array of data |
||
461 | * @param string $type Export Type (php, json, sv, xml) |
||
462 | * |
||
463 | * @return null |
||
464 | */ |
||
465 | public function build( $data = null, $type = null ) { |
||
481 | |||
482 | /** |
||
483 | * @param array $data Array of data |
||
484 | * |
||
485 | * @return bool |
||
486 | */ |
||
487 | public function build_json( $data = null ) { |
||
488 | |||
489 | if ( ! empty( $data ) ) { |
||
490 | $this->set_data( $data ); |
||
491 | } |
||
492 | |||
493 | if ( empty( $this->data ) || ! is_array( $this->data ) ) { |
||
494 | return false; |
||
495 | } |
||
496 | |||
497 | // Only export to a basic object if building for a single item. |
||
498 | if ( ! empty( $this->data['single'] ) ) { |
||
499 | $data = $this->data['items']; |
||
500 | } else { |
||
501 | $data = array( |
||
502 | 'items' => array( |
||
503 | 'count' => count( $this->data['items'] ), |
||
504 | 'item' => array(), |
||
505 | ), |
||
506 | ); |
||
507 | |||
508 | foreach ( $this->data['items'] as $item ) { |
||
509 | $row = array(); |
||
510 | |||
511 | foreach ( $this->data['columns'] as $column => $label ) { |
||
512 | if ( is_numeric( $column ) && ( ( is_object( $item ) && ! isset( $item->$column ) ) || ( is_array( $item ) && ! isset( $item[ $column ] ) ) ) ) { |
||
513 | $column = $label; |
||
514 | } |
||
515 | |||
516 | $value = ''; |
||
517 | |||
518 | if ( is_object( $item ) ) { |
||
519 | if ( ! isset( $item->$column ) ) { |
||
520 | $item->$column = ''; |
||
521 | } |
||
522 | |||
523 | $value = $item->$column; |
||
524 | } elseif ( is_array( $item ) ) { |
||
525 | if ( ! isset( $item[ $column ] ) ) { |
||
526 | $item[ $column ] = ''; |
||
527 | } |
||
528 | |||
529 | $value = $item[ $column ]; |
||
530 | } |
||
531 | |||
532 | $row[ $column ] = $value; |
||
533 | }//end foreach |
||
534 | |||
535 | $data['items']['item'][] = $row; |
||
536 | }//end foreach |
||
537 | } |
||
538 | |||
539 | $this->built = @json_encode( $data ); |
||
540 | |||
541 | return $this->built; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * @param array $data Array of data |
||
546 | * @param string $delimiter Delimiter for export type 'sv' |
||
547 | * |
||
548 | * @return bool |
||
549 | */ |
||
550 | public function build_sv( $data = null, $delimiter = null ) { |
||
551 | |||
552 | if ( ! empty( $data ) ) { |
||
553 | $this->set_data( $data ); |
||
554 | } |
||
555 | |||
556 | if ( ! empty( $delimiter ) ) { |
||
557 | $this->delimiter = $delimiter; |
||
558 | } |
||
559 | |||
560 | if ( empty( $this->data ) || ! is_array( $this->data ) ) { |
||
561 | return false; |
||
562 | } |
||
563 | |||
564 | $head = ''; |
||
565 | $lines = ''; |
||
566 | |||
567 | foreach ( $this->data['columns'] as $column => $label ) { |
||
568 | $head .= '"' . $label . '"' . $this->delimiter; |
||
569 | } |
||
570 | |||
571 | $head = substr( $head, 0, - 1 ); |
||
572 | |||
573 | foreach ( $this->data['items'] as $item ) { |
||
574 | $line = ''; |
||
575 | |||
576 | foreach ( $this->data['columns'] as $column => $label ) { |
||
577 | if ( is_numeric( $column ) && ( ( is_object( $item ) && ! isset( $item->$column ) ) || ( is_array( $item ) && ! isset( $item[ $column ] ) ) ) ) { |
||
578 | $column = $label; |
||
579 | } |
||
580 | |||
581 | $value = ''; |
||
582 | |||
583 | if ( is_object( $item ) ) { |
||
584 | if ( ! isset( $item->$column ) ) { |
||
585 | $item->$column = ''; |
||
586 | } |
||
587 | |||
588 | $value = $item->$column; |
||
589 | } elseif ( is_array( $item ) ) { |
||
590 | if ( ! isset( $item[ $column ] ) ) { |
||
591 | $item[ $column ] = ''; |
||
592 | } |
||
593 | |||
594 | $value = $item[ $column ]; |
||
595 | } |
||
596 | |||
597 | if ( is_array( $value ) || is_object( $value ) ) { |
||
598 | $value = pods_serial_comma( |
||
599 | $value, array( |
||
600 | 'field' => $column, |
||
601 | 'fields' => pods_var_raw( $column, $this->data['fields'] ), |
||
602 | 'and' => '', |
||
603 | ) |
||
604 | ); |
||
605 | } |
||
606 | |||
607 | $value = str_replace( array( '"', "\r\n", "\r", "\n" ), array( '\\"', "\n", "\n", '\n' ), $value ); |
||
608 | |||
609 | $line .= '"' . $value . '"' . $this->delimiter; |
||
610 | }//end foreach |
||
611 | |||
612 | $lines .= substr( $line, 0, - 1 ) . "\n"; |
||
613 | }//end foreach |
||
614 | |||
615 | if ( ! empty( $lines ) ) { |
||
616 | $lines = "\n" . substr( $lines, 0, - 1 ); |
||
617 | } |
||
618 | |||
619 | $this->built = $head . $lines; |
||
620 | |||
621 | return $this->built; |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * @param array $data Array of data |
||
626 | * |
||
627 | * @return bool |
||
628 | */ |
||
629 | public function build_xml( $data = null ) { |
||
630 | |||
631 | if ( ! empty( $data ) ) { |
||
632 | $this->set_data( $data ); |
||
633 | } |
||
634 | |||
635 | if ( empty( $this->data ) || ! is_array( $this->data ) ) { |
||
636 | return false; |
||
637 | } |
||
638 | |||
639 | $head = '<' . '?' . 'xml version="1.0" encoding="utf-8" ' . '?' . '>' . "\r\n<items count=\"" . count( $this->data['items'] ) . "\">\r\n"; |
||
640 | $lines = ''; |
||
641 | |||
642 | foreach ( $this->data['items'] as $item ) { |
||
643 | $line = "\t<item>\r\n"; |
||
644 | |||
645 | foreach ( $this->data['columns'] as $column => $label ) { |
||
646 | if ( is_numeric( $column ) && ( ( is_object( $item ) && ! isset( $item->$column ) ) || ( is_array( $item ) && ! isset( $item[ $column ] ) ) ) ) { |
||
647 | $column = $label; |
||
648 | } |
||
649 | |||
650 | $line .= $this->build_xml_level( $item, $column ); |
||
651 | } |
||
652 | |||
653 | $line .= "\t</item>\r\n"; |
||
654 | $lines .= $line; |
||
655 | } |
||
656 | |||
657 | $foot = '</items>'; |
||
658 | |||
659 | $this->built = $head . $lines . $foot; |
||
660 | |||
661 | return $this->built; |
||
662 | } |
||
663 | |||
664 | /** |
||
665 | * @param array|object $item |
||
666 | * @param string $column |
||
667 | * @param int $level |
||
668 | * @param string $column_name |
||
669 | * |
||
670 | * @return string |
||
671 | */ |
||
672 | public function build_xml_level( $item, $column, $level = 2, $column_name = '' ) { |
||
673 | |||
674 | $column = pods_clean_name( $column, false, false ); |
||
675 | |||
676 | $line = ''; |
||
677 | |||
678 | $value = ''; |
||
679 | |||
680 | if ( is_object( $item ) ) { |
||
681 | if ( ! isset( $item->$column ) ) { |
||
682 | $item->$column = ''; |
||
683 | } |
||
684 | |||
685 | $value = $item->$column; |
||
686 | } elseif ( is_array( $item ) ) { |
||
687 | if ( ! isset( $item[ $column ] ) ) { |
||
688 | $item[ $column ] = ''; |
||
689 | } |
||
690 | |||
691 | $value = $item[ $column ]; |
||
692 | } |
||
693 | |||
694 | if ( ! empty( $column_name ) ) { |
||
695 | $column = $column_name; |
||
696 | } |
||
697 | |||
698 | $tabs = str_repeat( "\t", $level ); |
||
699 | |||
700 | $line .= $tabs . "<{$column}>"; |
||
701 | |||
702 | if ( is_array( $value ) || is_object( $value ) ) { |
||
703 | if ( is_object( $value ) ) { |
||
704 | $value = get_object_vars( $value ); |
||
705 | } |
||
706 | |||
707 | foreach ( $value as $k => $v ) { |
||
708 | if ( is_int( $k ) ) { |
||
709 | $line .= $this->build_xml_level( $value, $k, $level + 1, 'value' ); |
||
710 | } else { |
||
711 | $line .= $this->build_xml_level( $value, $k, $level + 1 ); |
||
712 | } |
||
713 | } |
||
714 | } elseif ( false !== strpos( $value, '<' ) ) { |
||
715 | $value = str_replace( array( '<![CDATA[', ']]>' ), array( '<![CDATA[', ']]>' ), $value ); |
||
716 | |||
717 | $line .= '<![CDATA[' . $value . ']]>'; |
||
718 | } else { |
||
719 | $line .= str_replace( '&', '&', $value ); |
||
720 | } |
||
721 | |||
722 | $line .= "</{$column}>\r\n"; |
||
723 | |||
724 | return $line; |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * @param array $data Array of data |
||
729 | * |
||
730 | * @return mixed |
||
731 | */ |
||
732 | public function build_sql( $data = null ) { |
||
742 | |||
743 | /** |
||
744 | * Save export to a file. |
||
745 | * |
||
746 | * @param array $params Additional options for saving. |
||
747 | * |
||
748 | * @return string The URL of the saved file, a path if not attached. |
||
749 | */ |
||
750 | public function save( $params = array() ) { |
||
851 | |||
852 | /* |
||
853 | * The real enchilada! |
||
854 | */ |
||
855 | /* |
||
856 | EXAMPLES |
||
857 | //// minimal import (if your fields match on both your pods and tables) |
||
858 | $import = array('my_pod' => array('table' => 'my_table')); // if your table name doesn't match the pod name |
||
859 | $import = array('my_pod'); // if your table name matches your pod name |
||
860 | |||
861 | //// advanced import |
||
862 | $import = array(); |
||
863 | $import['my_pod'] = array(); |
||
864 | $import['my_pod']['fields']['this_field'] = 'field_name_in_table'; // if the field name doesn't match on table and pod |
||
865 | $import['my_pod']['fields'][] = 'that_field'; // if the field name matches on table and pod |
||
866 | $import['my_pod']['fields']['this_other_field'] = array('filter' => 'wpautop'); // if you want the value to be different than is provided, set a filter function to use [filter uses = filter_name($value,$rowdata)] |
||
867 | $import['my_pod']['fields']['another_field'] = array('field' => 'the_real_field_in_table','filter' => 'my_custom_function'); // if you want the value to be filtered, and the field name doesn't match on the table and pod |
||
868 | $import[] = 'my_other_pod'; // if your table name matches your pod name |
||
869 | $import['another_pod'] = array('update_on' => 'main_field'); // you can update a pod item if the value of this field is the same on both tables |
||
870 | $import['another_pod'] = array('reset' => true); // you can choose to reset all data in a pod before importing |
||
871 | |||
872 | //// run import |
||
873 | pods_import_it($import); |
||
874 | */ |
||
875 | /** |
||
876 | * @param $import |
||
877 | * @param bool $output |
||
878 | */ |
||
879 | public function heres_the_beef( $import, $output = true ) { |
||
1233 | |||
1234 | /** |
||
1235 | * Export data to a file. |
||
1236 | * |
||
1237 | * @param string $file File to export to. |
||
1238 | * @param array $data Data to export. |
||
1239 | * @param bool $single Whether this is a single item export. |
||
1240 | * |
||
1241 | * @return mixed |
||
1242 | */ |
||
1243 | public static function export_data_to_file( $file, $data, $single = false ) { |
||
1280 | |||
1281 | /** |
||
1282 | * Get data from a file. |
||
1283 | * |
||
1284 | * @param string $file File to get data from. |
||
1285 | * @param bool $single Whether this is a single item. |
||
1286 | * |
||
1287 | * @return mixed |
||
1288 | */ |
||
1289 | public static function get_data_from_file( $file, $single = false ) { |
||
1321 | |||
1322 | } |
||
1323 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.