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 |
||
55 | class Jetpack_Gutenberg { |
||
56 | |||
57 | /** |
||
58 | * Only these extensions can be registered. Used to control availability of beta blocks. |
||
59 | * |
||
60 | * @var array Extensions whitelist |
||
61 | */ |
||
62 | private static $extensions = array(); |
||
63 | |||
64 | /** |
||
65 | * Keeps track of the reasons why a given extension is unavailable. |
||
66 | * |
||
67 | * @var array Extensions availability information |
||
68 | */ |
||
69 | private static $availability = array(); |
||
70 | |||
71 | /** |
||
72 | * Since there is no `register_plugin()` counterpart to `register_block_type()` in Gutenberg, |
||
73 | * we have to keep track of plugin registration ourselves |
||
74 | * |
||
75 | * @var array Plugin registry |
||
76 | */ |
||
77 | private static $registered_plugins = array(); |
||
78 | |||
79 | /** |
||
80 | * Prepend the 'jetpack/' prefix to a block name |
||
81 | * |
||
82 | * @param string $block_name The block name. |
||
83 | * |
||
84 | * @return string The prefixed block name. |
||
85 | */ |
||
86 | private static function prepend_block_prefix( $block_name ) { |
||
89 | |||
90 | /** |
||
91 | * Whether two arrays share at least one item |
||
92 | * |
||
93 | * @param array $a An array. |
||
94 | * @param array $b Another array. |
||
95 | * |
||
96 | * @return boolean True if $a and $b share at least one item |
||
97 | */ |
||
98 | protected static function share_items( $a, $b ) { |
||
101 | |||
102 | /** |
||
103 | * Register a block |
||
104 | * |
||
105 | * If the block isn't whitelisted, set its unavailability reason instead. |
||
106 | * |
||
107 | * @param string $slug Slug of the block. |
||
108 | * @param array $args Arguments that are passed into register_block_type(). |
||
109 | */ |
||
110 | public static function register_block( $slug, $args ) { |
||
121 | |||
122 | /** |
||
123 | * Register a plugin |
||
124 | * |
||
125 | * If the plugin isn't whitelisted, set its unavailability reason instead. |
||
126 | * |
||
127 | * @param string $slug Slug of the plugin. |
||
128 | */ |
||
129 | public static function register_plugin( $slug ) { |
||
136 | |||
137 | /** |
||
138 | * Register a block |
||
139 | * |
||
140 | * @deprecated 7.0.0 Use register_block() instead |
||
141 | * |
||
142 | * @param string $slug Slug of the block. |
||
143 | * @param array $args Arguments that are passed into the register_block_type. |
||
144 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
145 | */ |
||
146 | public static function register( $slug, $args, $availability ) { |
||
153 | |||
154 | /** |
||
155 | * Set the reason why an extension (block or plugin) is unavailable |
||
156 | * |
||
157 | * @param string $slug Slug of the extension. |
||
158 | * @param string $reason A string representation of why the extension is unavailable. |
||
159 | */ |
||
160 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
||
163 | |||
164 | /** |
||
165 | * Set up a whitelist of allowed block editor extensions |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | public static function init() { |
||
220 | |||
221 | /** |
||
222 | * Resets the class to its original state |
||
223 | * |
||
224 | * Used in unit tests |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | public static function reset() { |
||
233 | |||
234 | /** |
||
235 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
236 | * |
||
237 | * @return string The Gutenberg extensions directory |
||
238 | */ |
||
239 | public static function get_blocks_directory() { |
||
249 | |||
250 | /** |
||
251 | * Checks for a given .json file in the blocks folder. |
||
252 | * |
||
253 | * @param string $preset The name of the .json file to look for. |
||
254 | * |
||
255 | * @return bool True if the file is found. |
||
256 | */ |
||
257 | public static function preset_exists( $preset ) { |
||
260 | |||
261 | /** |
||
262 | * Decodes JSON loaded from a preset file in the blocks folder |
||
263 | * |
||
264 | * @param string $preset The name of the .json file to load. |
||
265 | * |
||
266 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
267 | */ |
||
268 | public static function get_preset( $preset ) { |
||
271 | |||
272 | /** |
||
273 | * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
274 | * |
||
275 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
276 | */ |
||
277 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
||
289 | |||
290 | /** |
||
291 | * Get availability of each block / plugin. |
||
292 | * |
||
293 | * @return array A list of block and plugins and their availablity status |
||
294 | */ |
||
295 | public static function get_availability() { |
||
322 | |||
323 | /** |
||
324 | * Check if Gutenberg editor is available |
||
325 | * |
||
326 | * @since 6.7.0 |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | public static function is_gutenberg_available() { |
||
333 | |||
334 | /** |
||
335 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
336 | * |
||
337 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
338 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
339 | * |
||
340 | * @since 6.9.0 |
||
341 | * |
||
342 | * @return bool |
||
343 | */ |
||
344 | public static function should_load() { |
||
358 | |||
359 | /** |
||
360 | * Only enqueue block assets when needed. |
||
361 | * |
||
362 | * @param string $type slug of the block. |
||
363 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
396 | |||
397 | /** |
||
398 | * Check if an asset exists for a block. |
||
399 | * |
||
400 | * @param string $file Path of the file we are looking for. |
||
401 | * |
||
402 | * @return bool $block_has_asset Does the file exist. |
||
403 | */ |
||
404 | public static function block_has_asset( $file ) { |
||
407 | |||
408 | /** |
||
409 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
410 | * |
||
411 | * @param string $file Path of the file we are looking for. |
||
412 | * |
||
413 | * @return string $script_version Version number. |
||
414 | */ |
||
415 | public static function get_asset_version( $file ) { |
||
420 | |||
421 | /** |
||
422 | * Load Gutenberg editor assets |
||
423 | * |
||
424 | * @since 6.7.0 |
||
425 | * |
||
426 | * @return void |
||
427 | */ |
||
428 | public static function enqueue_block_editor_assets() { |
||
490 | } |
||
491 |