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 |
||
81 | class Jetpack_Gutenberg { |
||
82 | |||
83 | /** |
||
84 | * Only these extensions can be registered. Used to control availability of beta blocks. |
||
85 | * |
||
86 | * @var array Extensions whitelist |
||
87 | */ |
||
88 | private static $extensions = array(); |
||
89 | |||
90 | /** |
||
91 | * Keeps track of the reasons why a given extension is unavailable. |
||
92 | * |
||
93 | * @var array Extensions availability information |
||
94 | */ |
||
95 | private static $availability = array(); |
||
96 | |||
97 | /** |
||
98 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
||
99 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
||
100 | * optionally fall back to that. |
||
101 | * |
||
102 | * @param array $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
||
103 | * @param string $slug The slug of the block or plugin that has the gutenberg version requirement. |
||
104 | * |
||
105 | * @since 8.3.0 |
||
106 | * |
||
107 | * @return boolean True if the version of gutenberg required by the block or plugin is available. |
||
108 | */ |
||
109 | public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
||
149 | |||
150 | /** |
||
151 | * Prepend the 'jetpack/' prefix to a block name |
||
152 | * |
||
153 | * @param string $block_name The block name. |
||
154 | * |
||
155 | * @return string The prefixed block name. |
||
156 | */ |
||
157 | private static function prepend_block_prefix( $block_name ) { |
||
160 | |||
161 | /** |
||
162 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
||
163 | * |
||
164 | * @param string $extension_name The extension name. |
||
165 | * |
||
166 | * @return string The unprefixed extension name. |
||
167 | */ |
||
168 | private static function remove_extension_prefix( $extension_name ) { |
||
174 | |||
175 | /** |
||
176 | * Whether two arrays share at least one item |
||
177 | * |
||
178 | * @param array $a An array. |
||
179 | * @param array $b Another array. |
||
180 | * |
||
181 | * @return boolean True if $a and $b share at least one item |
||
182 | */ |
||
183 | protected static function share_items( $a, $b ) { |
||
186 | |||
187 | /** |
||
188 | * Register a block |
||
189 | * |
||
190 | * @deprecated 7.1.0 Use jetpack_register_block() instead |
||
191 | * |
||
192 | * @param string $slug Slug of the block. |
||
193 | * @param array $args Arguments that are passed into register_block_type(). |
||
194 | */ |
||
195 | public static function register_block( $slug, $args ) { |
||
200 | |||
201 | /** |
||
202 | * Register a plugin |
||
203 | * |
||
204 | * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead |
||
205 | * |
||
206 | * @param string $slug Slug of the plugin. |
||
207 | */ |
||
208 | public static function register_plugin( $slug ) { |
||
213 | |||
214 | /** |
||
215 | * Register a block |
||
216 | * |
||
217 | * @deprecated 7.0.0 Use jetpack_register_block() instead |
||
218 | * |
||
219 | * @param string $slug Slug of the block. |
||
220 | * @param array $args Arguments that are passed into the register_block_type. |
||
221 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
222 | */ |
||
223 | public static function register( $slug, $args, $availability ) { |
||
232 | |||
233 | /** |
||
234 | * Set a (non-block) extension as available |
||
235 | * |
||
236 | * @param string $slug Slug of the extension. |
||
237 | */ |
||
238 | public static function set_extension_available( $slug ) { |
||
241 | |||
242 | /** |
||
243 | * Set the reason why an extension (block or plugin) is unavailable |
||
244 | * |
||
245 | * @param string $slug Slug of the extension. |
||
246 | * @param string $reason A string representation of why the extension is unavailable. |
||
247 | * @param array $details A free-form array containing more information on why the extension is unavailable. |
||
248 | */ |
||
249 | public static function set_extension_unavailable( $slug, $reason, $details = array() ) { |
||
280 | |||
281 | /** |
||
282 | * Set the reason why an extension (block or plugin) is unavailable |
||
283 | * |
||
284 | * @deprecated 7.1.0 Use set_extension_unavailable() instead |
||
285 | * |
||
286 | * @param string $slug Slug of the extension. |
||
287 | * @param string $reason A string representation of why the extension is unavailable. |
||
288 | */ |
||
289 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
||
294 | |||
295 | /** |
||
296 | * Set up a whitelist of allowed block editor extensions |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public static function init() { |
||
358 | |||
359 | /** |
||
360 | * Resets the class to its original state |
||
361 | * |
||
362 | * Used in unit tests |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | public static function reset() { |
||
370 | |||
371 | /** |
||
372 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
373 | * |
||
374 | * @return string The Gutenberg extensions directory |
||
375 | */ |
||
376 | public static function get_blocks_directory() { |
||
386 | |||
387 | /** |
||
388 | * Checks for a given .json file in the blocks folder. |
||
389 | * |
||
390 | * @param string $preset The name of the .json file to look for. |
||
391 | * |
||
392 | * @return bool True if the file is found. |
||
393 | */ |
||
394 | public static function preset_exists( $preset ) { |
||
397 | |||
398 | /** |
||
399 | * Decodes JSON loaded from a preset file in the blocks folder |
||
400 | * |
||
401 | * @param string $preset The name of the .json file to load. |
||
402 | * |
||
403 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
404 | */ |
||
405 | public static function get_preset( $preset ) { |
||
411 | |||
412 | /** |
||
413 | * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
414 | * |
||
415 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
416 | */ |
||
417 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
||
425 | |||
426 | /** |
||
427 | * Returns a diff from a combined list of whitelisted extensions and extensions determined to be excluded |
||
428 | * |
||
429 | * @param array $whitelisted_extensions An array of whitelisted extensions. |
||
430 | * |
||
431 | * @return array A list of blocks: eg array( 'publicize', 'markdown' ) |
||
432 | */ |
||
433 | public static function get_available_extensions( $whitelisted_extensions = null ) { |
||
439 | |||
440 | /** |
||
441 | * Get availability of each block / plugin. |
||
442 | * |
||
443 | * @return array A list of block and plugins and their availablity status |
||
444 | */ |
||
445 | public static function get_availability() { |
||
478 | |||
479 | /** |
||
480 | * Check if an extension/block is already registered |
||
481 | * |
||
482 | * @since 7.2 |
||
483 | * |
||
484 | * @param string $slug Name of extension/block to check. |
||
485 | * |
||
486 | * @return bool |
||
487 | */ |
||
488 | public static function is_registered( $slug ) { |
||
491 | |||
492 | /** |
||
493 | * Check if Gutenberg editor is available |
||
494 | * |
||
495 | * @since 6.7.0 |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | public static function is_gutenberg_available() { |
||
502 | |||
503 | /** |
||
504 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
505 | * |
||
506 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
507 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
508 | * |
||
509 | * @since 6.9.0 |
||
510 | * |
||
511 | * @return bool |
||
512 | */ |
||
513 | public static function should_load() { |
||
527 | |||
528 | /** |
||
529 | * Only enqueue block assets when needed. |
||
530 | * |
||
531 | * @param string $type Slug of the block. |
||
532 | * @param array $script_dependencies Script dependencies. Will be merged with automatically |
||
533 | * detected script dependencies from the webpack build. |
||
534 | * |
||
535 | * @return void |
||
536 | */ |
||
537 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
547 | |||
548 | /** |
||
549 | * Only enqueue block sytles when needed. |
||
550 | * |
||
551 | * @param string $type Slug of the block. |
||
552 | * |
||
553 | * @since 7.2.0 |
||
554 | * |
||
555 | * @return void |
||
556 | */ |
||
557 | public static function load_styles_as_required( $type ) { |
||
572 | |||
573 | /** |
||
574 | * Only enqueue block scripts when needed. |
||
575 | * |
||
576 | * @param string $type Slug of the block. |
||
577 | * @param array $dependencies Script dependencies. Will be merged with automatically |
||
578 | * detected script dependencies from the webpack build. |
||
579 | * |
||
580 | * @since 7.2.0 |
||
581 | * |
||
582 | * @return void |
||
583 | */ |
||
584 | public static function load_scripts_as_required( $type, $dependencies = array() ) { |
||
611 | |||
612 | /** |
||
613 | * Check if an asset exists for a block. |
||
614 | * |
||
615 | * @param string $file Path of the file we are looking for. |
||
616 | * |
||
617 | * @return bool $block_has_asset Does the file exist. |
||
618 | */ |
||
619 | public static function block_has_asset( $file ) { |
||
622 | |||
623 | /** |
||
624 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
625 | * |
||
626 | * @param string $file Path of the file we are looking for. |
||
627 | * |
||
628 | * @return string $script_version Version number. |
||
629 | */ |
||
630 | public static function get_asset_version( $file ) { |
||
635 | |||
636 | /** |
||
637 | * Load Gutenberg editor assets |
||
638 | * |
||
639 | * @since 6.7.0 |
||
640 | * |
||
641 | * @return void |
||
642 | */ |
||
643 | public static function enqueue_block_editor_assets() { |
||
727 | |||
728 | /** |
||
729 | * Some blocks do not depend on a specific module, |
||
730 | * and can consequently be loaded outside of the usual modules. |
||
731 | * We will look for such modules in the extensions/ directory. |
||
732 | * |
||
733 | * @since 7.1.0 |
||
734 | */ |
||
735 | public static function load_independent_blocks() { |
||
749 | |||
750 | /** |
||
751 | * Get CSS classes for a block. |
||
752 | * |
||
753 | * @since 7.7.0 |
||
754 | * |
||
755 | * @param string $slug Block slug. |
||
756 | * @param array $attr Block attributes. |
||
757 | * @param array $extra Potential extra classes you may want to provide. |
||
758 | * |
||
759 | * @return string $classes List of CSS classes for a block. |
||
760 | */ |
||
761 | public static function block_classes( $slug = '', $attr, $extra = array() ) { |
||
791 | |||
792 | /** |
||
793 | * Determine whether a site should use the default set of blocks, or a custom set. |
||
794 | * Possible variations are currently beta, experimental, and production. |
||
795 | * |
||
796 | * @since 8.1.0 |
||
797 | * |
||
798 | * @return string $block_varation production|beta|experimental |
||
799 | */ |
||
800 | public static function blocks_variation() { |
||
824 | |||
825 | /** |
||
826 | * Get a list of extensions available for the variation you chose. |
||
827 | * |
||
828 | * @since 8.1.0 |
||
829 | * |
||
830 | * @param obj $preset_extensions_manifest List of extensions available in Jetpack. |
||
831 | * @param string $blocks_variation Subset of blocks. production|beta|experimental. |
||
832 | * |
||
833 | * @return array $preset_extensions Array of extensions for that variation |
||
834 | */ |
||
835 | public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) { |
||
873 | |||
874 | /** |
||
875 | * Validate a URL used in a SSR block. |
||
876 | * |
||
877 | * @since 8.3.0 |
||
878 | * |
||
879 | * @param string $url URL saved as an attribute in block. |
||
880 | * @param array $allowed Array of allowed hosts for that block, or regexes to check against. |
||
881 | * @param bool $is_regex Array of regexes matching the URL that could be used in block. |
||
882 | * |
||
883 | * @return bool|string |
||
884 | */ |
||
885 | public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) { |
||
939 | |||
940 | } |
||
941 |
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.