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 |
||
| 70 | class Jetpack_Gutenberg { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Only these extensions can be registered. Used to control availability of beta blocks. |
||
| 74 | * |
||
| 75 | * @var array Extensions whitelist |
||
| 76 | */ |
||
| 77 | private static $extensions = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Keeps track of the reasons why a given extension is unavailable. |
||
| 81 | * |
||
| 82 | * @var array Extensions availability information |
||
| 83 | */ |
||
| 84 | private static $availability = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Since there is no `register_plugin()` counterpart to `register_block_type()` in Gutenberg, |
||
| 88 | * we have to keep track of plugin registration ourselves |
||
| 89 | * |
||
| 90 | * @var array Plugin registry |
||
| 91 | */ |
||
| 92 | private static $registered_plugins = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Keeps track of the generic extensions registered |
||
| 96 | * |
||
| 97 | * @var array Plugin registry |
||
| 98 | */ |
||
| 99 | private static $registered_extensions = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Prepend the 'jetpack/' prefix to a block name |
||
| 103 | * |
||
| 104 | * @param string $block_name The block name. |
||
| 105 | * |
||
| 106 | * @return string The prefixed block name. |
||
| 107 | */ |
||
| 108 | private static function prepend_block_prefix( $block_name ) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Whether two arrays share at least one item |
||
| 114 | * |
||
| 115 | * @param array $a An array. |
||
| 116 | * @param array $b Another array. |
||
| 117 | * |
||
| 118 | * @return boolean True if $a and $b share at least one item |
||
| 119 | */ |
||
| 120 | protected static function share_items( $a, $b ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Register a block |
||
| 126 | * |
||
| 127 | * If the block isn't whitelisted, set its unavailability reason instead. |
||
| 128 | * |
||
| 129 | * @param string $slug Slug of the block. |
||
| 130 | * @param array $args Arguments that are passed into register_block_type(). |
||
| 131 | */ |
||
| 132 | public static function register_block( $slug, $args ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Register a plugin |
||
| 146 | * |
||
| 147 | * If the plugin isn't whitelisted, set its unavailability reason instead. |
||
| 148 | * |
||
| 149 | * @param string $slug Slug of the plugin. |
||
| 150 | */ |
||
| 151 | public static function register_plugin( $slug ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Register a generic extension |
||
| 161 | * |
||
| 162 | * If the extension isn't whitelisted, set its unavailability reason instead. |
||
| 163 | * |
||
| 164 | * @param string $slug Slug of the extension. |
||
| 165 | * @param callable $register_callback Callback used to register the extension. |
||
| 166 | */ |
||
| 167 | public static function register_extension( $slug, $register_callback ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Register a block |
||
| 178 | * |
||
| 179 | * @deprecated 7.0.0 Use register_block() instead |
||
| 180 | * |
||
| 181 | * @param string $slug Slug of the block. |
||
| 182 | * @param array $args Arguments that are passed into the register_block_type. |
||
| 183 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
| 184 | */ |
||
| 185 | public static function register( $slug, $args, $availability ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Set the reason why an extension (block or plugin) is unavailable |
||
| 195 | * |
||
| 196 | * @param string $slug Slug of the extension. |
||
| 197 | * @param string $reason A string representation of why the extension is unavailable. |
||
| 198 | */ |
||
| 199 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set up a whitelist of allowed block editor extensions |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public static function init() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Resets the class to its original state |
||
| 262 | * |
||
| 263 | * Used in unit tests |
||
| 264 | * |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | public static function reset() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
| 276 | * |
||
| 277 | * @return string The Gutenberg extensions directory |
||
| 278 | */ |
||
| 279 | public static function get_blocks_directory() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Checks for a given .json file in the blocks folder. |
||
| 292 | * |
||
| 293 | * @param string $preset The name of the .json file to look for. |
||
| 294 | * |
||
| 295 | * @return bool True if the file is found. |
||
| 296 | */ |
||
| 297 | public static function preset_exists( $preset ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Decodes JSON loaded from a preset file in the blocks folder |
||
| 303 | * |
||
| 304 | * @param string $preset The name of the .json file to load. |
||
| 305 | * |
||
| 306 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
| 307 | */ |
||
| 308 | public static function get_preset( $preset ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
| 314 | * |
||
| 315 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
| 316 | */ |
||
| 317 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get availability of each block / plugin. |
||
| 332 | * |
||
| 333 | * @return array A list of block and plugins and their availablity status |
||
| 334 | */ |
||
| 335 | public static function get_availability() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Check if Gutenberg editor is available |
||
| 366 | * |
||
| 367 | * @since 6.7.0 |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public static function is_gutenberg_available() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
| 377 | * |
||
| 378 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
| 379 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
| 380 | * |
||
| 381 | * @since 6.9.0 |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | public static function should_load() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Only enqueue block assets when needed. |
||
| 402 | * |
||
| 403 | * @param string $type slug of the block. |
||
| 404 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
||
| 405 | * |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Check if an asset exists for a block. |
||
| 440 | * |
||
| 441 | * @param string $file Path of the file we are looking for. |
||
| 442 | * |
||
| 443 | * @return bool $block_has_asset Does the file exist. |
||
| 444 | */ |
||
| 445 | public static function block_has_asset( $file ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 451 | * |
||
| 452 | * @param string $file Path of the file we are looking for. |
||
| 453 | * |
||
| 454 | * @return string $script_version Version number. |
||
| 455 | */ |
||
| 456 | public static function get_asset_version( $file ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Load Gutenberg editor assets |
||
| 464 | * |
||
| 465 | * @since 6.7.0 |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | public static function enqueue_block_editor_assets() { |
||
| 531 | } |
||
| 532 |