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_Memberships 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_Memberships, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Jetpack_Memberships { |
||
| 16 | /** |
||
| 17 | * CSS class prefix to use in the styling. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public static $css_classname_prefix = 'jetpack-memberships'; |
||
| 22 | /** |
||
| 23 | * Our CPT type for the product (plan). |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public static $post_type_plan = 'jp_mem_plan'; |
||
| 28 | /** |
||
| 29 | * Option that will store currently set up account (Stripe etc) id for memberships. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public static $connected_account_id_option_name = 'jetpack-memberships-connected-account-id'; |
||
| 34 | /** |
||
| 35 | * Button block type to use. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private static $button_block_name = 'recurring-payments'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * These are defaults for wp_kses ran on the membership button. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $tags_allowed_in_the_button = array( 'br' => array() ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The minimum required plan for this Gutenberg block. |
||
| 50 | * |
||
| 51 | * @var string Plan slug |
||
| 52 | */ |
||
| 53 | private static $required_plan; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Track recurring payments block registration. |
||
| 57 | * |
||
| 58 | * @var boolean True if block registration has been executed. |
||
| 59 | */ |
||
| 60 | private static $has_registered_block = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Classic singleton pattern |
||
| 64 | * |
||
| 65 | * @var Jetpack_Memberships |
||
| 66 | */ |
||
| 67 | private static $instance; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Currencies we support and Stripe's minimum amount for a transaction in that currency. |
||
| 71 | * |
||
| 72 | * @link https://stripe.com/docs/currencies#minimum-and-maximum-charge-amounts |
||
| 73 | * |
||
| 74 | * List has to be in with `SUPPORTED_CURRENCIES` in extensions/shared/currencies.js and |
||
| 75 | * `Memberships_Product::SUPPORTED_CURRENCIES` in the WP.com memberships library. |
||
| 76 | */ |
||
| 77 | const SUPPORTED_CURRENCIES = array( |
||
| 78 | 'USD' => 0.5, |
||
| 79 | 'AUD' => 0.5, |
||
| 80 | 'BRL' => 0.5, |
||
| 81 | 'CAD' => 0.5, |
||
| 82 | 'CHF' => 0.5, |
||
| 83 | 'DKK' => 2.5, |
||
| 84 | 'EUR' => 0.5, |
||
| 85 | 'GBP' => 0.3, |
||
| 86 | 'HKD' => 4.0, |
||
| 87 | 'INR' => 0.5, |
||
| 88 | 'JPY' => 50, |
||
| 89 | 'MXN' => 10, |
||
| 90 | 'NOK' => 3.0, |
||
| 91 | 'NZD' => 0.5, |
||
| 92 | 'PLN' => 2.0, |
||
| 93 | 'SEK' => 3.0, |
||
| 94 | 'SGD' => 0.5, |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Jetpack_Memberships constructor. |
||
| 99 | */ |
||
| 100 | private function __construct() {} |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The actual constructor initializing the object. |
||
| 104 | * |
||
| 105 | * @return Jetpack_Memberships |
||
| 106 | */ |
||
| 107 | View Code Duplication | public static function get_instance() { |
|
| 117 | /** |
||
| 118 | * Get the map that defines the shape of CPT post. keys are names of fields and |
||
| 119 | * 'meta' is the name of actual WP post meta field that corresponds. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | private static function get_plan_property_mapping() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Inits further hooks on init hook. |
||
| 138 | */ |
||
| 139 | private function register_init_hook() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Actual hooks initializing on init. |
||
| 146 | */ |
||
| 147 | public function init_hook_action() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Sets up the custom post types for the module. |
||
| 155 | */ |
||
| 156 | private function setup_cpts() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Allows custom post types to be used by REST API. |
||
| 192 | * |
||
| 193 | * @param array $post_types - other post types. |
||
| 194 | * |
||
| 195 | * @see hook 'rest_api_allowed_post_types' |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function allow_rest_api_types( $post_types ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Allows custom meta fields to sync. |
||
| 206 | * |
||
| 207 | * @param array $post_meta - previously changet post meta. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function allow_sync_post_meta( $post_meta ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * This returns meta attribute of passet array. |
||
| 221 | * Used for array functions. |
||
| 222 | * |
||
| 223 | * @param array $map - stuff. |
||
| 224 | * |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | public function return_meta( $map ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Renders a preview of the Recurring Payment button, which is not hooked |
||
| 233 | * up to the subscription url. Used to preview the block on the frontend |
||
| 234 | * for site editors when Stripe has not been connected. |
||
| 235 | * |
||
| 236 | * @param array $attrs - attributes in the shortcode. |
||
| 237 | * @param string $content - Recurring Payment block content. |
||
|
|
|||
| 238 | * |
||
| 239 | * @return string|void |
||
| 240 | */ |
||
| 241 | public function render_button_preview( $attrs, $content = null ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determines whether the button preview should be rendered. Returns true |
||
| 252 | * if the user has editing permissions, the button is not configured correctly |
||
| 253 | * (because it requires a plan upgrade or Stripe connection), and the |
||
| 254 | * button is a child of a Premium Content block. |
||
| 255 | * |
||
| 256 | * @param WP_Block $block Recurring Payments block instance. |
||
| 257 | * |
||
| 258 | * @return boolean |
||
| 259 | */ |
||
| 260 | public function should_render_button_preview( $block ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Callback that parses the membership purchase shortcode. |
||
| 280 | * |
||
| 281 | * @param array $attributes - attributes in the shortcode. `id` here is the CPT id of the plan. |
||
| 282 | * @param string $content - Recurring Payment block content. |
||
| 283 | * @param WP_Block $block - Recurring Payment block instance. |
||
| 284 | * |
||
| 285 | * @return string|void |
||
| 286 | */ |
||
| 287 | public function render_button( $attributes, $content, $block ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Builds subscription URL for this membership using the current blog and |
||
| 321 | * supplied plan IDs. |
||
| 322 | * |
||
| 323 | * @param integer $plan_id - Unique ID for the plan being subscribed to. |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function get_subscription_url( $plan_id ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Renders a deprecated legacy version of the button HTML. |
||
| 343 | * |
||
| 344 | * @param array $attrs - Array containing the Recurring Payment block attributes. |
||
| 345 | * @param integer $plan_id - Unique plan ID the membership is for. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function deprecated_render_button_v1( $attrs, $plan_id ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get current blog id. |
||
| 394 | * |
||
| 395 | * @return int |
||
| 396 | */ |
||
| 397 | public static function get_blog_id() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get the id of the connected payment acount (Stripe etc). |
||
| 407 | * |
||
| 408 | * @return int|void |
||
| 409 | */ |
||
| 410 | public static function get_connected_account_id() { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Determines whether the current user can edit. |
||
| 416 | * |
||
| 417 | * @return bool Whether the user can edit. |
||
| 418 | */ |
||
| 419 | public static function user_can_edit() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Whether Recurring Payments are enabled. True if the block |
||
| 427 | * is supported by the site's plan, or if it is a Jetpack site |
||
| 428 | * and the feature to enable upgrade nudges is active. |
||
| 429 | * |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | public static function is_enabled_jetpack_recurring_payments() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Whether the site's plan supports the Recurring Payments block. |
||
| 445 | */ |
||
| 446 | public static function is_supported_jetpack_recurring_payments() { |
||
| 447 | // For WPCOM sites. |
||
| 448 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) { |
||
| 449 | $site_id = get_current_blog_id(); |
||
| 450 | return has_any_blog_stickers( array( 'personal-plan', 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id ); |
||
| 451 | } |
||
| 452 | // For Jetpack sites. |
||
| 453 | return ( |
||
| 454 | Jetpack::is_active() && |
||
| 455 | Jetpack_Plan::supports( 'recurring-payments' ) |
||
| 456 | ); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Register the Recurring Payments Gutenberg block |
||
| 461 | */ |
||
| 462 | public function register_gutenberg_block() { |
||
| 493 | } |
||
| 494 | Jetpack_Memberships::get_instance(); |
||
| 495 |
This check looks for
@paramannotations 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.