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 Jetpack_Functions 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 Jetpack_Functions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class Jetpack_Functions { |
||
13 | |||
14 | /** |
||
15 | * Determine whether the active plan supports a particular feature |
||
16 | * |
||
17 | * @uses Jetpack::get_active_plan() |
||
18 | * |
||
19 | * @access public |
||
20 | * @static |
||
21 | * |
||
22 | * @return bool True if plan supports feature, false if not |
||
23 | */ |
||
24 | public static function active_plan_supports( $feature ) { |
||
49 | |||
50 | public static function admin_url( $args = null ) { |
||
55 | |||
56 | /** |
||
57 | * Converts any url in a stylesheet, to the correct absolute url. |
||
58 | * |
||
59 | * Considerations: |
||
60 | * - Normal, relative URLs `feh.png` |
||
61 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
62 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
63 | * - Absolute URLs `http://domain.com/feh.png` |
||
64 | * - Domain root relative URLs `/feh.png` |
||
65 | * |
||
66 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
67 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
68 | * |
||
69 | * @return mixed|string |
||
70 | */ |
||
71 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
115 | |||
116 | /* |
||
117 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
118 | * so we can bring them directly to their site in calypso. |
||
119 | * |
||
120 | * @param string | url |
||
121 | * @return string | url without the guff |
||
122 | */ |
||
123 | public static function build_raw_urls( $url ) { |
||
129 | |||
130 | /** |
||
131 | * Checks if the site is currently in an identity crisis. |
||
132 | * |
||
133 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
134 | */ |
||
135 | public static function check_identity_crisis() { |
||
142 | |||
143 | /** |
||
144 | * Gets current user IP address. |
||
145 | * |
||
146 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
147 | * |
||
148 | * @return string Current user IP address. |
||
149 | */ |
||
150 | public static function current_user_ip( $check_all_headers = false ) { |
||
170 | |||
171 | function current_user_is_connection_owner() { |
||
175 | |||
176 | /** |
||
177 | * Determines whether the current theme supports featured images or not. |
||
178 | * @return string ( '1' | '0' ) |
||
179 | */ |
||
180 | public static function featured_images_enabled() { |
||
184 | |||
185 | /** |
||
186 | * Returns true if the site has file write access false otherwise. |
||
187 | * @return string ( '1' | '0' ) |
||
188 | **/ |
||
189 | public static function file_system_write_access() { |
||
209 | |||
210 | /** |
||
211 | * Get a list of activated modules as an array of module slugs. |
||
212 | */ |
||
213 | public static function get_active_modules() { |
||
245 | |||
246 | /** |
||
247 | * Get the plan that this Jetpack site is currently using |
||
248 | * |
||
249 | * @uses get_option() |
||
250 | * |
||
251 | * @access public |
||
252 | * @static |
||
253 | * |
||
254 | * @return array Active Jetpack plan details |
||
255 | */ |
||
256 | public static function get_active_plan() { |
||
334 | |||
335 | /** |
||
336 | * Gets all plugins currently active in values, regardless of whether they're |
||
337 | * traditionally activated or network activated. |
||
338 | * |
||
339 | * @todo Store the result in core's object cache maybe? |
||
340 | */ |
||
341 | public static function get_active_plugins() { |
||
357 | |||
358 | /** |
||
359 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
360 | * |
||
361 | * @deprecated 4.7 use get_avatar_url instead. |
||
362 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
363 | * @param int $size Size of the avatar image |
||
364 | * @param string $default URL to a default image to use if no avatar is available |
||
365 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
377 | |||
378 | /** |
||
379 | * Get the wpcom user data of the current|specified connected user. |
||
380 | */ |
||
381 | public static function get_connected_user_data( $user_id = null ) { |
||
405 | |||
406 | /** |
||
407 | * Get the wpcom email of the current|specified connected user. |
||
408 | */ |
||
409 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
423 | |||
424 | /** |
||
425 | * Get $content_width, but with a <s>twist</s> filter. |
||
426 | */ |
||
427 | public static function get_content_width() { |
||
438 | |||
439 | |||
440 | /** |
||
441 | * Like core's get_file_data implementation, but caches the result. |
||
442 | */ |
||
443 | public static function get_file_data( $file, $headers ) { |
||
471 | |||
472 | /** |
||
473 | * Given a minified path, and a non-minified path, will return |
||
474 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
475 | * |
||
476 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
477 | * root Jetpack directory. |
||
478 | * |
||
479 | * @since 5.6.0 |
||
480 | * |
||
481 | * @param string $min_path |
||
482 | * @param string $non_min_path |
||
483 | * @return string The URL to the file |
||
484 | */ |
||
485 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
492 | |||
493 | /** |
||
494 | * Return string containing the Jetpack logo. |
||
495 | * |
||
496 | * @since 3.9.0 |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | public static function get_jp_emblem() { |
||
503 | |||
504 | /* |
||
505 | * This method is used to organize all options that can be reset |
||
506 | * without disconnecting Jetpack. |
||
507 | * |
||
508 | * It is used in class.jetpack-cli.php to reset options |
||
509 | * |
||
510 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
511 | * |
||
512 | * @return array of options to delete. |
||
513 | */ |
||
514 | public static function get_jetpack_options_for_reset() { |
||
517 | |||
518 | /** |
||
519 | * Get the locale. |
||
520 | * |
||
521 | * @return string|bool |
||
522 | */ |
||
523 | function get_locale() { |
||
532 | |||
533 | /** |
||
534 | * Get the wpcom email of the master user. |
||
535 | */ |
||
536 | public static function get_master_user_email() { |
||
543 | |||
544 | /** |
||
545 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
546 | * |
||
547 | * Based on local php max_execution_time in php.ini |
||
548 | * |
||
549 | * @since 5.4 |
||
550 | * @return int |
||
551 | **/ |
||
552 | public static function get_max_execution_time() { |
||
561 | |||
562 | /** |
||
563 | * Extract a module's slug from its full path. |
||
564 | */ |
||
565 | public static function get_module_slug( $file ) { |
||
568 | |||
569 | /** |
||
570 | * Generate a module's path from its slug. |
||
571 | */ |
||
572 | public static function get_module_path( $slug ) { |
||
575 | |||
576 | /** |
||
577 | * Load module data from module file. Headers differ from WordPress |
||
578 | * plugin headers to avoid them being identified as standalone |
||
579 | * plugins on the WordPress plugins page. |
||
580 | */ |
||
581 | public static function get_module( $module ) { |
||
672 | |||
673 | /** |
||
674 | * Gets and parses additional plugin data to send with the heartbeat data |
||
675 | * |
||
676 | * @since 3.8.1 |
||
677 | * |
||
678 | * @return array Array of plugin data |
||
679 | */ |
||
680 | public static function get_parsed_plugin_data() { |
||
701 | |||
702 | /** |
||
703 | * Gets and parses theme data to send with the heartbeat data |
||
704 | * |
||
705 | * @since 3.8.1 |
||
706 | * |
||
707 | * @return array Array of theme data |
||
708 | */ |
||
709 | public static function get_parsed_theme_data() { |
||
731 | |||
732 | /** |
||
733 | * Guess locale from language code. |
||
734 | * |
||
735 | * @param string $lang Language code. |
||
736 | * @return string|bool |
||
737 | */ |
||
738 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
777 | |||
778 | private static function get_site_user_count() { |
||
793 | |||
794 | /** |
||
795 | * Returns an array of all PHP files in the specified absolute path. |
||
796 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
797 | * |
||
798 | * @param string $absolute_path The absolute path of the directory to search. |
||
799 | * @return array Array of absolute paths to the PHP files. |
||
800 | */ |
||
801 | public static function glob_php( $absolute_path ) { |
||
830 | |||
831 | /** |
||
832 | * Checks if Akismet is active and working. |
||
833 | * |
||
834 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
835 | * that implied usage of methods present since more recent version. |
||
836 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
837 | * |
||
838 | * @since 5.1.0 |
||
839 | * |
||
840 | * @return bool True = Akismet available. False = Aksimet not available. |
||
841 | */ |
||
842 | public static function is_akismet_active() { |
||
856 | |||
857 | /** |
||
858 | * Is Jetpack in development (offline) mode? |
||
859 | */ |
||
860 | public static function is_development_mode() { |
||
881 | |||
882 | /** |
||
883 | * Whether Jetpack's version maps to a public release, or a development version. |
||
884 | */ |
||
885 | public static function is_development_version() { |
||
900 | |||
901 | /** |
||
902 | * Check whether or not a Jetpack module is active. |
||
903 | * |
||
904 | * @param string $module The slug of a Jetpack module. |
||
905 | * @return bool |
||
906 | * |
||
907 | * @static |
||
908 | */ |
||
909 | public static function is_module_active( $module ) { |
||
912 | |||
913 | public static function is_module( $module ) { |
||
916 | |||
917 | /** |
||
918 | * Implemented since there is no core is multi network function |
||
919 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
920 | * |
||
921 | * @since 3.3 |
||
922 | * @return boolean |
||
923 | */ |
||
924 | public static function is_multi_network() { |
||
939 | |||
940 | /** |
||
941 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
942 | * |
||
943 | * @param string $option |
||
944 | * @return boolean |
||
945 | */ |
||
946 | public function is_multisite( $option ) { |
||
949 | |||
950 | /** |
||
951 | * Whether the site is currently onboarding or not. |
||
952 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
953 | * |
||
954 | * @since 5.8 |
||
955 | * |
||
956 | * @access public |
||
957 | * @static |
||
958 | * |
||
959 | * @return bool True if the site is currently onboarding, false otherwise |
||
960 | */ |
||
961 | public static function is_onboarding() { |
||
964 | |||
965 | /** |
||
966 | * Checks whether a specific plugin is active. |
||
967 | * |
||
968 | * We don't want to store these in a static variable, in case |
||
969 | * there are switch_to_blog() calls involved. |
||
970 | */ |
||
971 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
974 | |||
975 | /** |
||
976 | * Checks for whether Jetpack Rewind is enabled. |
||
977 | * Will return true if the state of Rewind is anything except "unavailable". |
||
978 | * @return bool|int|mixed |
||
979 | */ |
||
980 | public static function is_rewind_enabled() { |
||
999 | |||
1000 | /** |
||
1001 | * Get back if the current site is single user site. |
||
1002 | * |
||
1003 | * @return bool |
||
1004 | */ |
||
1005 | public static function is_single_user_site() { |
||
1014 | |||
1015 | /** |
||
1016 | * Checks whether the home and siteurl specifically are whitelisted |
||
1017 | * Written so that we don't have re-check $key and $value params every time |
||
1018 | * we want to check if this site is whitelisted, for example in footer.php |
||
1019 | * |
||
1020 | * @since 3.8.0 |
||
1021 | * @return bool True = already whitelisted False = not whitelisted |
||
1022 | */ |
||
1023 | public static function is_staging_site() { |
||
1082 | |||
1083 | /** |
||
1084 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1085 | */ |
||
1086 | public static function is_user_connected( $user_id = false ) { |
||
1094 | |||
1095 | /** |
||
1096 | * Finds out if a site is using a version control system. |
||
1097 | * @return string ( '1' | '0' ) |
||
1098 | **/ |
||
1099 | public static function is_version_controlled() { |
||
1103 | |||
1104 | /** |
||
1105 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1106 | * @param bool $option |
||
1107 | * @return string |
||
1108 | */ |
||
1109 | public function jetpack_main_network_site_option( $option ) { |
||
1112 | |||
1113 | /** |
||
1114 | * Checks whether or not TOS has been agreed upon. |
||
1115 | * Will return true if a user has clicked to register, or is already connected. |
||
1116 | */ |
||
1117 | public static function jetpack_tos_agreed() { |
||
1120 | |||
1121 | /** |
||
1122 | * Loads a view file from the views |
||
1123 | * |
||
1124 | * Data passed in with the $data parameter will be available in the |
||
1125 | * template file as $data['value'] |
||
1126 | * |
||
1127 | * @param string $template - Template file to load |
||
1128 | * @param array $data - Any data to pass along to the template |
||
1129 | * @return boolean - If template file was found |
||
1130 | **/ |
||
1131 | public function load_view( $template, $data = array() ) { |
||
1142 | |||
1143 | /** |
||
1144 | * Network Name. |
||
1145 | */ |
||
1146 | static function network_name( $option = null ) { |
||
1150 | /** |
||
1151 | * Does the network allow new user and site registrations. |
||
1152 | * @return string |
||
1153 | */ |
||
1154 | static function network_allow_new_registrations( $option = null ) { |
||
1157 | /** |
||
1158 | * Does the network allow admins to add new users. |
||
1159 | * @return boolian |
||
1160 | */ |
||
1161 | static function network_add_new_users( $option = null ) { |
||
1164 | /** |
||
1165 | * File upload psace left per site in MB. |
||
1166 | * -1 means NO LIMIT. |
||
1167 | * @return number |
||
1168 | */ |
||
1169 | static function network_site_upload_space( $option = null ) { |
||
1173 | |||
1174 | /** |
||
1175 | * Network allowed file types. |
||
1176 | * @return string |
||
1177 | */ |
||
1178 | static function network_upload_file_types( $option = null ) { |
||
1181 | |||
1182 | /** |
||
1183 | * Maximum file upload size set by the network. |
||
1184 | * @return number |
||
1185 | */ |
||
1186 | static function network_max_upload_file_size( $option = null ) { |
||
1190 | |||
1191 | /** |
||
1192 | * Lets us know if a site allows admins to manage the network. |
||
1193 | * @return array |
||
1194 | */ |
||
1195 | static function network_enable_administration_menus( $option = null ) { |
||
1198 | |||
1199 | /** |
||
1200 | * Normalizes a url by doing three things: |
||
1201 | * - Strips protocol |
||
1202 | * - Strips www |
||
1203 | * - Adds a trailing slash |
||
1204 | * |
||
1205 | * @since 4.4.0 |
||
1206 | * @param string $url |
||
1207 | * @return WP_Error|string |
||
1208 | */ |
||
1209 | public static function normalize_url_protocol_agnostic( $url ) { |
||
1219 | |||
1220 | /** |
||
1221 | * Sets a minimum request timeout, and returns the current timeout |
||
1222 | * |
||
1223 | * @since 5.4 |
||
1224 | **/ |
||
1225 | public static function set_min_time_limit( $min_timeout ) { |
||
1233 | |||
1234 | public static function staticize_subdomain( $url ) { |
||
1269 | |||
1270 | static function translate_current_user_to_role() { |
||
1279 | |||
1280 | static function translate_user_to_role( $user ) { |
||
1289 | |||
1290 | static function translate_role_to_cap( $role ) { |
||
1297 | |||
1298 | /** |
||
1299 | * Returns the Jetpack XML-RPC API |
||
1300 | * |
||
1301 | * @return string |
||
1302 | */ |
||
1303 | public static function xmlrpc_api_url() { |
||
1307 | } |
||
1308 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.