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 |
||
| 30 | class Jetpack_Gutenberg { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Array of blocks we will be registering. |
||
| 34 | * |
||
| 35 | * @var array $blocks Array of blocks we will be registering. |
||
| 36 | */ |
||
| 37 | private static $jetpack_blocks = array(); |
||
| 38 | private static $blocks_index = array(); |
||
| 39 | /** |
||
| 40 | * Add a block to the list of blocks to be registered. |
||
| 41 | * |
||
| 42 | * @param string $type Slug of the block. |
||
| 43 | * @param array $args Arguments that are passed into the register_block_type. |
||
| 44 | */ |
||
| 45 | public static function add_block( $type, $args, $availability ) { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Register all Jetpack blocks available. |
||
| 51 | * |
||
| 52 | * @return void|WP_Block_Type|false The registered block type on success, or false on failure. |
||
| 53 | */ |
||
| 54 | public static function load_blocks() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Checks for a given .json file in the `_inc/blocks` folder. |
||
| 88 | * |
||
| 89 | * @param $preset The name of the .json file to look for. |
||
| 90 | * |
||
| 91 | * @return bool True if the file is found. |
||
| 92 | */ |
||
| 93 | public static function preset_exists( $preset ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Decodes JSON loaded from a preset file in `_inc/blocks` |
||
| 99 | * |
||
| 100 | * @param $preset The name of the .json file to load. |
||
| 101 | * |
||
| 102 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
| 103 | */ |
||
| 104 | public static function get_preset( $preset ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Filters the results of `apply_filter( 'jetpack_set_available_blocks', array() )` |
||
| 110 | * using the merged contents of `_inc/blocks/blocks-manifest.json` ( $preset_blocks ) |
||
| 111 | * and self::$jetpack_blocks ( $internal_blocks ) |
||
| 112 | * |
||
| 113 | * @param $blocks The default list. |
||
| 114 | * |
||
| 115 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
| 116 | */ |
||
| 117 | public static function jetpack_set_available_blocks( $blocks ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return array A list of block-availability information, eg: [ "publicize" => ["available" => true ], "markdown" => [ "available" => false, "unavailable_reason" => 'missing_module' ] ] |
||
| 132 | */ |
||
| 133 | public static function get_block_availability() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Check if Gutenberg editor is available |
||
| 170 | * |
||
| 171 | * @since 6.7.0 |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public static function is_gutenberg_available() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check whether conditions indicate Gutenberg blocks should be loaded |
||
| 181 | * |
||
| 182 | * Loading blocks is enabled by default and may be disabled via filter: |
||
| 183 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
| 184 | * |
||
| 185 | * @since 6.7.0 |
||
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public static function should_load_blocks() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Only enqueue block assets when needed. |
||
| 206 | * |
||
| 207 | * @param string $type slug of the block. |
||
| 208 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
||
| 209 | * |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Check if an asset exists for a block. |
||
| 244 | * |
||
| 245 | * @param string $file Path of the file we are looking for. |
||
| 246 | * |
||
| 247 | * @return bool $block_has_asset Does the file exist. |
||
| 248 | */ |
||
| 249 | public static function block_has_asset( $file ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 255 | * |
||
| 256 | * @param string $file Path of the file we are looking for. |
||
| 257 | * |
||
| 258 | * @return string $script_version Version number. |
||
| 259 | */ |
||
| 260 | public static function get_asset_version( $file ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Load Gutenberg editor assets |
||
| 268 | * |
||
| 269 | * @since 6.7.0 |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public static function enqueue_block_editor_assets() { |
||
| 336 | } |
||
| 337 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.