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 | ); |
||
66 | |||
67 | /** |
||
68 | * @var array Array of blocks information. |
||
69 | * |
||
70 | * For each block we have information about the availability for the current user |
||
71 | */ |
||
72 | private static $blocks = array(); |
||
73 | |||
74 | private static $availability = array(); |
||
75 | |||
76 | // PLUGINS |
||
77 | private static $default_plugins = array( |
||
78 | 'publicize', |
||
79 | 'shortlinks', |
||
80 | ); |
||
81 | /** |
||
82 | * @var array Array of plugins information. |
||
83 | * |
||
84 | * For each block we have information about the availability for the current user |
||
85 | */ |
||
86 | private static $plugins = array(); |
||
87 | |||
88 | /** |
||
89 | * @var array Array of extensions we will be registering. |
||
90 | */ |
||
91 | private static $registered = array(); |
||
92 | |||
93 | /** |
||
94 | * Add a block to the list of blocks to be registered. |
||
95 | * |
||
96 | * @param string $slug Slug of the block. |
||
97 | * @param array $args Arguments that are passed into the register_block_type. |
||
98 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
99 | */ |
||
100 | public static function register( $slug, $args, $availability ) { |
||
104 | |||
105 | /** |
||
106 | * Register all Jetpack blocks available. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public static function init() { |
||
128 | |||
129 | /** |
||
130 | * @deprecated |
||
131 | */ |
||
132 | static function load_blocks() { |
||
135 | |||
136 | /** |
||
137 | * Add a block to the list of blocks to be registered. |
||
138 | * |
||
139 | * @param string $slug Slug of the block. |
||
140 | * @param array $args Arguments that are passed into the register_block_type. |
||
141 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
142 | * |
||
143 | * @deprecated |
||
144 | */ |
||
145 | static function add_block( $type, $args, $availability ) { |
||
148 | |||
149 | static function load( $request = null ) { |
||
176 | |||
177 | static function is_registered( $slug ) { |
||
180 | |||
181 | static function get_registered_args( $slug ) { |
||
187 | |||
188 | static function is_available( $slug ) { |
||
194 | |||
195 | |||
196 | static function get_extension_availability( $slug ) { |
||
219 | |||
220 | static function set_blocks_availability() { |
||
225 | |||
226 | static function set_plugins_availability() { |
||
231 | |||
232 | static function register_blocks() { |
||
239 | |||
240 | /** |
||
241 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
242 | * |
||
243 | * @return string The Gutenberg extensions directory |
||
244 | */ |
||
245 | public static function get_blocks_directory() { |
||
255 | |||
256 | /** |
||
257 | * Checks for a given .json file in the blocks folder. |
||
258 | * |
||
259 | * @param $preset The name of the .json file to look for. |
||
260 | * |
||
261 | * @return bool True if the file is found. |
||
262 | */ |
||
263 | public static function preset_exists( $preset ) { |
||
266 | |||
267 | /** |
||
268 | * Decodes JSON loaded from a preset file in the blocks folder |
||
269 | * |
||
270 | * @param $preset The name of the .json file to load. |
||
271 | * |
||
272 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
273 | */ |
||
274 | public static function get_preset( $preset ) { |
||
277 | |||
278 | /** |
||
279 | * Filters the results of `apply_filter( 'jetpack_set_available_blocks', array() )` |
||
280 | * using the merged contents of `blocks-manifest.json` ( $preset_blocks ) |
||
281 | * and self::$jetpack_blocks ( $internal_blocks ) |
||
282 | * |
||
283 | * @param $blocks The default list. |
||
284 | * |
||
285 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
286 | */ |
||
287 | public static function jetpack_set_available_blocks( $blocks ) { |
||
301 | |||
302 | /** |
||
303 | * @deprecated |
||
304 | * @return array A list of block-availability information, eg: [ "publicize" => ["available" => true ], "markdown" => [ "available" => false, "unavailable_reason" => 'missing_module' ] ] |
||
305 | */ |
||
306 | public static function get_block_availability() { |
||
309 | |||
310 | /** |
||
311 | * @return array A list of block and plugins and their availablity status |
||
312 | */ |
||
313 | public static function get_availability() { |
||
320 | |||
321 | /** |
||
322 | * Check if Gutenberg editor is available |
||
323 | * |
||
324 | * @since 6.7.0 |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | public static function is_gutenberg_available() { |
||
331 | |||
332 | /** |
||
333 | * Check whether conditions indicate Gutenberg blocks should be loaded |
||
334 | * |
||
335 | * Loading blocks is enabled by default and may be disabled via filter: |
||
336 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
337 | * |
||
338 | * @since 6.7.0 |
||
339 | * @deprecated |
||
340 | * @return bool |
||
341 | */ |
||
342 | public static function should_load_blocks() { |
||
345 | |||
346 | /** |
||
347 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
348 | * |
||
349 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
350 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
351 | * |
||
352 | * @since 6.9.0 |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | public static function should_load() { |
||
370 | |||
371 | /** |
||
372 | * Only enqueue block assets when needed. |
||
373 | * |
||
374 | * @param string $type slug of the block. |
||
375 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
||
376 | * |
||
377 | * @return void |
||
378 | */ |
||
379 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
408 | |||
409 | /** |
||
410 | * Check if an asset exists for a block. |
||
411 | * |
||
412 | * @param string $file Path of the file we are looking for. |
||
413 | * |
||
414 | * @return bool $block_has_asset Does the file exist. |
||
415 | */ |
||
416 | public static function block_has_asset( $file ) { |
||
419 | |||
420 | /** |
||
421 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
422 | * |
||
423 | * @param string $file Path of the file we are looking for. |
||
424 | * |
||
425 | * @return string $script_version Version number. |
||
426 | */ |
||
427 | public static function get_asset_version( $file ) { |
||
432 | |||
433 | /** |
||
434 | * Load Gutenberg editor assets |
||
435 | * |
||
436 | * @since 6.7.0 |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | public static function enqueue_block_editor_assets() { |
||
505 | } |
||
506 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.