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_Calypsoify 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_Calypsoify, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Jetpack_Calypsoify { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Singleton instance of `Jetpack_Calypsoify`. |
||
| 13 | * |
||
| 14 | * @var object |
||
| 15 | */ |
||
| 16 | public static $instance = false; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Is Calypsoify enabled, based on any value of `calypsoify` user meta. |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | public $is_calypsoify_enabled = false; |
||
| 24 | |||
| 25 | private function __construct() { |
||
| 28 | |||
| 29 | public static function getInstance() { |
||
| 36 | |||
| 37 | public function setup() { |
||
| 51 | |||
| 52 | public function setup_admin() { |
||
| 72 | |||
| 73 | public function admin_color_override( $color ) { |
||
| 76 | |||
| 77 | public function mock_masterbar_activation() { |
||
| 81 | |||
| 82 | public function remove_core_menus() { |
||
| 106 | |||
| 107 | public function add_custom_menus() { |
||
| 130 | |||
| 131 | public function enqueue() { |
||
| 144 | |||
| 145 | public function enqueue_for_gutenberg() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Inserts Sidebar HTML |
||
| 163 | * |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public function insert_sidebar_html() { |
||
| 180 | |||
| 181 | public function modify_masterbar() { |
||
| 197 | |||
| 198 | private function installed_plugins_icon() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the Calypso domain that originated the current request. |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | private function get_calypso_origin() { |
||
| 210 | $origin = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com'; |
||
| 211 | $allowed = array( |
||
| 212 | 'http://calypso.localhost:3000', |
||
| 213 | 'http://127.0.0.1:41050', // Desktop App |
||
| 214 | 'https://wpcalypso.wordpress.com', |
||
| 215 | 'https://horizon.wordpress.com', |
||
| 216 | 'https://wordpress.com', |
||
| 217 | ); |
||
| 218 | return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com'; |
||
| 219 | |||
| 220 | function get_site_suffix() { |
||
|
|
|||
| 221 | if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
||
| 222 | $site_suffix = Jetpack::build_raw_urls( home_url() ); |
||
| 223 | } elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
||
| 224 | $site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
||
| 225 | } |
||
| 226 | |||
| 227 | if ( $site_suffix ) { |
||
| 228 | return "/${site_suffix}"; |
||
| 229 | } |
||
| 230 | return ''; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the site slug suffix to be used as part of the Calypso URLs. It already |
||
| 236 | * includes the slash separator at the beginning. |
||
| 237 | * |
||
| 238 | * @example "https://wordpress.com/block-editor" . $this->get_site_suffix() |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | private function get_site_suffix() { |
||
| 243 | if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
||
| 244 | $site_suffix = Jetpack::build_raw_urls( home_url() ); |
||
| 245 | } elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
||
| 246 | $site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
||
| 247 | } |
||
| 248 | |||
| 249 | if ( $site_suffix ) { |
||
| 250 | return "/${site_suffix}"; |
||
| 251 | } |
||
| 252 | return ''; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the Calypso URL that displays either the current post type list (if no args |
||
| 257 | * are supplied) or the classic editor for the current post (if a post ID is supplied). |
||
| 258 | * |
||
| 259 | * @param int|null $post_id |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function get_calypso_url( $post_id = null ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the URL to be used on the block editor close button for going back to the |
||
| 283 | * Calypso post list. |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function get_close_gutenberg_url() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor. |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function get_switch_to_classic_editor_url() { |
||
| 303 | |||
| 304 | public function check_param() { |
||
| 313 | |||
| 314 | public function check_page() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Return whether a post type should display the Gutenberg/block editor. |
||
| 327 | * |
||
| 328 | * @since 6.7.0 |
||
| 329 | */ |
||
| 330 | public function is_post_type_gutenberg( $post_type ) { |
||
| 333 | |||
| 334 | public function is_page_gutenberg() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Attach a WP_List_Table views filter to all screens. |
||
| 365 | */ |
||
| 366 | public function attach_views_filter( $current_screen ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Remove the parentheses from list table view counts when Calypsofied. |
||
| 372 | * |
||
| 373 | * @param array $views Array of views. See: WP_List_Table::get_views(). |
||
| 374 | * @return array Filtered views. |
||
| 375 | */ |
||
| 376 | public function filter_views( $views ) { |
||
| 383 | } |
||
| 384 | |||
| 385 | $Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance(); |
||
| 386 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.