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 //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
73 | class Jetpack_Gutenberg { |
||
74 | |||
75 | /** |
||
76 | * Only these extensions can be registered. Used to control availability of beta blocks. |
||
77 | * |
||
78 | * @var array Extensions whitelist |
||
79 | */ |
||
80 | private static $extensions = array(); |
||
81 | |||
82 | /** |
||
83 | * Keeps track of the reasons why a given extension is unavailable. |
||
84 | * |
||
85 | * @var array Extensions availability information |
||
86 | */ |
||
87 | private static $availability = array(); |
||
88 | |||
89 | /** |
||
90 | * Prepend the 'jetpack/' prefix to a block name |
||
91 | * |
||
92 | * @param string $block_name The block name. |
||
93 | * |
||
94 | * @return string The prefixed block name. |
||
95 | */ |
||
96 | private static function prepend_block_prefix( $block_name ) { |
||
99 | |||
100 | /** |
||
101 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
||
102 | * |
||
103 | * @param string $extension_name The extension name. |
||
104 | * |
||
105 | * @return string The unprefixed extension name. |
||
106 | */ |
||
107 | private static function remove_extension_prefix( $extension_name ) { |
||
113 | |||
114 | /** |
||
115 | * Whether two arrays share at least one item |
||
116 | * |
||
117 | * @param array $a An array. |
||
118 | * @param array $b Another array. |
||
119 | * |
||
120 | * @return boolean True if $a and $b share at least one item |
||
121 | */ |
||
122 | protected static function share_items( $a, $b ) { |
||
125 | |||
126 | /** |
||
127 | * Register a block |
||
128 | * |
||
129 | * @deprecated 7.1.0 Use jetpack_register_block() instead |
||
130 | * |
||
131 | * @param string $slug Slug of the block. |
||
132 | * @param array $args Arguments that are passed into register_block_type(). |
||
133 | */ |
||
134 | public static function register_block( $slug, $args ) { |
||
139 | |||
140 | /** |
||
141 | * Register a plugin |
||
142 | * |
||
143 | * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead |
||
144 | * |
||
145 | * @param string $slug Slug of the plugin. |
||
146 | */ |
||
147 | public static function register_plugin( $slug ) { |
||
152 | |||
153 | /** |
||
154 | * Register a block |
||
155 | * |
||
156 | * @deprecated 7.0.0 Use jetpack_register_block() instead |
||
157 | * |
||
158 | * @param string $slug Slug of the block. |
||
159 | * @param array $args Arguments that are passed into the register_block_type. |
||
160 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
161 | */ |
||
162 | public static function register( $slug, $args, $availability ) { |
||
171 | |||
172 | /** |
||
173 | * Set a (non-block) extension as available |
||
174 | * |
||
175 | * @param string $slug Slug of the extension. |
||
176 | */ |
||
177 | public static function set_extension_available( $slug ) { |
||
180 | |||
181 | /** |
||
182 | * Set the reason why an extension (block or plugin) is unavailable |
||
183 | * |
||
184 | * @param string $slug Slug of the extension. |
||
185 | * @param string $reason A string representation of why the extension is unavailable. |
||
186 | */ |
||
187 | public static function set_extension_unavailable( $slug, $reason ) { |
||
190 | |||
191 | /** |
||
192 | * Set the reason why an extension (block or plugin) is unavailable |
||
193 | * |
||
194 | * @deprecated 7.1.0 Use set_extension_unavailable() instead |
||
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 ) { |
||
204 | |||
205 | /** |
||
206 | * Set up a whitelist of allowed block editor extensions |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | public static function init() { |
||
257 | |||
258 | /** |
||
259 | * Resets the class to its original state |
||
260 | * |
||
261 | * Used in unit tests |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public static function reset() { |
||
269 | |||
270 | /** |
||
271 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
272 | * |
||
273 | * @return string The Gutenberg extensions directory |
||
274 | */ |
||
275 | public static function get_blocks_directory() { |
||
285 | |||
286 | /** |
||
287 | * Checks for a given .json file in the blocks folder. |
||
288 | * |
||
289 | * @param string $preset The name of the .json file to look for. |
||
290 | * |
||
291 | * @return bool True if the file is found. |
||
292 | */ |
||
293 | public static function preset_exists( $preset ) { |
||
296 | |||
297 | /** |
||
298 | * Decodes JSON loaded from a preset file in the blocks folder |
||
299 | * |
||
300 | * @param string $preset The name of the .json file to load. |
||
301 | * |
||
302 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
303 | */ |
||
304 | public static function get_preset( $preset ) { |
||
310 | |||
311 | /** |
||
312 | * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
313 | * |
||
314 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
315 | */ |
||
316 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
||
328 | |||
329 | /** |
||
330 | * Returns a diff from a combined list of whitelisted extensions and extensions determined to be excluded |
||
331 | * |
||
332 | * @param array $whitelisted_extensions An array of whitelisted extensions. |
||
333 | * |
||
334 | * @return array A list of blocks: eg array( 'publicize', 'markdown' ) |
||
335 | */ |
||
336 | public static function get_available_extensions( $whitelisted_extensions = null ) { |
||
337 | $exclusions = get_option( 'jetpack_excluded_extensions', array() ); |
||
338 | $whitelisted_extensions = is_null( $whitelisted_extensions ) ? self::get_jetpack_gutenberg_extensions_whitelist() : $whitelisted_extensions; |
||
339 | |||
340 | return array_diff( $whitelisted_extensions, $exclusions ); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get availability of each block / plugin. |
||
345 | * |
||
346 | * @return array A list of block and plugins and their availablity status |
||
347 | */ |
||
348 | public static function get_availability() { |
||
379 | |||
380 | /** |
||
381 | * Check if an extension/block is already registered |
||
382 | * |
||
383 | * @since 7.2 |
||
384 | * |
||
385 | * @param string $slug Name of extension/block to check. |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | public static function is_registered( $slug ) { |
||
392 | |||
393 | /** |
||
394 | * Check if Gutenberg editor is available |
||
395 | * |
||
396 | * @since 6.7.0 |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | public static function is_gutenberg_available() { |
||
403 | |||
404 | /** |
||
405 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
406 | * |
||
407 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
408 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
409 | * |
||
410 | * @since 6.9.0 |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public static function should_load() { |
||
428 | |||
429 | /** |
||
430 | * Only enqueue block assets when needed. |
||
431 | * |
||
432 | * @param string $type Slug of the block. |
||
433 | * @param array $script_dependencies Script dependencies. Will be merged with automatically |
||
434 | * detected script dependencies from the webpack build. |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
448 | |||
449 | /** |
||
450 | * Only enqueue block sytles when needed. |
||
451 | * |
||
452 | * @param string $type Slug of the block. |
||
453 | * |
||
454 | * @since 7.2.0 |
||
455 | * |
||
456 | * @return void |
||
457 | */ |
||
458 | public static function load_styles_as_required( $type ) { |
||
473 | |||
474 | /** |
||
475 | * Only enqueue block scripts when needed. |
||
476 | * |
||
477 | * @param string $type Slug of the block. |
||
478 | * @param array $dependencies Script dependencies. Will be merged with automatically |
||
479 | * detected script dependencies from the webpack build. |
||
480 | * |
||
481 | * @since 7.2.0 |
||
482 | * |
||
483 | * @return void |
||
484 | */ |
||
485 | public static function load_scripts_as_required( $type, $dependencies = array() ) { |
||
512 | |||
513 | /** |
||
514 | * Check if an asset exists for a block. |
||
515 | * |
||
516 | * @param string $file Path of the file we are looking for. |
||
517 | * |
||
518 | * @return bool $block_has_asset Does the file exist. |
||
519 | */ |
||
520 | public static function block_has_asset( $file ) { |
||
523 | |||
524 | /** |
||
525 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
526 | * |
||
527 | * @param string $file Path of the file we are looking for. |
||
528 | * |
||
529 | * @return string $script_version Version number. |
||
530 | */ |
||
531 | public static function get_asset_version( $file ) { |
||
536 | |||
537 | /** |
||
538 | * Load Gutenberg editor assets |
||
539 | * |
||
540 | * @since 6.7.0 |
||
541 | * |
||
542 | * @return void |
||
543 | */ |
||
544 | public static function enqueue_block_editor_assets() { |
||
605 | |||
606 | /** |
||
607 | * A workaround for setting i18n data for WordPress client-side i18n mechanism. |
||
608 | * We are not yet using dotorg language packs for the editor file, so this short-circuits |
||
609 | * the translation loading and feeds our JSON data directly into the translation getter. |
||
610 | * |
||
611 | * @param NULL $null not used. |
||
612 | * @param String $file the file path that is being loaded, ignored. |
||
613 | * @param String $handle the script handle. |
||
614 | * @return NULL|String the translation data only if we're working with our handle. |
||
615 | */ |
||
616 | public static function filter_pre_load_script_translations( $null, $file, $handle ) { |
||
623 | |||
624 | /** |
||
625 | * Some blocks do not depend on a specific module, |
||
626 | * and can consequently be loaded outside of the usual modules. |
||
627 | * We will look for such modules in the extensions/ directory. |
||
628 | * |
||
629 | * @since 7.1.0 |
||
630 | */ |
||
631 | public static function load_independent_blocks() { |
||
645 | } |
||
646 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.