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_Photon_Static_Assets_CDN 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_Photon_Static_Assets_CDN, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Jetpack_Photon_Static_Assets_CDN { |
||
| 22 | const CDN = 'https://c0.wp.com/'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Sets up action handlers needed for Jetpack CDN. |
||
| 26 | */ |
||
| 27 | public static function go() { |
||
| 28 | add_action( 'wp_print_scripts', array( __CLASS__, 'cdnize_assets' ) ); |
||
| 29 | add_action( 'wp_print_styles', array( __CLASS__, 'cdnize_assets' ) ); |
||
| 30 | add_action( 'admin_print_scripts', array( __CLASS__, 'cdnize_assets' ) ); |
||
| 31 | add_action( 'admin_print_styles', array( __CLASS__, 'cdnize_assets' ) ); |
||
| 32 | add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) ); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Sets up CDN URLs for assets that are enqueued by the WordPress Core. |
||
| 37 | */ |
||
| 38 | public static function cdnize_assets() { |
||
| 39 | global $wp_scripts, $wp_styles, $wp_version; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Filters Jetpack CDN's Core version number and locale. Can be used to override the values |
||
| 43 | * that Jetpack uses to retrieve assets. Expects the values to be returned in an array. |
||
| 44 | * |
||
| 45 | * @since 6.6 |
||
| 46 | * |
||
| 47 | * @param array $values array( $version = core assets version, i.e. 4.9.8, $locale = desired locale ) |
||
| 48 | */ |
||
| 49 | list( $version, $locale ) = apply_filters( |
||
|
|
|||
| 50 | 'jetpack_cdn_core_version_and_locale', |
||
| 51 | array( $wp_version, get_locale() ) |
||
| 52 | ); |
||
| 53 | |||
| 54 | if ( self::is_public_version( $version ) ) { |
||
| 55 | $site_url = trailingslashit( site_url() ); |
||
| 56 | View Code Duplication | foreach ( $wp_scripts->registered as $handle => $thing ) { |
|
| 57 | if ( wp_startswith( $thing->src, self::CDN ) ) { |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' ); |
||
| 61 | if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) { |
||
| 62 | $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src ); |
||
| 63 | $wp_scripts->registered[ $handle ]->ver = null; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | View Code Duplication | foreach ( $wp_styles->registered as $handle => $thing ) { |
|
| 67 | if ( wp_startswith( $thing->src, self::CDN ) ) { |
||
| 68 | continue; |
||
| 69 | } |
||
| 70 | $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' ); |
||
| 71 | if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) { |
||
| 72 | $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src ); |
||
| 73 | $wp_styles->registered[ $handle ]->ver = null; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION ); |
||
| 79 | if ( class_exists( 'WooCommerce' ) ) { |
||
| 80 | self::cdnize_plugin_assets( 'woocommerce', WC_VERSION ); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets up CDN URLs for supported plugin assets. |
||
| 86 | * |
||
| 87 | * @param String $plugin_slug plugin slug string. |
||
| 88 | * @param String $current_version plugin version string. |
||
| 89 | * @return null|bool |
||
| 90 | */ |
||
| 91 | public static function cdnize_plugin_assets( $plugin_slug, $current_version ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns cdn-able assets for a given plugin. |
||
| 144 | * |
||
| 145 | * @param string $plugin plugin slug string. |
||
| 146 | * @param string $version plugin version number string. |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public static function get_plugin_assets( $plugin, $version ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Checks a path whether it is a JS or CSS file. |
||
| 214 | * |
||
| 215 | * @param String $path file path. |
||
| 216 | * @return Boolean whether the file is a JS or CSS. |
||
| 217 | */ |
||
| 218 | public static function is_js_or_css_file( $path ) { |
||
| 219 | return ( false === strpos( $path, '?' ) ) && in_array( substr( $path, -3 ), array( 'css', '.js' ), true ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Checks whether the version string indicates a production version. |
||
| 224 | * |
||
| 225 | * @param String $version the version string. |
||
| 226 | * @param Boolean $include_beta_and_rc whether to count beta and RC versions as production. |
||
| 227 | * @return Boolean |
||
| 228 | */ |
||
| 229 | public static function is_public_version( $version, $include_beta_and_rc = false ) { |
||
| 240 | } |
||
| 241 | Jetpack_Photon_Static_Assets_CDN::go(); |
||
| 242 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.