Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Message { |
||
16 | /** |
||
17 | * Message ID |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $id; |
||
22 | |||
23 | /** |
||
24 | * Template |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $template; |
||
29 | |||
30 | /** |
||
31 | * Hosted with partner name |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $hosted_with_partner; |
||
36 | |||
37 | /** |
||
38 | * Inactive plugins |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $inactive_plugins; |
||
43 | |||
44 | /** |
||
45 | * Active plugins |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $active_plugins; |
||
50 | |||
51 | /** |
||
52 | * Installed plugins |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $installed_plugins; |
||
57 | |||
58 | /** |
||
59 | * Uninstalled plugins |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $uninstalled_plugins; |
||
64 | |||
65 | /** |
||
66 | * User roles |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $roles; |
||
71 | |||
72 | /** |
||
73 | * Message content |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $content; |
||
78 | |||
79 | /** |
||
80 | * Message path regular expression |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $message_path_regex; |
||
85 | |||
86 | /** |
||
87 | * Call to action |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $cta; |
||
92 | |||
93 | /** |
||
94 | * Redux action |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $redux_action; |
||
99 | |||
100 | /** |
||
101 | * Query |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $query; |
||
106 | |||
107 | /** |
||
108 | * Message priority |
||
109 | * |
||
110 | * @var int |
||
111 | */ |
||
112 | protected $priority; |
||
113 | |||
114 | /** |
||
115 | * Feature class |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | protected $feature_class; |
||
120 | |||
121 | /** |
||
122 | * Max dismissals |
||
123 | * |
||
124 | * @var int |
||
125 | */ |
||
126 | protected $max_dismissals; |
||
127 | |||
128 | /** |
||
129 | * Next show in |
||
130 | * |
||
131 | * @var int |
||
132 | */ |
||
133 | protected $next_show; |
||
134 | |||
135 | /** |
||
136 | * Theme |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $theme; |
||
141 | |||
142 | /** |
||
143 | * Active widgets |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | protected $active_widgets; |
||
148 | |||
149 | /** |
||
150 | * Inactive widgets |
||
151 | * |
||
152 | * @var array |
||
153 | */ |
||
154 | protected $inactive_widgets; |
||
155 | |||
156 | /** |
||
157 | * Option matches |
||
158 | * |
||
159 | * @var array |
||
160 | */ |
||
161 | protected $option_matches; |
||
162 | |||
163 | /** |
||
164 | * Uses mobile browser |
||
165 | * |
||
166 | * @var bool |
||
167 | */ |
||
168 | protected $mobile_browser; |
||
169 | |||
170 | /** |
||
171 | * User locales |
||
172 | * |
||
173 | * @var array |
||
174 | */ |
||
175 | protected $user_locales; |
||
176 | |||
177 | /** |
||
178 | * Message is dismissable |
||
179 | * |
||
180 | * @var bool |
||
181 | */ |
||
182 | protected $is_dismissible; |
||
183 | |||
184 | /** |
||
185 | * Calculated score |
||
186 | * |
||
187 | * @var int |
||
188 | */ |
||
189 | protected $calculated_score; |
||
190 | |||
191 | /** |
||
192 | * Class constructor |
||
193 | * |
||
194 | * @param string $id Message ID. |
||
195 | * @param string $feature_class Feature class. |
||
196 | */ |
||
197 | public function __construct( $id, $feature_class ) { |
||
234 | |||
235 | /** |
||
236 | * Open the CTA link in the same window instead of a new window |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function open_cta_in_same_window() { |
||
245 | |||
246 | /** |
||
247 | * Score the message path |
||
248 | * |
||
249 | * @param string $path path. |
||
250 | * @param int $external_user_id external_user_id. |
||
251 | * @param array $query query. |
||
252 | * @param int $score score. |
||
253 | * |
||
254 | * @return bool|int |
||
255 | */ |
||
256 | private function score_message_path( $path, $external_user_id, $query, $score ) { |
||
265 | |||
266 | /** |
||
267 | * Score the query string |
||
268 | * |
||
269 | * @param string $path path. |
||
270 | * @param int $external_user_id external_user_id. |
||
271 | * @param array $query query. |
||
272 | * @param int $score score. |
||
273 | * |
||
274 | * @return bool|int |
||
275 | */ |
||
276 | private function score_query_string( $path, $external_user_id, $query, $score ) { |
||
299 | |||
300 | /** |
||
301 | * Score option matches |
||
302 | * |
||
303 | * @param string $path path. |
||
304 | * @param int $external_user_id external_user_id. |
||
305 | * @param array $query query. |
||
306 | * @param int $score score. |
||
307 | * |
||
308 | * @return bool|int |
||
309 | */ |
||
310 | private function score_option_matches( $path, $external_user_id, $query, $score ) { |
||
334 | |||
335 | /** |
||
336 | * Score dismissal |
||
337 | * |
||
338 | * @param string $path path. |
||
339 | * @param int $external_user_id external_user_id. |
||
340 | * @param array $query query. |
||
341 | * @param int $score score. |
||
342 | * |
||
343 | * @return bool|int |
||
344 | */ |
||
345 | private function score_dismissal( $path, $external_user_id, $query, $score ) { |
||
360 | |||
361 | /** |
||
362 | * Score hosted with partner |
||
363 | * |
||
364 | * @param string $path path. |
||
365 | * @param int $external_user_id external_user_id. |
||
366 | * @param array $query query. |
||
367 | * @param int $score score. |
||
368 | * |
||
369 | * @return bool|int |
||
370 | */ |
||
371 | private function score_hosted_with_partner( $path, $external_user_id, $query, $score ) { |
||
385 | |||
386 | /** |
||
387 | * Score user locale |
||
388 | * |
||
389 | * @param string $path path. |
||
390 | * @param int $external_user_id external_user_id. |
||
391 | * @param array $query query. |
||
392 | * @param int $score score. |
||
393 | * |
||
394 | * @return bool|int |
||
395 | */ |
||
396 | private function score_user_locale( $path, $external_user_id, $query, $score ) { |
||
405 | |||
406 | /** |
||
407 | * Score user roles |
||
408 | * |
||
409 | * @param string $path path. |
||
410 | * @param int $external_user_id external_user_id. |
||
411 | * @param array $query query. |
||
412 | * @param int $score score. |
||
413 | * |
||
414 | * @return bool|int |
||
415 | */ |
||
416 | private function score_user_roles( $path, $external_user_id, $query, $score ) { |
||
430 | |||
431 | /** |
||
432 | * Score user theme |
||
433 | * |
||
434 | * @param string $path path. |
||
435 | * @param int $external_user_id external_user_id. |
||
436 | * @param array $query query. |
||
437 | * @param int $score score. |
||
438 | * |
||
439 | * @return bool|int |
||
440 | */ |
||
441 | private function score_user_theme( $path, $external_user_id, $query, $score ) { |
||
462 | |||
463 | /** |
||
464 | * Score plugins |
||
465 | * |
||
466 | * @param string $path path. |
||
467 | * @param int $external_user_id external_user_id. |
||
468 | * @param array $query query. |
||
469 | * @param int $score score. |
||
470 | * |
||
471 | * @return bool|int |
||
472 | */ |
||
473 | private function score_plugins( $path, $external_user_id, $query, $score ) { |
||
564 | |||
565 | /** |
||
566 | * Score active widgets |
||
567 | * |
||
568 | * @param string $path path. |
||
569 | * @param int $external_user_id external_user_id. |
||
570 | * @param array $query query. |
||
571 | * @param int $score score. |
||
572 | * |
||
573 | * @return bool|int |
||
574 | */ |
||
575 | View Code Duplication | private function score_active_widgets( $path, $external_user_id, $query, $score ) { |
|
589 | |||
590 | /** |
||
591 | * Score inactive widgets |
||
592 | * |
||
593 | * @param string $path path. |
||
594 | * @param int $external_user_id external_user_id. |
||
595 | * @param array $query query. |
||
596 | * @param int $score score. |
||
597 | * |
||
598 | * @return bool|int |
||
599 | */ |
||
600 | View Code Duplication | private function score_inactive_widgets( $path, $external_user_id, $query, $score ) { |
|
615 | |||
616 | /** |
||
617 | * Score mobile browser |
||
618 | * |
||
619 | * @param string $path path. |
||
620 | * @param int $external_user_id external_user_id. |
||
621 | * @param array $query query. |
||
622 | * @param int $score score. |
||
623 | * @param bool $mobile_browser mobile browser. |
||
624 | * |
||
625 | * @return bool|int |
||
626 | */ |
||
627 | private function score_mobile_browser( $path, $external_user_id, $query, $score, $mobile_browser ) { |
||
638 | |||
639 | /** |
||
640 | * Calculates the score of the jitm message |
||
641 | * |
||
642 | * The goal is to return a score of 0 if anything fails to match, and a score > 1 for all matches. |
||
643 | * |
||
644 | * @param string $path The message path from the user's browser. |
||
645 | * @param int $external_user_id The external user id. |
||
646 | * @param array $external_caps The external user's role. |
||
647 | * @param array $query The query string from the browser. |
||
648 | * @param bool $mobile_browser Is using a mobile browser. |
||
649 | * |
||
650 | * @return int The score for this jitm |
||
651 | */ |
||
652 | public function score( $path, $external_user_id, $external_caps, $query, $mobile_browser ) { |
||
679 | |||
680 | /** |
||
681 | * Renders the internal state to a simple object |
||
682 | * |
||
683 | * @return \stdClass The simple object |
||
684 | */ |
||
685 | public function render() { |
||
730 | |||
731 | /** |
||
732 | * Adds an item to the list that may be shown on the jitm |
||
733 | * |
||
734 | * @param string|callable $item item. |
||
735 | * @param string $url url. |
||
736 | * |
||
737 | * @return $this |
||
738 | */ |
||
739 | public function add_item_to_list( $item, $url = null ) { |
||
751 | |||
752 | /** |
||
753 | * Requires a widget to be active |
||
754 | * |
||
755 | * @param string $widget_slug The slug of the widget. |
||
756 | * |
||
757 | * @return $this |
||
758 | */ |
||
759 | public function has_widget_active( $widget_slug ) { |
||
764 | |||
765 | /** |
||
766 | * Requires a widget to be inactive |
||
767 | * |
||
768 | * @param string $widget_slug The slug of the widget. |
||
769 | * |
||
770 | * @return $this |
||
771 | */ |
||
772 | public function has_widget_inactive( $widget_slug ) { |
||
777 | |||
778 | /** |
||
779 | * Ensure that the browser carries a specific query string |
||
780 | * |
||
781 | * @param string $key The key to check for. |
||
782 | * @param string $value A regex to match. |
||
783 | * |
||
784 | * @return $this |
||
785 | */ |
||
786 | public function with_query_string( $key, $value ) { |
||
791 | |||
792 | /** |
||
793 | * A redux action dispatched when the CTA is clicked |
||
794 | * |
||
795 | * @param string $type Action name. |
||
796 | * @param object $props Action props. |
||
797 | * |
||
798 | * @return $this |
||
799 | */ |
||
800 | public function with_redux_action( $type, $props = null ) { |
||
805 | |||
806 | /** |
||
807 | * Ensure that a specific theme is active |
||
808 | * |
||
809 | * @param string $theme theme. |
||
810 | * |
||
811 | * @return $this |
||
812 | */ |
||
813 | public function with_active_theme( $theme ) { |
||
818 | |||
819 | /** |
||
820 | * Only show this JITM when the specified plugin is inactive OR not installed |
||
821 | * |
||
822 | * @param string $plugin The path to the plugin. |
||
823 | * |
||
824 | * @return $this |
||
825 | */ |
||
826 | public function plugin_inactive( $plugin ) { |
||
831 | |||
832 | /** |
||
833 | * Only show this JITM when the specified plugin is active and installed |
||
834 | * Multiple calls are treated as OR: if _any_ of the plugins are active, the rule passes |
||
835 | * |
||
836 | * @param string $plugin The path to the plugin. |
||
837 | * |
||
838 | * @return $this |
||
839 | */ |
||
840 | public function plugin_active( $plugin ) { |
||
845 | |||
846 | /** |
||
847 | * A rule for a plugin being installed but either active or not active |
||
848 | * |
||
849 | * @param string $plugin The path to the plugin. |
||
850 | * |
||
851 | * @return $this |
||
852 | */ |
||
853 | public function plugin_installed( $plugin ) { |
||
858 | |||
859 | /** |
||
860 | * A rule to check that a specific plugin is not installed |
||
861 | * |
||
862 | * @param string $plugin The path to the plugin. |
||
863 | * |
||
864 | * @return $this |
||
865 | */ |
||
866 | public function plugin_not_installed( $plugin ) { |
||
871 | |||
872 | /** |
||
873 | * Limits JITM to users who speak specific languages. |
||
874 | * |
||
875 | * @param array|string $lang target user locales. |
||
876 | */ |
||
877 | public function for_user_locale( $lang ) { |
||
886 | |||
887 | /** |
||
888 | * Only show if the user is in the specified role |
||
889 | * |
||
890 | * @param string $role The role. |
||
891 | * |
||
892 | * @return $this |
||
893 | */ |
||
894 | public function user_is( $role ) { |
||
899 | |||
900 | /** |
||
901 | * Show the specified message to the user |
||
902 | * |
||
903 | * @param string $message The message. |
||
904 | * @param string $description A longer description that shows up under the message. |
||
905 | * @param string $classes Any special classes to put on the card (such as is-upgrade-personal). |
||
906 | * |
||
907 | * @return $this |
||
908 | */ |
||
909 | public function show( $message, $description = '', $classes = '' ) { |
||
916 | |||
917 | /** |
||
918 | * Call a hook in the client to get the message to display |
||
919 | * |
||
920 | * @param string $hook The hook to call in the client. |
||
921 | * |
||
922 | * @return $this |
||
923 | */ |
||
924 | public function show_hook( $hook ) { |
||
929 | |||
930 | /** |
||
931 | * The message path that needs to match before showing |
||
932 | * |
||
933 | * Follows the form: wp:PAGE(REGEX):HOOK |
||
934 | * |
||
935 | * first part: |
||
936 | * |
||
937 | * wp: for wp-admin |
||
938 | * |
||
939 | * second part: |
||
940 | * |
||
941 | * a regex that will need to match "$screen->base" |
||
942 | * |
||
943 | * last part: |
||
944 | * |
||
945 | * The hook to display the content on, such as `admin-notices` |
||
946 | * |
||
947 | * @param string $regex The message path regex. |
||
948 | * |
||
949 | * @return $this |
||
950 | */ |
||
951 | public function message_path( $regex ) { |
||
956 | |||
957 | /** |
||
958 | * A call to action |
||
959 | * |
||
960 | * @param string $cta The message to display on the CTA button. |
||
961 | * @param string $hook A hook to call on the client side to filter the message with. |
||
962 | * @param string $link URL. |
||
963 | * @param bool $primary Whether to use the primary button color or not. |
||
964 | * |
||
965 | * @return $this |
||
966 | */ |
||
967 | public function with_cta( $cta, $hook = '', $link = '', $primary = true ) { |
||
975 | |||
976 | /** |
||
977 | * Adds an icon to the JITM |
||
978 | * |
||
979 | * @param string $emblem You may put an svg here, or a predifined emblem from Jetpack. |
||
980 | * |
||
981 | * @return $this |
||
982 | */ |
||
983 | public function with_icon( $emblem = 'jetpack' ) { |
||
988 | |||
989 | /** |
||
990 | * Set the template of the JITM |
||
991 | * |
||
992 | * @param string $template Template name. |
||
993 | * |
||
994 | * @return $this |
||
995 | */ |
||
996 | public function with_template( $template ) { |
||
1001 | |||
1002 | /** |
||
1003 | * Set's the priority of this specific jitm if there are any conflicts |
||
1004 | * |
||
1005 | * @param int $priority The priority. Higher numbers result in a higher priority. |
||
1006 | * |
||
1007 | * @return $this |
||
1008 | */ |
||
1009 | public function priority( $priority ) { |
||
1014 | |||
1015 | /** |
||
1016 | * Sets the amount of time to reshow a jitm after it has been dismissed |
||
1017 | * |
||
1018 | * @param int $seconds The number of seconds to show wait to show the jitm again. |
||
1019 | * |
||
1020 | * @return $this |
||
1021 | */ |
||
1022 | public function show_again_after( $seconds ) { |
||
1027 | |||
1028 | /** |
||
1029 | * Set the maximum number of dismissals before this jitm will never be shown again |
||
1030 | * |
||
1031 | * @param int $times The maximum number of times to show this message. |
||
1032 | * |
||
1033 | * @return $this |
||
1034 | */ |
||
1035 | public function max_dismissals( $times ) { |
||
1040 | |||
1041 | /** |
||
1042 | * Sets a flag to check if a site is hosted with a certain partner. |
||
1043 | * |
||
1044 | * @param bool|string $partner_name partner name. |
||
1045 | * |
||
1046 | * @return $this |
||
1047 | */ |
||
1048 | public function is_hosted_with_partner( $partner_name ) { |
||
1053 | |||
1054 | /** |
||
1055 | * Ensure that the blog has an option that satisfies the given matcher function. |
||
1056 | * |
||
1057 | * $matcher should return: |
||
1058 | * false - to reject the value |
||
1059 | * true - to accept the value |
||
1060 | * (int) - to accept the value and assign it a score (@see ->score_option_matches()) |
||
1061 | * |
||
1062 | * @param string $option_name option name. |
||
1063 | * @param callable $matcher - $matcher( $option_value ). |
||
1064 | * |
||
1065 | * @return $this |
||
1066 | */ |
||
1067 | public function with_option_matching( $option_name, callable $matcher ) { |
||
1072 | |||
1073 | /** |
||
1074 | * Limits the JITM to mobile or non-mobile browsers |
||
1075 | * |
||
1076 | * @param bool $mobile_browser - Whether to limit to mobile or non-mobile browsers. |
||
1077 | * |
||
1078 | * @return $this |
||
1079 | */ |
||
1080 | public function mobile_browser( $mobile_browser ) { |
||
1085 | |||
1086 | /** |
||
1087 | * Get the feature class name |
||
1088 | * |
||
1089 | * @return string |
||
1090 | */ |
||
1091 | public function get_feature_class() { |
||
1094 | |||
1095 | /** |
||
1096 | * Whether or not to display the dismiss button for the JITM. |
||
1097 | * |
||
1098 | * @param bool $dismissible Should JITM be dismissible. |
||
1099 | * |
||
1100 | * @return $this |
||
1101 | */ |
||
1102 | public function is_dismissible( $dismissible ) { |
||
1107 | |||
1108 | /** |
||
1109 | * Replaces the CTA button link with an AJAX action trigger. |
||
1110 | * |
||
1111 | * @param string $action AJAX action name. |
||
1112 | * |
||
1113 | * @return $this |
||
1114 | */ |
||
1115 | public function with_cta_ajax_action( $action ) { |
||
1120 | |||
1121 | /** |
||
1122 | * Get the site's dismissals |
||
1123 | * |
||
1124 | * @return array The array of dismissed jitms |
||
1125 | */ |
||
1126 | public function get_dismissals() { |
||
1129 | |||
1130 | /** |
||
1131 | * Get's the site's installed plugins |
||
1132 | * |
||
1133 | * @return array An array of installed plugins |
||
1134 | */ |
||
1135 | View Code Duplication | public function get_installed_plugins() { |
|
1144 | |||
1145 | /** |
||
1146 | * Get's the site's active plugins |
||
1147 | * |
||
1148 | * @return array An array of active plugins |
||
1149 | */ |
||
1150 | public function get_active_plugins() { |
||
1159 | |||
1160 | /** |
||
1161 | * Get the list of widgets |
||
1162 | * |
||
1163 | * @return array |
||
1164 | */ |
||
1165 | public function get_widget_list() { |
||
1180 | |||
1181 | } |
||
1182 |
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..