Complex classes like Dynamic_Featured_Image 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 Dynamic_Featured_Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Dynamic_Featured_Image { |
||
47 | /** |
||
48 | * Current version of the plugin. |
||
49 | * |
||
50 | * @since 3.0.0 |
||
51 | */ |
||
52 | const VERSION = '3.6.5'; |
||
53 | |||
54 | /** |
||
55 | * Text domain. |
||
56 | * |
||
57 | * @since 3.6.0 |
||
58 | */ |
||
59 | const TEXT_DOMAIN = 'dynamic-featured-image'; |
||
60 | |||
61 | /** |
||
62 | * Documentation Link. |
||
63 | * |
||
64 | * @since 3.6.0 |
||
65 | */ |
||
66 | const WIKI_LINK = 'https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/'; |
||
67 | |||
68 | /** |
||
69 | * Upgrade Link. |
||
70 | * |
||
71 | * @since 3.6.0 |
||
72 | */ |
||
73 | const UPGRADE_LINK = 'https://ankitpokhrel.com/explore/dynamic-featured-image-pro/'; |
||
74 | |||
75 | /** |
||
76 | * Image upload directory. |
||
77 | * |
||
78 | * @var $upload_dir string |
||
79 | */ |
||
80 | private $upload_dir; |
||
81 | |||
82 | /** |
||
83 | * Image upload URL. |
||
84 | * |
||
85 | * @var $upload_url string |
||
86 | */ |
||
87 | private $upload_url; |
||
88 | |||
89 | /** |
||
90 | * Database object. |
||
91 | * |
||
92 | * @var $db wpdb |
||
93 | */ |
||
94 | private $db; |
||
95 | |||
96 | /** |
||
97 | * Title for dfi metabox. |
||
98 | * |
||
99 | * @var $metabox_title string |
||
100 | */ |
||
101 | protected $metabox_title; |
||
102 | |||
103 | /** |
||
104 | * Users post type filter for dfi metabox. |
||
105 | * |
||
106 | * @var $user_filter array |
||
107 | */ |
||
108 | protected $user_filter; |
||
109 | |||
110 | /** |
||
111 | * Constructor. Hooks all interactions to initialize the class. |
||
112 | * |
||
113 | * @since 1.0.0 |
||
114 | * @access public |
||
115 | * @global object $wpdb |
||
116 | * |
||
117 | * @see add_action() |
||
118 | */ |
||
119 | 1 | public function __construct() { |
|
158 | |||
159 | public function dismiss_admin_notice() |
||
171 | |||
172 | public function admin_notice() |
||
232 | |||
233 | public function is_plugin_installed() |
||
239 | |||
240 | public function is_plugin_active() |
||
244 | |||
245 | public function notice_css() |
||
299 | |||
300 | /** |
||
301 | * Return site protocol. |
||
302 | * |
||
303 | * @since 3.5.1 |
||
304 | * @access public |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | private function get_protocol() { |
||
311 | |||
312 | /** |
||
313 | * Add required admin scripts. |
||
314 | * |
||
315 | * @since 1.0.0 |
||
316 | * @access public |
||
317 | * |
||
318 | * @see wp_enqueue_style() |
||
319 | * @see wp_register_script() |
||
320 | * @see wp_enqueue_script() |
||
321 | * |
||
322 | * @return void |
||
323 | */ |
||
324 | 1 | public function enqueue_admin_scripts() { |
|
347 | |||
348 | /** |
||
349 | * Add upgrade link. |
||
350 | * |
||
351 | * @access public |
||
352 | * @since 3.5.1 |
||
353 | * @action plugin_action_links |
||
354 | * |
||
355 | * @codeCoverageIgnore |
||
356 | * |
||
357 | * @param array $links Action links. |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | public function dfi_action_links( $links ) { |
||
368 | |||
369 | /** |
||
370 | * Add featured meta boxes dynamically. |
||
371 | * |
||
372 | * @since 1.0.0 |
||
373 | * @access public |
||
374 | * @global object $post |
||
375 | * |
||
376 | * @see get_post_meta() |
||
377 | * @see get_post_types() |
||
378 | * @see add_meta_box() |
||
379 | * @see add_filter() |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | public function initialize_featured_box() { |
||
407 | |||
408 | /** |
||
409 | * Translates more than one digit number digit by digit. |
||
410 | * |
||
411 | * @param int $number Integer to be translated. |
||
412 | * |
||
413 | * @return string Translated number |
||
414 | */ |
||
415 | 2 | protected function get_number_translation( $number ) { |
|
428 | |||
429 | /** |
||
430 | * Adds meta boxes. |
||
431 | * |
||
432 | * @param array $post_types Post types to show featured image box. |
||
433 | * @param object $featured Callback arguments. |
||
434 | * @param int $i Index of the featured image. |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | private function dfi_add_meta_box( $post_types, $featured = null, $i = null ) { |
||
469 | |||
470 | /** |
||
471 | * Separate thumb and full image url from given URL string. |
||
472 | * |
||
473 | * @since 3.3.1 |
||
474 | * |
||
475 | * @param string $url_string Url string. |
||
476 | * @param string $state Thumb or full. |
||
477 | * |
||
478 | * @return string|null |
||
479 | */ |
||
480 | 3 | private function separate( $url_string, $state = 'thumb' ) { |
|
489 | |||
490 | /** |
||
491 | * Create a nonce field. |
||
492 | * |
||
493 | * @since 3.5.0 |
||
494 | * |
||
495 | * @see wp_nonce_field() |
||
496 | * @see plugin_basename() |
||
497 | * |
||
498 | * @codeCoverageIgnore |
||
499 | * |
||
500 | * @param string $key Nonce key. |
||
501 | * |
||
502 | * @return string |
||
503 | */ |
||
504 | protected function nonce_field( $key ) { |
||
507 | |||
508 | /** |
||
509 | * Featured meta box as seen in the admin. |
||
510 | * |
||
511 | * @since 1.0.0 |
||
512 | * @access public |
||
513 | * |
||
514 | * @param object $post Global post object. |
||
515 | * @param array $featured Array containing featured image count. |
||
516 | * |
||
517 | * @throws Exception Medium size image not found. |
||
518 | * @return void |
||
519 | */ |
||
520 | 2 | public function featured_meta_box( $post, $featured ) { |
|
542 | |||
543 | /** |
||
544 | * Returns featured box html content. |
||
545 | * |
||
546 | * @since 3.1.0 |
||
547 | * @access private |
||
548 | * |
||
549 | * @param string $featured_img_trimmed Medium sized image. |
||
550 | * @param string $featured_img Full sized image. |
||
551 | * @param string $featured_id Attachment Id. |
||
552 | * @param string $thumbnail Thumb sized image. |
||
553 | * @param int $post_id Post id. |
||
554 | * |
||
555 | * @return string Html content |
||
556 | */ |
||
557 | 2 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) { |
|
571 | |||
572 | /** |
||
573 | * Load new featured meta box via ajax. |
||
574 | * |
||
575 | * @since 1.0.0 |
||
576 | * @access public |
||
577 | * |
||
578 | * @return void |
||
579 | */ |
||
580 | 2 | public function ajax_callback() { |
|
609 | |||
610 | /** |
||
611 | * Add custom class 'featured-meta-box' to meta box. |
||
612 | * |
||
613 | * @since 1.0.0 |
||
614 | * @access public |
||
615 | * |
||
616 | * @see add_metabox_classes |
||
617 | * |
||
618 | * @param array $classes Classes to add in the meta box. |
||
619 | * |
||
620 | * @return array |
||
621 | */ |
||
622 | 1 | public function add_metabox_classes( $classes ) { |
|
627 | |||
628 | /** |
||
629 | * Add custom fields in media uploader. |
||
630 | * |
||
631 | * @since 3.4.0 |
||
632 | * |
||
633 | * @param array $form_fields Fields to include in media attachment form. |
||
634 | * @param array $post Post data. |
||
635 | * |
||
636 | * @return array |
||
637 | */ |
||
638 | 1 | public function media_attachment_custom_fields( $form_fields, $post ) { |
|
647 | |||
648 | /** |
||
649 | * Save values of media uploader custom fields. |
||
650 | * |
||
651 | * @since 3.4.0 |
||
652 | * |
||
653 | * @param array $post Post data for database. |
||
654 | * @param array $attachment Attachment fields from $_POST form. |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | 1 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
|
665 | |||
666 | /** |
||
667 | * Update featured images in the database. |
||
668 | * |
||
669 | * @since 1.0.0 |
||
670 | * @access public |
||
671 | * |
||
672 | * @see plugin_basename() |
||
673 | * @see update_post_meta() |
||
674 | * @see current_user_can() |
||
675 | * |
||
676 | * @param int $post_id Current post id. |
||
677 | * |
||
678 | * @return bool|null |
||
679 | */ |
||
680 | 2 | public function save_meta( $post_id ) { |
|
697 | |||
698 | /** |
||
699 | * Sanitize array. |
||
700 | * |
||
701 | * @since 3.6.0 |
||
702 | * @access protected |
||
703 | * |
||
704 | * @param array $input_array Input array. |
||
705 | * |
||
706 | * @return array |
||
707 | */ |
||
708 | 1 | protected function sanitize_array( $input_array ) { |
|
717 | |||
718 | /** |
||
719 | * Verify metabox nonces. |
||
720 | * |
||
721 | * @access protected |
||
722 | * @see wp_verify_nonce() |
||
723 | * |
||
724 | * @return bool |
||
725 | */ |
||
726 | protected function verify_nonces() { |
||
744 | |||
745 | /** |
||
746 | * Add update notice. Displayed in plugin update page. |
||
747 | * |
||
748 | * @since 2.0.0 |
||
749 | * @access public |
||
750 | * |
||
751 | * @return void |
||
752 | */ |
||
753 | 1 | public function update_notice() { |
|
759 | |||
760 | /** |
||
761 | * Execute query. |
||
762 | * |
||
763 | * @param string $query Query to execute. |
||
764 | * |
||
765 | * @return null|string |
||
766 | */ |
||
767 | 6 | private function execute_query( $query ) { |
|
770 | |||
771 | /** |
||
772 | * Get attachment id of the image by image url. |
||
773 | * |
||
774 | * @since 3.1.7 |
||
775 | * @access protected |
||
776 | * @global object $wpdb |
||
777 | * |
||
778 | * @param string $image_url URL of an image. |
||
779 | * |
||
780 | * @return string |
||
781 | */ |
||
782 | 1 | protected function get_attachment_id( $image_url ) { |
|
785 | |||
786 | /** |
||
787 | * Get image url of the image by attachment id. |
||
788 | * |
||
789 | * @since 2.0.0 |
||
790 | * @access public |
||
791 | * |
||
792 | * @see wp_get_attachment_image_src() |
||
793 | * |
||
794 | * @param int $attachment_id attachment id of an image. |
||
795 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
796 | * |
||
797 | * @return string |
||
798 | */ |
||
799 | 1 | public function get_image_url( $attachment_id, $size = 'full' ) { |
|
804 | |||
805 | /** |
||
806 | * Get image thumbnail url of specific size by image url. |
||
807 | * |
||
808 | * @since 2.0.0 |
||
809 | * @access public |
||
810 | * |
||
811 | * @see get_image_id() |
||
812 | * @see wp_get_attachment_image_src() |
||
813 | * |
||
814 | * @param string $image_url url of an image. |
||
815 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
816 | * |
||
817 | * @return string |
||
818 | */ |
||
819 | 3 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
|
825 | |||
826 | /** |
||
827 | * Gets attachment id from given image url. |
||
828 | * |
||
829 | * @param string $image_url url of an image. |
||
830 | * |
||
831 | * @since 2.0.0 |
||
832 | * @access public |
||
833 | * |
||
834 | * @return int|null attachment id of an image |
||
835 | */ |
||
836 | 5 | public function get_image_id( $image_url ) { |
|
852 | |||
853 | /** |
||
854 | * Get image title. |
||
855 | * |
||
856 | * @since 2.0.0 |
||
857 | * @access public |
||
858 | * |
||
859 | * @param string $image_url URL of an image. |
||
860 | * |
||
861 | * @return string |
||
862 | */ |
||
863 | 1 | public function get_image_title( $image_url ) { |
|
866 | |||
867 | /** |
||
868 | * Get image title by id. |
||
869 | * |
||
870 | * @since 2.0.0 |
||
871 | * @access public |
||
872 | * |
||
873 | * @param int $attachment_id Attachment id of an image. |
||
874 | * |
||
875 | * @return string |
||
876 | */ |
||
877 | 1 | public function get_image_title_by_id( $attachment_id ) { |
|
880 | |||
881 | /** |
||
882 | * Get image caption. |
||
883 | * |
||
884 | * @since 2.0.0 |
||
885 | * @access public |
||
886 | * |
||
887 | * @param string $image_url URL of an image. |
||
888 | * |
||
889 | * @return string |
||
890 | */ |
||
891 | 1 | public function get_image_caption( $image_url ) { |
|
894 | |||
895 | /** |
||
896 | * Get image caption by id. |
||
897 | * |
||
898 | * @since 2.0.0 |
||
899 | * @access public |
||
900 | * |
||
901 | * @param int $attachment_id Attachment id of an image. |
||
902 | * |
||
903 | * @return string |
||
904 | */ |
||
905 | 1 | public function get_image_caption_by_id( $attachment_id ) { |
|
908 | |||
909 | /** |
||
910 | * Get image alternate text. |
||
911 | * |
||
912 | * @since 2.0.0 |
||
913 | * @access public |
||
914 | * |
||
915 | * @see get_post_meta() |
||
916 | * |
||
917 | * @param string $image_url URL of an image. |
||
918 | * |
||
919 | * @return string |
||
920 | */ |
||
921 | 1 | public function get_image_alt( $image_url ) { |
|
931 | |||
932 | /** |
||
933 | * Get image alternate text by attachment id. |
||
934 | * |
||
935 | * @since 2.0.0 |
||
936 | * @access public |
||
937 | * |
||
938 | * @see get_post_meta() |
||
939 | * |
||
940 | * @param int $attachment_id Attachment id of an image. |
||
941 | * |
||
942 | * @return string |
||
943 | */ |
||
944 | 1 | public function get_image_alt_by_id( $attachment_id ) { |
|
949 | |||
950 | /** |
||
951 | * Get image description. |
||
952 | * |
||
953 | * @since 3.0.0 |
||
954 | * @access public |
||
955 | * |
||
956 | * @param string $image_url URL of an image. |
||
957 | * |
||
958 | * @return string |
||
959 | */ |
||
960 | 1 | public function get_image_description( $image_url ) { |
|
963 | |||
964 | /** |
||
965 | * Get image description by id. |
||
966 | * |
||
967 | * @since 3.0.0 |
||
968 | * @access public |
||
969 | * |
||
970 | * @param int $attachment_id attachment id of an image. |
||
971 | * |
||
972 | * @return string |
||
973 | */ |
||
974 | 1 | public function get_image_description_by_id( $attachment_id ) { |
|
977 | |||
978 | /** |
||
979 | * Get link to image. |
||
980 | * |
||
981 | * @since 3.4.0 |
||
982 | * @access public |
||
983 | * |
||
984 | * @param int $attachment_id Attachment id of an image. |
||
985 | * |
||
986 | * @return string|null |
||
987 | */ |
||
988 | 1 | public function get_link_to_image( $attachment_id ) { |
|
991 | |||
992 | /** |
||
993 | * Get all attachment ids of the post. |
||
994 | * |
||
995 | * @since 2.0.0 |
||
996 | * @access public |
||
997 | * |
||
998 | * @see get_post_meta() |
||
999 | * |
||
1000 | * @param int $post_id id of the current post. |
||
1001 | * |
||
1002 | * @return array |
||
1003 | */ |
||
1004 | 2 | public function get_post_attachment_ids( $post_id ) { |
|
1017 | |||
1018 | /** |
||
1019 | * Get real post id. |
||
1020 | * |
||
1021 | * @since 3.6.0 |
||
1022 | * @access protected |
||
1023 | * |
||
1024 | * @param int|null $post_id Post id. |
||
1025 | * |
||
1026 | * @return int|null |
||
1027 | */ |
||
1028 | 6 | protected function get_real_post_id( $post_id = null ) { |
|
1037 | |||
1038 | /** |
||
1039 | * Fetches featured image data of nth position. |
||
1040 | * |
||
1041 | * @since 3.0.0 |
||
1042 | * @access public |
||
1043 | * |
||
1044 | * @see get_featured_images() |
||
1045 | * |
||
1046 | * @param int $position Position of the featured image. |
||
1047 | * @param int $post_id Current post id. |
||
1048 | * |
||
1049 | * @return array if found, null otherwise. |
||
1050 | */ |
||
1051 | 2 | public function get_nth_featured_image( $position, $post_id = null ) { |
|
1058 | |||
1059 | /** |
||
1060 | * Check if the image is attached with the particular post. |
||
1061 | * |
||
1062 | * @since 2.0.0 |
||
1063 | * @access public |
||
1064 | * |
||
1065 | * @see get_post_attachment_ids() |
||
1066 | * |
||
1067 | * @param int $attachment_id Attachment id of an image. |
||
1068 | * @param int $post_id Current post id. |
||
1069 | * |
||
1070 | * @return bool |
||
1071 | */ |
||
1072 | 1 | public function is_attached( $attachment_id, $post_id ) { |
|
1081 | |||
1082 | /** |
||
1083 | * Retrieve featured images for specific post(s). |
||
1084 | * |
||
1085 | * @since 2.0.0 |
||
1086 | * @access public |
||
1087 | * |
||
1088 | * @see get_post_meta() |
||
1089 | * |
||
1090 | * @param int $post_id id of the current post. |
||
1091 | * |
||
1092 | * @return array |
||
1093 | */ |
||
1094 | 7 | public function get_featured_images( $post_id = null ) { |
|
1121 | |||
1122 | /** |
||
1123 | * Check to see if the upload url is already available in path. |
||
1124 | * |
||
1125 | * @since 3.1.14 |
||
1126 | * @access protected |
||
1127 | * |
||
1128 | * @param string $img Uploaded image. |
||
1129 | * |
||
1130 | * @return string |
||
1131 | */ |
||
1132 | 2 | protected function get_real_upload_path( $img ) { |
|
1140 | |||
1141 | /** |
||
1142 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
1143 | * |
||
1144 | * @since 3.1.7 |
||
1145 | * @access public |
||
1146 | * |
||
1147 | * @see $this->get_featured_images() |
||
1148 | * |
||
1149 | * @param int $post_id Current post id. |
||
1150 | * |
||
1151 | * @return array An array of images or an empty array on failure |
||
1152 | */ |
||
1153 | 2 | public function get_all_featured_images( $post_id = null ) { |
|
1170 | |||
1171 | /** |
||
1172 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
1173 | * |
||
1174 | * @since 1.0.0 |
||
1175 | * @access public |
||
1176 | * |
||
1177 | * @see load_plugin_textdomain() |
||
1178 | * @see plugin_basename() |
||
1179 | * @action plugins_loaded |
||
1180 | * |
||
1181 | * @codeCoverageIgnore |
||
1182 | * |
||
1183 | * @return void |
||
1184 | */ |
||
1185 | public function load_plugin_textdomain() { |
||
1192 | } |
||
1193 | |||
1204 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: