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_Gutenberg 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_Gutenberg, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Jetpack_Gutenberg { |
||
| 46 | |||
| 47 | // BLOCKS |
||
| 48 | private static $default_blocks = array( |
||
| 49 | 'map', |
||
| 50 | 'markdown', |
||
| 51 | 'simple-payments', |
||
| 52 | 'related-posts', |
||
| 53 | 'contact-form', |
||
| 54 | 'field-text', |
||
| 55 | 'field-name', |
||
| 56 | 'field-email', |
||
| 57 | 'field-url', |
||
| 58 | 'field-date', |
||
| 59 | 'field-telephone', |
||
| 60 | 'field-textarea', |
||
| 61 | 'field-checkbox', |
||
| 62 | 'field-checkbox-multiple', |
||
| 63 | 'field-radio', |
||
| 64 | 'field-select', |
||
| 65 | 'subscriptions', |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array Array of blocks information. |
||
| 70 | * |
||
| 71 | * For each block we have information about the availability for the current user |
||
| 72 | */ |
||
| 73 | private static $blocks = array(); |
||
| 74 | |||
| 75 | private static $availability = array(); |
||
| 76 | |||
| 77 | // PLUGINS |
||
| 78 | private static $default_plugins = array( |
||
| 79 | 'publicize', |
||
| 80 | 'shortlinks', |
||
| 81 | ); |
||
| 82 | /** |
||
| 83 | * @var array Array of plugins information. |
||
| 84 | * |
||
| 85 | * For each block we have information about the availability for the current user |
||
| 86 | */ |
||
| 87 | private static $plugins = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array Array of extensions we will be registering. |
||
| 91 | */ |
||
| 92 | private static $registered = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add a block to the list of blocks to be registered. |
||
| 96 | * |
||
| 97 | * @param string $slug Slug of the block. |
||
| 98 | * @param array $args Arguments that are passed into the register_block_type. |
||
| 99 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
| 100 | */ |
||
| 101 | public static function register( $slug, $args, $availability ) { |
||
| 102 | $sanitized_slug = sanitize_title_with_dashes( $slug ); |
||
| 103 | self::$registered[ $sanitized_slug ] = array( 'args' => $args, 'availability' => $availability ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Register all Jetpack blocks available. |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public static function init() { |
||
| 112 | if ( ! self::is_gutenberg_available() ) { |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ( ! self::should_load() ) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ( Jetpack_Constants::is_true( 'REST_API_REQUEST' ) ) { |
||
| 121 | // We defer the loading of the blocks until we have a better scope in reset requests. |
||
| 122 | add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'load' ), 10, 3 ); |
||
| 123 | return; |
||
| 124 | } |
||
| 125 | self::load(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @deprecated |
||
| 130 | */ |
||
| 131 | static function load_blocks() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add a block to the list of blocks to be registered. |
||
| 137 | * |
||
| 138 | * @param string $slug Slug of the block. |
||
| 139 | * @param array $args Arguments that are passed into the register_block_type. |
||
| 140 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
| 141 | * |
||
| 142 | * @deprecated |
||
| 143 | */ |
||
| 144 | static function add_block( $type, $args, $availability ) { |
||
| 147 | |||
| 148 | static function load( $response = null, $handler = null, $request = null ) { |
||
| 149 | $is_availability_endpoint_beta = ! is_null( $request ) && $request->get_param( 'beta' ) && wp_endswith( $request->get_route(), 'gutenberg/available-extensions' ); |
||
| 188 | |||
| 189 | static function is_registered( $slug ) { |
||
| 192 | |||
| 193 | static function get_registered_args( $slug ) { |
||
| 199 | |||
| 200 | static function is_available( $slug ) { |
||
| 206 | |||
| 207 | |||
| 208 | static function get_extension_availability( $slug ) { |
||
| 231 | |||
| 232 | static function set_blocks_availability() { |
||
| 237 | |||
| 238 | static function set_plugins_availability() { |
||
| 243 | |||
| 244 | static function register_blocks() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
| 265 | * |
||
| 266 | * @return string The Gutenberg extensions directory |
||
| 267 | */ |
||
| 268 | public static function get_blocks_directory() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Checks for a given .json file in the blocks folder. |
||
| 281 | * |
||
| 282 | * @param $preset The name of the .json file to look for. |
||
| 283 | * |
||
| 284 | * @return bool True if the file is found. |
||
| 285 | */ |
||
| 286 | public static function preset_exists( $preset ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Decodes JSON loaded from a preset file in the blocks folder |
||
| 292 | * |
||
| 293 | * @param $preset The name of the .json file to load. |
||
| 294 | * |
||
| 295 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
| 296 | */ |
||
| 297 | public static function get_preset( $preset ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Filters the results of `apply_filter( 'jetpack_set_available_blocks', array() )` |
||
| 303 | * using the merged contents of `blocks-manifest.json` ( $preset_blocks ) |
||
| 304 | * and self::$jetpack_blocks ( $internal_blocks ) |
||
| 305 | * |
||
| 306 | * @param $blocks The default list. |
||
| 307 | * |
||
| 308 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
| 309 | */ |
||
| 310 | public static function jetpack_set_available_blocks( $blocks ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @deprecated |
||
| 327 | * @return array A list of block-availability information, eg: [ "publicize" => ["available" => true ], "markdown" => [ "available" => false, "unavailable_reason" => 'missing_module' ] ] |
||
| 328 | */ |
||
| 329 | public static function get_block_availability() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return array A list of block and plugins and their availablity status |
||
| 335 | */ |
||
| 336 | public static function get_availability() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Check if Gutenberg editor is available |
||
| 346 | * |
||
| 347 | * @since 6.7.0 |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public static function is_gutenberg_available() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Check whether conditions indicate Gutenberg blocks should be loaded |
||
| 357 | * |
||
| 358 | * Loading blocks is enabled by default and may be disabled via filter: |
||
| 359 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
| 360 | * |
||
| 361 | * @since 6.7.0 |
||
| 362 | * @deprecated |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public static function should_load_blocks() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
| 371 | * |
||
| 372 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
| 373 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
| 374 | * |
||
| 375 | * @since 6.9.0 |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public static function should_load() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Only enqueue block assets when needed. |
||
| 396 | * |
||
| 397 | * @param string $type slug of the block. |
||
| 398 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Check if an asset exists for a block. |
||
| 434 | * |
||
| 435 | * @param string $file Path of the file we are looking for. |
||
| 436 | * |
||
| 437 | * @return bool $block_has_asset Does the file exist. |
||
| 438 | */ |
||
| 439 | public static function block_has_asset( $file ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 445 | * |
||
| 446 | * @param string $file Path of the file we are looking for. |
||
| 447 | * |
||
| 448 | * @return string $script_version Version number. |
||
| 449 | */ |
||
| 450 | public static function get_asset_version( $file ) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Load Gutenberg editor assets |
||
| 458 | * |
||
| 459 | * @since 6.7.0 |
||
| 460 | * |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | public static function enqueue_block_editor_assets() { |
||
| 525 | } |
||
| 526 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.