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:
| 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 | * Callback that parses the membership purchase shortcode. |
||
| 232 | * |
||
| 233 | * @param array $attrs - attributes in the shortcode. `id` here is the CPT id of the plan. |
||
| 234 | * @param string $content - Recurring Payment block content. |
||
|
|
|||
| 235 | * |
||
| 236 | * @return string|void |
||
| 237 | */ |
||
| 238 | public function render_button( $attrs, $content = null ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Builds subscription URL for this membership using the current blog and |
||
| 267 | * supplied plan IDs. |
||
| 268 | * |
||
| 269 | * @param integer $plan_id - Unique ID for the plan being subscribed to. |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function get_subscription_url( $plan_id ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Renders a deprecated legacy version of the button HTML. |
||
| 289 | * |
||
| 290 | * @param array $attrs - Array containing the Recurring Payment block attributes. |
||
| 291 | * @param integer $plan_id - Unique plan ID the membership is for. |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function deprecated_render_button_v1( $attrs, $plan_id ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get current blog id. |
||
| 340 | * |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | public static function get_blog_id() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the id of the connected payment acount (Stripe etc). |
||
| 353 | * |
||
| 354 | * @return int|void |
||
| 355 | */ |
||
| 356 | public static function get_connected_account_id() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Whether Recurring Payments are enabled. |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public static function is_enabled_jetpack_recurring_payments() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Register the Recurring Payments Gutenberg block |
||
| 382 | */ |
||
| 383 | public function register_gutenberg_block() { |
||
| 411 | } |
||
| 412 | Jetpack_Memberships::get_instance(); |
||
| 413 |
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.