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 | ||
| 13 | class Jetpack_Memberships { | ||
| 14 | /** | ||
| 15 | * CSS class prefix to use in the styling. | ||
| 16 | * | ||
| 17 | * @var string | ||
| 18 | */ | ||
| 19 | public static $css_classname_prefix = 'jetpack-memberships'; | ||
| 20 | /** | ||
| 21 | * Our CPT type for the product (plan). | ||
| 22 | * | ||
| 23 | * @var string | ||
| 24 | */ | ||
| 25 | public static $post_type_plan = 'jp_mem_plan'; | ||
| 26 | /** | ||
| 27 | * Option that will store currently set up account (Stripe etc) id for memberships. | ||
| 28 | * | ||
| 29 | * @var string | ||
| 30 | */ | ||
| 31 | public static $connected_account_id_option_name = 'jetpack-memberships-connected-account-id'; | ||
| 32 | /** | ||
| 33 | * Button block type to use. | ||
| 34 | * | ||
| 35 | * @var string | ||
| 36 | */ | ||
| 37 | private static $button_block_name = 'recurring-payments'; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * These are defaults for wp_kses ran on the membership button. | ||
| 41 | * | ||
| 42 | * @var array | ||
| 43 | */ | ||
| 44 | private static $tags_allowed_in_the_button = array( 'br' => array() ); | ||
| 45 | |||
| 46 | /** | ||
| 47 | * The minimum required plan for this Gutenberg block. | ||
| 48 | * | ||
| 49 | * @var string Plan slug | ||
| 50 | */ | ||
| 51 | private static $required_plan; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Track recurring payments block registration. | ||
| 55 | * | ||
| 56 | * @var boolean True if block registration has been executed. | ||
| 57 | */ | ||
| 58 | private static $has_registered_block = false; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Classic singleton pattern | ||
| 62 | * | ||
| 63 | * @var Jetpack_Memberships | ||
| 64 | */ | ||
| 65 | private static $instance; | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Jetpack_Memberships constructor. | ||
| 69 | */ | ||
| 70 | 	private function __construct() {} | ||
| 71 | |||
| 72 | /** | ||
| 73 | * The actual constructor initializing the object. | ||
| 74 | * | ||
| 75 | * @return Jetpack_Memberships | ||
| 76 | */ | ||
| 77 | View Code Duplication | 	public static function get_instance() { | |
| 87 | /** | ||
| 88 | * Get the map that defines the shape of CPT post. keys are names of fields and | ||
| 89 | * 'meta' is the name of actual WP post meta field that corresponds. | ||
| 90 | * | ||
| 91 | * @return array | ||
| 92 | */ | ||
| 93 | 	private static function get_plan_property_mapping() { | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Inits further hooks on init hook. | ||
| 108 | */ | ||
| 109 | 	private function register_init_hook() { | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Actual hooks initializing on init. | ||
| 116 | */ | ||
| 117 | 	public function init_hook_action() { | ||
| 122 | |||
| 123 | /** | ||
| 124 | * Sets up the custom post types for the module. | ||
| 125 | */ | ||
| 126 | 	private function setup_cpts() { | ||
| 159 | |||
| 160 | /** | ||
| 161 | * Allows custom post types to be used by REST API. | ||
| 162 | * | ||
| 163 | * @param array $post_types - other post types. | ||
| 164 | * | ||
| 165 | * @see hook 'rest_api_allowed_post_types' | ||
| 166 | * @return array | ||
| 167 | */ | ||
| 168 | 	public function allow_rest_api_types( $post_types ) { | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Allows custom meta fields to sync. | ||
| 176 | * | ||
| 177 | * @param array $post_meta - previously changet post meta. | ||
| 178 | * | ||
| 179 | * @return array | ||
| 180 | */ | ||
| 181 | 	public function allow_sync_post_meta( $post_meta ) { | ||
| 188 | |||
| 189 | /** | ||
| 190 | * This returns meta attribute of passet array. | ||
| 191 | * Used for array functions. | ||
| 192 | * | ||
| 193 | * @param array $map - stuff. | ||
| 194 | * | ||
| 195 | * @return mixed | ||
| 196 | */ | ||
| 197 | 	public function return_meta( $map ) { | ||
| 200 | /** | ||
| 201 | * Callback that parses the membership purchase shortcode. | ||
| 202 | * | ||
| 203 | * @param array $attrs - attributes in the shortcode. `id` here is the CPT id of the plan. | ||
| 204 | * @param string $content - Recurring Payment block content. | ||
|  | |||
| 205 | * | ||
| 206 | * @return string|void | ||
| 207 | */ | ||
| 208 | 	public function render_button( $attrs, $content = null ) { | ||
| 234 | |||
| 235 | /** | ||
| 236 | * Builds subscription URL for this membership using the current blog and | ||
| 237 | * supplied plan IDs. | ||
| 238 | * | ||
| 239 | * @param integer $plan_id - Unique ID for the plan being subscribed to. | ||
| 240 | * @return string | ||
| 241 | */ | ||
| 242 | 	public function get_subscription_url( $plan_id ) { | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Renders a deprecated legacy version of the button HTML. | ||
| 259 | * | ||
| 260 | * @param array $attrs - Array containing the Recurring Payment block attributes. | ||
| 261 | * @param integer $plan_id - Unique plan ID the membership is for. | ||
| 262 | * | ||
| 263 | * @return string | ||
| 264 | */ | ||
| 265 | 	public function deprecated_render_button_v1( $attrs, $plan_id ) { | ||
| 307 | |||
| 308 | /** | ||
| 309 | * Get current blog id. | ||
| 310 | * | ||
| 311 | * @return int | ||
| 312 | */ | ||
| 313 | 	public static function get_blog_id() { | ||
| 320 | |||
| 321 | /** | ||
| 322 | * Get the id of the connected payment acount (Stripe etc). | ||
| 323 | * | ||
| 324 | * @return int|void | ||
| 325 | */ | ||
| 326 | 	public static function get_connected_account_id() { | ||
| 329 | |||
| 330 | /** | ||
| 331 | * Whether Recurring Payments are enabled. | ||
| 332 | * | ||
| 333 | * @return bool | ||
| 334 | */ | ||
| 335 | 	public static function is_enabled_jetpack_recurring_payments() { | ||
| 349 | |||
| 350 | /** | ||
| 351 | * Register the Recurring Payments Gutenberg block | ||
| 352 | */ | ||
| 353 | 	public function register_gutenberg_block() { | ||
| 381 | } | ||
| 382 | Jetpack_Memberships::get_instance(); | ||
| 383 | 
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.